3.6 按钮组件封装

3.6 按钮组件封装

实现步骤:


第 1 步:组件定义

设计稿参考

img

定义如下

name 说明
primary 主按钮
secondary 次按钮
text 文字按钮
icon 图标按钮
textFilled 文字/填充
textRoundFilled 文字/填充/圆形
iconTextUpDown 图标/文字/上下
iconTextOutlined 图标/文字/边框
iconTextUpDownOutlined 图标/文字/上下/边框
textIcon 文字/图标

第 2 步:ButtonWidget 封装

lib/common/widgets/button.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import 'package:flutter/material.dart';
import '../index.dart';

enum ButtonWidgetType {
none, // 无
primary, // 主按钮
secondary, // 次按钮
text, // 文字按钮
icon, // 图标按钮
textFilled, // 文字/填充
textRoundFilled, // 文字/填充/圆形
iconTextUpDown, // 图标/文字/上下
iconTextOutlined, // 图标/文字/边框
iconTextUpDownOutlined, // 图标/文字/上下/边框
textIcon, // 文字/图标
}

/// 按钮
class ButtonWidget extends StatelessWidget {
/// 按钮类型
final ButtonWidgetType type;

/// tap 事件
final Function()? onTap;

/// 文字字符串
final String? text;

/// 子组件
final Widget? child;

/// 图标
final Widget? icon;

/// 圆角
final double? borderRadius;

/// 背景色
final Color? backgroundColor;

/// 边框色
final Color? borderColor;

/// 宽度
final double? width;

/// 高度
final double? height;

const ButtonWidget({
Key? key,
this.type = ButtonWidgetType.none,
this.onTap,
this.text,
this.borderRadius,
this.child,
this.backgroundColor,
this.icon,
this.borderColor,
this.width,
this.height,
}) : super(key: key);

/// 主要
const ButtonWidget.primary(
this.text, {
Key? key,
this.type = ButtonWidgetType.primary,
this.width = double.infinity,
this.height = 50,
this.onTap,
this.borderRadius,
this.child,
this.backgroundColor,
this.icon,
this.borderColor,
}) : super(key: key);

/// 次要
const ButtonWidget.secondary(
this.text, {
Key? key,
this.type = ButtonWidgetType.secondary,
this.width = double.infinity,
this.height = 50,
this.onTap,
this.borderRadius,
this.child,
this.backgroundColor,
this.icon,
this.borderColor,
}) : super(key: key);

/// 文字
ButtonWidget.text(
this.text, {
Key? key,
this.type = ButtonWidgetType.text,
this.onTap,
Color? textColor,
double? textSize,
FontWeight? textWeight,
this.borderRadius,
this.backgroundColor,
this.icon,
this.borderColor,
this.width,
this.height,
}) : child = TextWidget.button(
text: text!,
size: textSize,
color: textColor ?? AppColors.onPrimaryContainer,
weight: textWeight,
),
super(key: key);

/// 图标
const ButtonWidget.icon(
this.icon, {
Key? key,
this.type = ButtonWidgetType.icon,
this.onTap,
this.text,
this.borderRadius,
this.backgroundColor,
this.child,
this.borderColor,
this.width,
this.height,
}) : super(key: key);

/// 文字/填充
ButtonWidget.textFilled(
this.text, {
Key? key,
this.type = ButtonWidgetType.textFilled,
Color? bgColor,
Color? textColor,
double? textSize,
FontWeight? textWeight,
this.onTap,
this.borderRadius,
this.icon,
this.borderColor,
this.width,
this.height,
}) : backgroundColor = bgColor ?? AppColors.primary,
child = TextWidget.button(
text: text!,
size: textSize,
color: textColor ?? AppColors.onPrimaryContainer,
weight: textWeight,
),
super(key: key);

/// 文字/填充/圆形 按钮
ButtonWidget.textRoundFilled(
this.text, {
Key? key,
this.type = ButtonWidgetType.textRoundFilled,
Color? bgColor,
Color? textColor,
double? textSize,
FontWeight? textWeight,
this.onTap,
this.borderRadius,
this.icon,
this.borderColor,
this.width,
this.height,
}) : backgroundColor = bgColor ?? AppColors.primary,
child = TextWidget.button(
text: text!,
size: textSize,
color: textColor ?? AppColors.onPrimaryContainer,
weight: textWeight,
),
super(key: key);

/// 图标文字 上下
ButtonWidget.iconTextUpDown(
this.icon,
this.text, {
Key? key,
this.type = ButtonWidgetType.iconTextUpDown,
Color? textColor,
double? textSize,
FontWeight? textWeight,
this.onTap,
this.borderRadius,
this.backgroundColor,
this.borderColor,
this.width,
this.height,
}) : child = <Widget>[
icon!,
TextWidget.button(
text: text!,
size: textSize,
color: textColor ?? AppColors.onPrimaryContainer,
weight: textWeight,
),
].toColumn(
mainAxisSize: MainAxisSize.min,
),
super(key: key);

/// 图标 / 文字 / 边框
ButtonWidget.iconTextOutlined(
this.icon,
this.text, {
Key? key,
this.type = ButtonWidgetType.iconTextOutlined,
this.onTap,
Color? textColor,
double? textSize,
FontWeight? textWeight,
this.borderRadius,
this.backgroundColor,
this.borderColor,
this.width,
this.height,
}) : child = <Widget>[
icon!.paddingRight(AppSpace.iconTextSmail),
TextWidget.button(
text: text!,
size: textSize,
color: textColor ?? AppColors.onPrimaryContainer,
weight: textWeight,
),
].toRow(
mainAxisSize: MainAxisSize.min,
),
super(key: key);

/// 图标 / 文字 / 上下 / 边框
ButtonWidget.iconTextUpDownOutlined(
this.icon,
this.text, {
Key? key,
this.type = ButtonWidgetType.iconTextUpDownOutlined,
this.onTap,
Color? textColor,
double? textSize,
FontWeight? textWeight,
this.borderRadius,
this.backgroundColor,
this.borderColor,
this.width,
this.height,
}) : child = <Widget>[
icon!.paddingBottom(AppSpace.iconTextSmail),
TextWidget.button(
text: text!,
size: textSize,
color: textColor ?? AppColors.onPrimaryContainer,
weight: textWeight,
),
].toColumn(
mainAxisSize: MainAxisSize.min,
),
super(key: key);

/// 文字 / 图标
ButtonWidget.textIcon(
this.text,
this.icon, {
Key? key,
Color? textColor,
double? textSize,
FontWeight? textWeight,
this.type = ButtonWidgetType.textIcon,
this.onTap,
this.borderRadius,
this.backgroundColor,
this.borderColor,
this.width,
this.height,
}) : child = <Widget>[
TextWidget.button(
text: text!,
size: textSize,
color: textColor ?? AppColors.onPrimaryContainer,
weight: textWeight,
).paddingRight(AppSpace.iconTextSmail),
icon!,
].toRow(
mainAxisSize: MainAxisSize.min,
),
super(key: key);

// 背景
MaterialStateProperty<Color?>? get _backgroundColor {
switch (type) {
case ButtonWidgetType.primary:
return MaterialStateProperty.all(backgroundColor ?? AppColors.primary);
default:
return MaterialStateProperty.all(backgroundColor ?? Colors.transparent);
}
}

// 边框
MaterialStateProperty<BorderSide?>? get _side {
switch (type) {
case ButtonWidgetType.secondary:
return MaterialStateProperty.all(BorderSide(
color: borderColor ?? AppColors.primary,
width: 1,
));
case ButtonWidgetType.iconTextOutlined:
case ButtonWidgetType.iconTextUpDownOutlined:
return MaterialStateProperty.all(BorderSide(
color: borderColor ?? AppColors.outline,
width: 1,
));
default:
return null;
}
}

// 阴影颜色
MaterialStateProperty<Color?>? get _overlayColor {
switch (type) {
case ButtonWidgetType.primary:
return null;
default:
return MaterialStateProperty.all(AppColors.surfaceVariant);
}
}

// 形状圆角
MaterialStateProperty<OutlinedBorder?>? get _shape {
switch (type) {
case ButtonWidgetType.primary:
case ButtonWidgetType.secondary:
return MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(borderRadius ?? AppRadius.button)),
),
);
case ButtonWidgetType.textFilled:
case ButtonWidgetType.iconTextOutlined:
case ButtonWidgetType.iconTextUpDownOutlined:
return MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(borderRadius ?? AppRadius.buttonTextFilled)),
),
);
case ButtonWidgetType.textRoundFilled:
return MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(borderRadius ?? 0)),
),
);
default:
return null;
}
}

