3.2 文字组件封装

3.2 文字组件封装

文本组件封装,实现步骤:


第 1 步:规划组件定义

设计稿样式标准

img

根据设计稿,定义如下

name fontSize fontWeight
title1 24 bold
title2 20 500
title3 15 500
body1 15 500
body2 12 400
body3 9 300

一共 6 种情况,如果需要可以再加,有这个规律,title 主要用在标题偏重,body 用在内容区。

第 2 步:TextWidget 封装

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

/// 文字组件
class TextWidget extends StatelessWidget {
/// 文字字符串
final String text;

/// 样式
final TextStyle? style;

/// 颜色
final Color? color;

/// 大小
final double? size;

/// 重量
final FontWeight? weight;

/// 行数
final int? maxLines;

/// 自动换行
final bool? softWrap;

/// 溢出
final TextOverflow? overflow;

/// 对齐方式
final TextAlign? textAlign;
1
2
3
4
5
6
7
8
9
10
11
12
const TextWidget({
Key? key,
required this.text,
this.style,
this.maxLines = 1,
this.softWrap = false,
this.overflow = TextOverflow.clip,
this.color,
this.size,
this.weight,
this.textAlign,
}) : super(key: key);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@override
Widget build(BuildContext context) {
if (text == "") {
return const SizedBox();
}
return Text(
text,
style: style?.copyWith(
color: color,
fontSize: size,
fontWeight: weight,
) ??
TextStyle(
color: color,
fontSize: size,
fontWeight: weight,
),
overflow: overflow,
maxLines: maxLines,
softWrap: softWrap,
textAlign: textAlign,
);
}

文字 - 标题 1, 命名构造函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/// 文字 - 标题1
const TextWidget.title1(
this.text, {
Key? key,
this.maxLines = 1,
this.softWrap = false,
this.overflow = TextOverflow.clip,
this.color,
this.size,
this.weight,
this.textAlign,
}) : style = const TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
super(key: key);

其它 title body 按上面方式重复

完整代码

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

/// 文字组件
class TextWidget extends StatelessWidget {
/// 文字字符串
final String text;

/// 样式
final TextStyle? style;

/// 颜色
final Color? color;

/// 大小
final double? size;

/// 重量
final FontWeight? weight;

/// 行数
final int? maxLines;

/// 自动换行
final bool? softWrap;

/// 溢出
final TextOverflow? overflow;

/// 对齐方式
final TextAlign? textAlign;

const TextWidget({
Key? key,
required this.text,
this.style,
this.maxLines = 1,
this.softWrap = false,
this.overflow = TextOverflow.clip,
this.color,
this.size,
this.weight,
this.textAlign,
}) : super(key: key);

/// 文字 - 标题1
const TextWidget.title1(
this.text, {
Key? key,
this.maxLines = 1,
this.softWrap = false,
this.overflow = TextOverflow.clip,
this.color,
this.size,
this.weight,
this.textAlign,
}) : style = const TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
super(key: key);

/// 文字 - 标题2
const TextWidget.title2(
this.text, {
Key? key,
this.maxLines = 1,
this.softWrap = false,
this.overflow = TextOverflow.clip,
this.color,
this.size,
this.weight,
this.textAlign,
}) : style = const TextStyle(
fontSize: 20,
fontWeight: FontWeight.w500,
),
super(key: key);

/// 文字 - 标题3
const TextWidget.title3(
this.text, {
Key? key,
this.maxLines = 1,
this.softWrap = false,
this.overflow = TextOverflow.clip,
this.color,
this.size,
this.weight,
this.textAlign,
}) : style = const TextStyle(
fontSize: 15,
fontWeight: FontWeight.w500,
),
super(key: key);

/// 文字 - 正文1
const TextWidget.body1(
this.text, {
Key? key,
this.maxLines = 1,
this.softWrap = false,
this.overflow = TextOverflow.clip,
this.color,
this.size,
this.weight,
this.textAlign,
}) : style = const TextStyle(
fontSize: 15,
fontWeight: FontWeight.w500,
),
super(key: key);

/// 文字 - 正文2
const TextWidget.body2(
this.text, {
Key? key,
this.maxLines = 1,
this.softWrap = false,
this.overflow = TextOverflow.clip,
this.color,
this.size,
this.weight,
this.textAlign,
}) : style = const TextStyle(
fontSize: 12,
fontWeight: FontWeight.w400,
),
super(key: key);

/// 文字 - 正文3
const TextWidget.body3(
this.text, {
Key? key,
this.maxLines = 1,
this.softWrap = false,
this.overflow = TextOverflow.clip,
this.color,
this.size,
this.weight,
this.textAlign,
}) : style = const TextStyle(
fontSize: 9,
fontWeight: FontWeight.w300,
),
super(key: key);

/// 文字 - 按钮
TextWidget.button({
Key? key,
required this.text,
this.maxLines = 1,
this.softWrap = false,
this.overflow = TextOverflow.clip,
Color? color,
this.size,
this.weight,
this.textAlign,
}) : color = color ?? AppColors.secondary,
style = TextStyle(
fontSize: 14,
fontWeight: FontWeight.w500,
color: color,
),
super(key: key);

/// 文字 - 导航
const TextWidget.navigation({
Key? key,
required this.text,
this.maxLines = 1,
this.softWrap = false,
this.overflow = TextOverflow.clip,
this.color,
this.size,
this.weight,
this.textAlign,
}) : style = const TextStyle(
fontSize: 24,
fontWeight: FontWeight.w700,
),
super(key: key);

@override
Widget build(BuildContext context) {
if (text == "") {
return const SizedBox();
}
return Text(
text,
style: style?.copyWith(
color: color,
fontSize: size,
fontWeight: weight,
) ??
TextStyle(
color: color,
fontSize: size,
fontWeight: weight,
),
overflow: overflow,
maxLines: maxLines,
softWrap: softWrap,
textAlign: textAlign,
);
}
}

第 3 步:text 调试界面

lib/pages/styles/styles_index/view.dart

1
2
3
4
5
6
7
8
Widget _buildView() {
return ListView(
children: [
// 文本
ListTile(
onTap: () => Get.toNamed(RouteNames.stylesText),
title: const Text("Text 文本"),
),

lib/pages/styles/text/view.dart

1
2
3
4
5
6
7
8
9
10
11
12
Widget _buildView() {
return ListView(
children: const [
ListTile(title: TextWidget.title1("title1")),
ListTile(title: TextWidget.title2("title2")),
ListTile(title: TextWidget.title3("title3")),
ListTile(title: TextWidget.body1("body1")),
ListTile(title: TextWidget.body2("body2")),
ListTile(title: TextWidget.body3("body3")),
],
);
}

最后:运行

img

提交代码到 git