3.7 输入组件封装

3.7 输入组件封装

实现步骤:


第 1 步:规划组件功能

image-20220711145605487

依据设计稿,我们定义如下

类型 说明
text 文本
textBorder 文本/边框
textFilled 文本/填充/边框
iconTextFilled 图标/文本/填充/边框
suffixTextFilled 后缀图标/文本/填充/边框
search 搜索

第 2 步:编写文本输入组件

lib/common/widgets/input.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
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../index.dart';

enum InputWidgetType {
none,
text, // 文字
textBorder, // 边框
textFilled, // 填充/边框
iconTextFilled, // 图标/文本/填充/边框
suffixTextFilled, // 后缀图标/文本/填充/边框
search, // 搜索
}

/// 输入框
class InputWidget extends StatelessWidget {
/// 输入框类型
final InputWidgetType type;

/// 事件 - 提交
final Function(String)? onSubmitted;

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

/// 事件 - change
final Function(String)? onChanged;

/// 输入控制器
final TextEditingController? controller;

/// 焦点
final FocusNode? focusNode;

/// 输入框提示文字
final String? hintText;

/// 键盘类型
final TextInputType? keyboardType;

/// 圆角
final double? borderRadius;

/// 密码
final bool isObscureText;

/// 只读
final bool readOnly;

/// 最大行数
final int? maxLines;

/// 最小行数
final int? minLines;

/// 字体
final double? fontSize;

/// 图标
final Widget? icon;
final Widget? suffixIcon;

/// 输入框确认操作方式
final TextInputAction? textInputAction;

/// 输入验证
final List<TextInputFormatter>? inputFormatters;

/// 填充颜色
final Color? fillColor;

/// 边框颜色
final Color? borderColor;

/// 内容 padding
final EdgeInsetsGeometry? contentPadding;

const InputWidget({
Key? key,
this.type = InputWidgetType.none,
this.onSubmitted,
this.controller,
this.focusNode,
this.hintText,
this.keyboardType,
this.isObscureText = false,
this.icon,
this.onTap,
this.readOnly = false,
this.maxLines,
this.minLines,
this.textInputAction,
this.fontSize,
this.inputFormatters,
this.fillColor,
this.suffixIcon,
this.contentPadding,
this.borderColor,
this.borderRadius,
this.onChanged,
}) : super(key: key);

/// 文本输入
const InputWidget.text({
Key? key,
this.type = InputWidgetType.text,
this.controller,
this.onSubmitted,
this.focusNode,
this.hintText,
this.keyboardType,
this.isObscureText = false,
this.icon,
this.onTap,
this.readOnly = false,
this.maxLines = 1,
this.minLines = 1,
this.textInputAction,
this.fontSize,
this.inputFormatters,
this.fillColor,
this.suffixIcon,
this.contentPadding,
this.borderColor,
this.borderRadius,
this.onChanged,
}) : super(key: key);

/// 文本输入 - 边框
const InputWidget.textBorder({
Key? key,
this.type = InputWidgetType.textBorder,
this.onSubmitted,
this.focusNode,
this.hintText,
this.keyboardType,
this.isObscureText = false,
this.icon,
this.onTap,
this.readOnly = false,
this.maxLines = 1,
this.minLines = 1,
this.textInputAction,
this.fontSize,
this.inputFormatters,
this.fillColor,
this.suffixIcon,
this.contentPadding,
this.borderColor,
this.borderRadius,
this.controller,
this.onChanged,
}) : super(key: key);

/// 文本输入 - 填充
InputWidget.textFilled({
Key? key,
this.type = InputWidgetType.textFilled,
Color? fillColor, // 输入颜色
this.onSubmitted,
this.focusNode,
this.hintText,
this.keyboardType,
this.isObscureText = false,
this.icon,
this.onTap,
this.readOnly = false,
this.maxLines = 1,
this.minLines = 1,
this.textInputAction,
this.fontSize,
this.inputFormatters,
this.suffixIcon,
this.contentPadding,
this.borderColor,
this.borderRadius,
this.onChanged,
this.controller,
}) : fillColor = fillColor ?? AppColors.surface.withOpacity(0.5),
super(key: key);

/// 文本输入 - 图标文本填充
InputWidget.iconTextFilled(
this.icon, {
Key? key,
this.type = InputWidgetType.iconTextFilled,
Color? fillColor, // 输入颜色
this.onSubmitted,
this.focusNode,
this.hintText,
this.keyboardType,
this.isObscureText = false,
this.onTap,
this.readOnly = false,
this.maxLines = 1,
this.minLines = 1,
this.textInputAction,
this.fontSize,
this.inputFormatters,
this.suffixIcon,
this.contentPadding,
this.borderColor,
this.borderRadius,
this.onChanged,
this.controller,
}) : fillColor = fillColor ?? AppColors.surface.withOpacity(0.5),
super(key: key);

/// 文本输入 - 后缀图标文本填充
InputWidget.suffixTextFilled(
this.suffixIcon, {
Key? key,
this.type = InputWidgetType.suffixTextFilled,
Color? fillColor, // 输入颜色
this.icon,
this.onSubmitted,
this.focusNode,
this.hintText,
this.keyboardType,
this.isObscureText = false,
this.onTap,
this.readOnly = false,
this.maxLines = 1,
this.minLines = 1,
this.textInputAction,
this.fontSize,
this.inputFormatters,
this.contentPadding,
this.borderColor,
this.borderRadius,
this.onChanged,
this.controller,
}) : fillColor = fillColor ?? AppColors.surface.withOpacity(0.5),
super(key: key);

/// 搜索
InputWidget.search({
Key? key,
this.type = InputWidgetType.search,
Color? fillColor, // 输入颜色
Widget? icon,
this.suffixIcon,
this.onSubmitted,
this.focusNode,
this.hintText,
this.keyboardType,
this.isObscureText = false,
this.onTap,
this.readOnly = false,
this.maxLines = 1,
this.minLines = 1,
this.textInputAction,
this.fontSize,
this.inputFormatters,
this.contentPadding,
this.borderColor,
this.borderRadius = 11,
this.onChanged,
this.controller,
}) : icon = icon ??
IconWidget.icon(
Icons.search,
color: AppColors.outline,
),
fillColor = fillColor ?? AppColors.surface.withOpacity(0.5),
super(key: key);

// 边框
InputBorder? get _border {
switch (type) {
case InputWidgetType.none:
case InputWidgetType.text:
return InputBorder.none;
default:
return OutlineInputBorder(
borderSide:
BorderSide(color: borderColor ?? AppColors.surfaceVariant),
borderRadius: BorderRadius.all(
Radius.circular(borderRadius ?? AppRadius.input)),
);
}
}

// 尾部图标
Widget? get _suffixIcon {
switch (type) {
case InputWidgetType.search:
return <Widget>[
Container(
width: 1,
height: 16,
color: AppColors.surfaceVariant,
).paddingRight(AppSpace.iconTextSmail),
suffixIcon ??
IconWidget.icon(
Icons.photo_camera_outlined,
color: AppColors.outline,
)
].toRow().width(30).paddingRight(5);
default:
return suffixIcon;
}
}

@override
Widget build(BuildContext context) {
var inputBorder = _border;
return TextField(
onTap: onTap,
readOnly: readOnly,
autocorrect: false,
obscureText: isObscureText,
controller: controller,
focusNode: focusNode,
maxLines: maxLines,
minLines: minLines,
onSubmitted: onSubmitted,
onChanged: onChanged,
textInputAction: textInputAction,
style: AppTextStyles.bodyText1?.copyWith(
fontSize: fontSize,
fontWeight: FontWeight.w500,
color: AppColors.secondary,
),
keyboardType: keyboardType,
inputFormatters: inputFormatters,
decoration: InputDecoration(
fillColor: fillColor ?? Colors.transparent,
prefixIcon: icon,
prefixIconConstraints: const BoxConstraints(
minWidth: 30,
minHeight: 0,
),
suffixIcon: _suffixIcon,
suffixIconConstraints: const BoxConstraints(
minWidth: 30,
minHeight: 0,
),
hintText: hintText,
hintStyle: AppTextStyles.bodyText2?.copyWith(
fontWeight: FontWeight.w300,
color: AppColors.secondary.withOpacity(0.5),
),
contentPadding: contentPadding ?? AppSpace.edgeInput,
isCollapsed: true,
isDense: true,
filled: true,
border: inputBorder,
enabledBorder: inputBorder,
focusedBorder: inputBorder,
errorBorder: inputBorder,
focusedErrorBorder: inputBorder,
disabledBorder: inputBorder,
),
);
}
}