// padding
MaterialStateProperty<EdgeInsetsGeometry?>? get _padding {
switch (type) {
// case ButtonWidgetType.primary:
// case ButtonWidgetType.secondary:
// return null;
default:
return MaterialStateProperty.all(EdgeInsets.zero);
}
}

// 子元素
Widget? get _child {
switch (type) {
case ButtonWidgetType.primary:
return TextWidget.button(
text: text!,
color: AppColors.onPrimary,
);
case ButtonWidgetType.secondary:
return TextWidget.button(
text: text!,
color: AppColors.primary,
);
case ButtonWidgetType.icon:
return icon;
default:
return child;
}
}

@override
Widget build(BuildContext context) {
return SizedBox(
width: width,
height: height,
child: ElevatedButton(
onPressed: onTap,
child: _child,
style: ButtonStyle(
minimumSize: MaterialStateProperty.all(const Size(0, 0)),
elevation: MaterialStateProperty.all(0),
backgroundColor: _backgroundColor,
side: _side,
overlayColor: _overlayColor,
shape: _shape,
padding: _padding,
),
),
);
}
}

第 3 步:按钮调试

lib/pages/styles/styles_index/view.dart

1
2
3
4
5
// Button 按钮
ListTile(
onTap: () => Get.toNamed(RouteNames.stylesButtons),
title: const TextWidget.body1("Button 按钮"),
),

