3.9 视图层打薄【重要】

3.9 视图层打薄【重要】

方案

  • 使用 extension 扩展方式简化代码
  • 抽取组件,提高复用率 基础组件业务组件
  • 样式全局管理 字体、颜色、间距、圆角
  • assets 资源索引 图片、文件、图标 svg

原来的代码

image-20220712101910612

lib/pages/login.dart

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import 'package:flutter/material.dart';
import 'package:flutter_quickstart_learn/common/index.dart';

/// 登录页面
class LoginPage extends StatefulWidget {
const LoginPage({Key? key}) : super(key: key);

@override
State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
// 账号输入是否有效
bool isUserNameValid = false;

// 登录表单
Widget _buildForm() {
return Container(
padding: const EdgeInsets.fromLTRB(20, 50, 20, 20),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(35),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Username or E-Mail
const Text(
"Username or E-Mail",
style: TextStyle(
fontSize: 15,
color: Color(0xff838383),
fontWeight: FontWeight.w300,
),
),
TextField(
onChanged: (value) {
bool valid = false;
if (value.length >= 6) {
valid = true;
} else {
valid = false;
}

setState(() {
isUserNameValid = valid;
});
},
decoration: InputDecoration(
hintText: "@",
// labelText: "Username or E-Mail",
// labelStyle: const TextStyle(
// fontSize: 15,
// color: Color(0xff838383),
// fontWeight: FontWeight.w300,
// ),
prefixIcon: Image.asset(
AssetsImages.iconUserPng,
width: 23,
height: 23,
),
suffixIcon: isUserNameValid == true
? const Icon(
Icons.done,
color: Colors.green,
)
: null,
),
),

// 间距
const SizedBox(height: 35),

// Password
const Text(
"Password",
style: TextStyle(
fontSize: 15,
color: Color(0xff838383),
fontWeight: FontWeight.w300,
),
),
TextField(
obscureText: true,
decoration: InputDecoration(
hintText: "6 digits",
// labelText: "Password",
// labelStyle: const TextStyle(
// fontSize: 15,
// color: Color(0xff838383),
// fontWeight: FontWeight.w300,
// ),
prefixIcon: Image.asset(
AssetsImages.iconLockPng,
width: 23,
height: 23,
),
suffixIcon: TextButton(
onPressed: () {},
child: const Text(
"Forget?",
style: TextStyle(
fontSize: 15,
color: Color(0xff0274bc),
fontWeight: FontWeight.w500,
),
),
),
),
),

// 间距
const SizedBox(height: 30),

// Sign In
ButtonWidget(
text: 'Sign In',
onPressed: () {},
height: 60,
widget: double.infinity,
radius: 18,
),

// 间距
const SizedBox(height: 16),

// Don’t have an account? + Sign Up
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// 文字
const Text(
"Don’t have an account? ",
style: TextStyle(
fontSize: 15,
color: Color(0xff171717),
fontWeight: FontWeight.w300,
),
),
// 按钮
TextButton(
onPressed: () {},
child: const Text(
"Sign Up",
style: TextStyle(
fontSize: 15,
color: Color(0xff0274bc),
fontWeight: FontWeight.bold,
),
),
),
],
),
],
),
);
}

// 主视图
Widget _buildView() {
return Container(
padding: const EdgeInsets.fromLTRB(15, 150, 15, 0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// 图标
Image.asset(
AssetsImages.logoPng,
width: 60,
),

const SizedBox(height: 20),

// 主标
const Text(
"Let’s Sign You In",
style: TextStyle(
fontSize: 20,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),

const SizedBox(height: 10),

// 子标
const Text(
"Welcome back, you’ve been missed!",
style: TextStyle(
fontSize: 13,
color: Colors.white,
fontWeight: FontWeight.w300,
),
),

const SizedBox(height: 50),

// 表单
_buildForm(),
],
),
);
}

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: AppColors.backgroundSplash,
body: SingleChildScrollView(child: _buildView()),
);
}
}

212 行 5528 字符数

打薄后代码

lib/pages/system/login_quick/view.dart

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import 'package:flutter/material.dart';
import 'package:flutter_woo_commerce_getx_learn/common/index.dart';
import 'package:get/get.dart';

import 'index.dart';

class LoginQuickPage extends GetView<LoginQuickController> {
const LoginQuickPage({Key? key}) : super(key: key);

// 主视图
Widget _buildView() {
return <Widget>[
// logo
const ImageWidget.asset(
AssetsImages.logoPng,
width: 60,
height: 57,
).paddingBottom(22),

// 标题1
TextWidget.title2(
"Let’s Sign You In",
color: AppColors.onPrimary,
).paddingBottom(10),

// 标题2
TextWidget.body2(
"Welcome back, you’ve",
color: AppColors.onPrimary,
).paddingBottom(55),

// 表单
<Widget>[
// username
const TextWidget.body1(
"Username or E-Mail",
color: Color(0xff838383),
).paddingBottom(AppSpace.listRow),

// username input
InputWidget.iconTextFilled(IconWidget.icon(Icons.person))
.paddingBottom(AppSpace.listRow * 2),

// password
const TextWidget.body1(
"Password",
color: Color(0xff838383),
).paddingBottom(AppSpace.listRow),

// password input
InputWidget.iconTextFilled(IconWidget.icon(Icons.lock_outline))
.paddingBottom(29),

// 登录按钮
const ButtonWidget.primary(
"Sıgn In",
backgroundColor: Color(0xffFD8700),
borderRadius: 18,
).tight(width: double.infinity, height: 57),

// 注册
<Widget>[
// 文字
const TextWidget.body1(
"Don’t have an accoun?",
color: Color(0xff838383),
).paddingRight(AppSpace.listItem),

// 注册按钮
ButtonWidget.text(
"Sign Up",
textColor: const Color(0xff0274BC),
textWeight: FontWeight.bold,
),
].toRow(
mainAxisAlignment: MainAxisAlignment.center,
),
]
.toColumn(
crossAxisAlignment: CrossAxisAlignment.start,
)
.paddingAll(20)
.card(
color: Colors.white,
radius: 35,
),

// end
]
.toColumn(
mainAxisAlignment: MainAxisAlignment.center,
)
.paddingHorizontal(15);
}

@override
Widget build(BuildContext context) {
return GetBuilder<LoginQuickController>(
init: LoginQuickController(),
id: "login_quick",
builder: (_) {
return Scaffold(
backgroundColor: const Color(0xff0274BC),
body: SafeArea(
child: _buildView(),
),
);
},
);
}
}

111 行 2730 字符数

总结

通过方案实施后

  • 代码量减少了
  • 书写方式简洁,更容易阅读
  • 可维护性强了
  • 代码层次分明,各司其职