第 3 步:调试组件

lib/pages/styles/inputs/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
// 主视图
Widget _buildView() {
return SingleChildScrollView(
child: _buildInputs().center(),
);
}

Widget _buildInputs() {
return <Widget>[
/// 文本
const InputWidget.text(
hintText: "文本",
).width(300).paddingBottom(AppSpace.listRow),

/// 文本/边框
const InputWidget.textBorder(
hintText: "文本/边框",
).width(300).paddingBottom(AppSpace.listRow),

/// 文本/填充/边框
InputWidget.textFilled(
hintText: "文本/填充/边框",
).width(300).paddingBottom(AppSpace.listRow),

/// 图标/文本/填充/边框
InputWidget.iconTextFilled(
IconWidget.svg(
AssetsSvgs.cHomeSvg,
),
hintText: "图标/文本/填充/边框",
).width(300).paddingBottom(AppSpace.listRow),

/// 后缀图标/文本/填充/边框
InputWidget.suffixTextFilled(
IconWidget.svg(
AssetsSvgs.cHomeSvg,
),
hintText: "后缀图标/文本/填充/边框",
).width(300).paddingBottom(AppSpace.listRow),

/// 搜索
InputWidget.search(
hintText: "搜索",
).width(300).paddingBottom(AppSpace.listRow),

// end
].toColumn();
}

lib/pages/styles/styles_index/view.dart

1
2
3
4
5
// Input 输入框
ListTile(
onTap: () => Get.toNamed(RouteNames.stylesInputs),
title: const TextWidget.body1("Input 输入框"),
),

最后:运行

img

提交代码到 git