lib/pages/styles/buttons/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
112
113
114
115

Widget _buildView() {
return SingleChildScrollView(
child: Column(
children: [
_buildButtons(),
],
).paddingAll(AppSpace.card),
);
}

Widget _buildButtons() {
return <Widget>[
// primary 主按钮
ButtonWidget.primary(
"主按钮",
onTap: () {},
).paddingBottom(AppSpace.listRow),

// secondary 次按钮
ButtonWidget.secondary(
"次按钮",
onTap: () {},
).paddingBottom(AppSpace.listRow),

// 文字按钮
ButtonWidget.text(
"文字按钮",
textSize: 15,
onTap: () {},
// onTap: () async {
// await ConfigService.to.switchThemeModel();
// controller.update(["buttons"]);
// },
).paddingBottom(AppSpace.listRow),

// 图标按钮
ButtonWidget.icon(
IconWidget.svg(
AssetsSvgs.cHomeSvg,
size: 30,
),
onTap: () {},
).tightSize(30).paddingBottom(AppSpace.listRow),

// 文字/填充 按钮
ButtonWidget.textFilled(
"15",
bgColor: Get.theme.colorScheme.surfaceVariant.withOpacity(0.5),
textSize: 12,
onTap: () {},
).height(30).width(45).paddingBottom(AppSpace.listRow),

// 文字/填充/圆形 按钮
ButtonWidget.textRoundFilled(
"5",
bgColor: Get.theme.colorScheme.surfaceVariant.withOpacity(0.4),
borderRadius: 12,
textSize: 9,
onTap: () {},
).tight(width: 24, height: 24).paddingBottom(AppSpace.listRow),

// iconTextUpDown, // 图标/文字/上下
ButtonWidget.iconTextUpDown(
IconWidget.svg(
AssetsSvgs.cHomeSvg,
size: 30,
),
"Home",
onTap: () {},
).paddingBottom(AppSpace.listRow),

// iconTextOutlined, // 图标/文字/边框
ButtonWidget.iconTextOutlined(
IconWidget.svg(
AssetsSvgs.cHomeSvg,
size: 30,
),
"Home",
onTap: () {},
)
.tight(
width: 100,
height: 50,
)
.paddingBottom(AppSpace.listRow),

// iconTextUpDownOutlined, // 图标/文字/上下/边框
ButtonWidget.iconTextUpDownOutlined(
IconWidget.svg(
AssetsSvgs.cHomeSvg,
size: 30,
),
"Home",
onTap: () {},
)
.tight(
width: 100,
height: 50,
)
.paddingBottom(AppSpace.listRow),

// textIcon, // 文字/图标
ButtonWidget.textIcon(
"Home",
IconWidget.svg(
AssetsSvgs.cHomeSvg,
size: 30,
),
onTap: () {},
).paddingBottom(AppSpace.listRow),

//
].toColumn();
}

最后:运行

亮色

img

暗色

img

提交代码到 git