forked from Tencent/tdesign-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtd_checkbox_page.dart
More file actions
481 lines (461 loc) · 13.9 KB
/
td_checkbox_page.dart
File metadata and controls
481 lines (461 loc) · 13.9 KB
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
import 'package:flutter/material.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import '../../base/example_widget.dart';
import '../annotation/demo.dart';
/// TDCheckbox演示
class TDCheckboxPage extends StatefulWidget {
const TDCheckboxPage({Key? key}) : super(key: key);
@override
State<StatefulWidget> createState() {
return TDCheckboxPageState();
}
}
class TDCheckboxPageState extends State<TDCheckboxPage> {
List<String>? checkIds = [
'index:1',
'index:2',
'index:3',
];
TDCheckboxGroupController? controller;
@override
void initState() {
super.initState();
controller = TDCheckboxGroupController();
}
@override
Widget build(BuildContext context) {
return ExamplePage(
title: tdTitle(),
desc: '用于预设的一组选项中执行多项选择,并呈现选择结果。',
exampleCodeGroup: 'checkbox',
children: [
ExampleModule(title: '组件类型', children: [
ExampleItem(desc: '纵向多选框', builder: _verticalCheckbox),
ExampleItem(desc: '横向多选框', builder: _horizontalCheckbox),
ExampleItem(desc: '带全选多选框', builder: _checkAllSelected)
]),
ExampleModule(title: '组件状态', children: [
ExampleItem(desc: '多选框状态', builder: _checkboxStatus),
]),
ExampleModule(title: '组件样式', children: [
ExampleItem(desc: '勾选样式', builder: _checkStyle),
ExampleItem(desc: '勾选显示位置', builder: _checkPosition),
ExampleItem(desc: '非通栏多选样式', builder: _passThroughStyle),
]),
ExampleModule(title: '特殊样式', children: [
ExampleItem(desc: '纵向卡片单选框', builder: _verticalCardStyle),
ExampleItem(desc: '横向卡片单选框', builder: _horizontalCardStyle),
]),
],
test: [
ExampleItem(desc: '自定义Icon', builder: _customIconBuildStyle),
ExampleItem(desc: '自定义颜色', builder: _customColor),
ExampleItem(desc: '自定义字体尺寸', builder: _customFont),
],
);
}
@Demo(group: 'checkbox')
Widget _verticalCheckbox(BuildContext context) {
return TDCheckboxGroupContainer(
selectIds: const ['index:1'],
child: ListView.builder(
padding: EdgeInsets.zero,
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemBuilder: (BuildContext context, int index) {
var title = '多选';
var subTitle = '';
if (index == 2) {
title = '多选标题多行多选标题多行多选标题多行多选标题多行多选标题多行多选标题多行';
}
if (index == 3) {
subTitle = '描述信息描述信息描述信息描述信息描述信息描述信息描述信息描述信息描述信息';
}
return TDCheckbox(
id: 'index:$index',
title: title,
titleMaxLine: 2,
subTitleMaxLine: 2,
subTitle: subTitle,
);
},
itemCount: 4,
),
);
}
@Demo(group: 'checkbox')
Widget _horizontalCheckbox(BuildContext context) {
return TDCheckboxGroupContainer(
selectIds: const ['1'],
direction: Axis.horizontal,
directionalTdCheckboxes: const [
TDCheckbox(
id: '0',
title: '多选标题',
style: TDCheckboxStyle.circle,
insetSpacing: 12,
showDivider: false,
),
TDCheckbox(
id: '1',
title: '多选标题',
style: TDCheckboxStyle.circle,
insetSpacing: 12,
showDivider: false,
),
TDCheckbox(
id: '2',
title: '上限四字',
style: TDCheckboxStyle.circle,
insetSpacing: 12,
showDivider: false,
),
],
);
}
@Demo(group: 'checkbox')
Widget _checkAllSelected(BuildContext context) {
const itemCount = 4;
return TDCheckboxGroupContainer(
selectIds: checkIds,
passThrough: false,
controller: controller,
child: ListView.builder(
padding: const EdgeInsets.all(0),
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemBuilder: (context, index) {
var title = '多选';
if (index == 0) {
title = '全选';
return SizedBox(
height: 56,
child: TDCheckbox(
id: 'index:$index',
title: title,
customIconBuilder: (context, checked) {
var length = controller!.allChecked().length -
(controller!.checked('index:0') ? 1 : 0);
var allCheck = itemCount - 1 == length;
var halfSelected =
controller != null && !allCheck && length > 0;
return getAllIcon(allCheck, halfSelected);
},
onCheckBoxChanged: (checked) {
if (checked) {
controller?.toggleAll(true);
} else {
controller?.toggleAll(false);
}
},
),
);
} else {
return SizedBox(
height: index == itemCount - 1 ? null : 56,
child: TDCheckbox(
id: 'index:$index',
title: title,
subTitle: index == itemCount - 1
? '描述信息描述信息描述信息描述信息描述信息描述信息描述信息描述信息描述信息'
: null,
subTitleMaxLine: 2,
onCheckBoxChanged: (checked) {
var length = controller!.allChecked().length -
(controller!.checked('index:0') ? 1 : 0);
var allCheck = itemCount - 1 == length;
var halfSelected =
controller != null && !allCheck && length > 0;
controller!.toggle('index:0', allCheck);
getAllIcon(allCheck, halfSelected);
},
),
);
}
},
itemCount: itemCount,
),
);
}
@Demo(group: 'checkbox')
Widget _checkboxStatus(BuildContext context) {
return TDCheckboxGroupContainer(
contentDirection: TDContentDirection.right,
selectIds: const ['0'],
child: const Column(
children: [
TDCheckbox(
id: '0',
title: '选项禁用-已选',
style: TDCheckboxStyle.circle,
enable: false,
),
TDCheckbox(
id: '1',
title: '选项禁用-默认',
style: TDCheckboxStyle.circle,
enable: false,
),
],
),
);
}
@Demo(group: 'checkbox')
Widget _checkStyle(BuildContext context) {
return Column(
children: [
TDCheckboxGroupContainer(
style: TDCheckboxStyle.check,
selectIds: const ['index:0'],
child: const TDCheckbox(
id: 'index:0',
title: '多选',
),
),
const SizedBox(
height: 17,
),
TDCheckboxGroupContainer(
style: TDCheckboxStyle.square,
selectIds: const ['index:0'],
child: const TDCheckbox(
id: 'index:0',
title: '多选',
),
)
],
);
}
@Demo(group: 'checkbox')
Widget _checkPosition(BuildContext context) {
return Column(
children: [
TDCheckboxGroupContainer(
contentDirection: TDContentDirection.right,
selectIds: const ['index:0'],
child: const TDCheckbox(
id: 'index:0',
title: '多选',
),
),
TDCheckboxGroupContainer(
contentDirection: TDContentDirection.left,
selectIds: const ['index:0'],
child: const TDCheckbox(
id: 'index:0',
title: '多选',
),
)
],
);
}
@Demo(group: 'checkbox')
Widget _passThroughStyle(BuildContext context) {
return TDCheckboxGroupContainer(
selectIds: const ['index:0'],
passThrough: true,
child: ListView.builder(
padding: const EdgeInsets.all(0),
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemBuilder: (context, index) {
var title = '多选';
return TDCheckbox(
id: 'index:$index',
title: title,
size: TDCheckBoxSize.large,
);
},
itemCount: 4,
),
);
}
@Demo(group: 'checkbox')
Widget _verticalCardStyle(BuildContext context) {
return TDCheckboxGroupContainer(
selectIds: const ['index:1'],
cardMode: true,
direction: Axis.vertical,
directionalTdCheckboxes: const [
TDCheckbox(
id: 'index:0',
title: '多选',
titleMaxLine: 2,
subTitleMaxLine: 2,
subTitle: '描述信息',
cardMode: true,
),
TDCheckbox(
id: 'index:1',
title: '多选',
titleMaxLine: 2,
subTitleMaxLine: 2,
subTitle: '描述信息',
cardMode: true,
),
TDCheckbox(
id: 'index:2',
title: '多选',
titleMaxLine: 2,
subTitleMaxLine: 2,
subTitle: '描述信息',
cardMode: true,
),
TDCheckbox(
id: 'index:3',
title: '多选',
titleMaxLine: 2,
subTitleMaxLine: 2,
subTitle: '描述信息',
cardMode: true,
),
],
);
}
@Demo(group: 'checkbox')
Widget _horizontalCardStyle(BuildContext context) {
return TDCheckboxGroupContainer(
selectIds: const ['index:1'],
cardMode: true,
direction: Axis.horizontal,
directionalTdCheckboxes: const [
TDCheckbox(
id: 'index:0',
title: '多选',
cardMode: true,
),
TDCheckbox(
id: 'index:1',
title: '多选',
cardMode: true,
),
TDCheckbox(
id: 'index:2',
title: '多选',
cardMode: true,
),
],
);
}
@Demo(group: 'checkbox')
Widget _customIconBuildStyle(BuildContext context) {
return TDCheckboxGroupContainer(
selectIds: const ['index:1'],
cardMode: true,
direction: Axis.vertical,
directionalTdCheckboxes: [
TDCheckbox(
id: 'index:0',
title: '多选',
subTitle: '描述信息',
titleMaxLine: 2,
subTitleMaxLine: 2,
cardMode: true,
customIconBuilder: (context, checked) {
return const Icon(
TDIcons.app,
size: 12,
);
},
),
],
);
}
@Demo(group: 'checkbox')
Widget _customColor(BuildContext context) {
return TDCheckboxGroupContainer(
contentDirection: TDContentDirection.right,
selectIds: const ['0'],
child: Column(
children: [
TDCheckbox(
selectColor: TDTheme.of(context).errorColor3,
disableColor: TDTheme.of(context).errorColor1,
id: '0',
title: '选项禁用-已选',
style: TDCheckboxStyle.circle,
enable: false,
),
TDCheckbox(
selectColor: TDTheme.of(context).errorColor3,
disableColor: TDTheme.of(context).errorColor1,
id: '1',
title: '选项禁用-默认',
style: TDCheckboxStyle.circle,
),
TDCheckbox(
selectColor: TDTheme.of(context).errorColor3,
disableColor: TDTheme.of(context).errorColor1,
id: 'index:0',
title: '多选',
subTitle: '描述信息',
titleMaxLine: 2,
subTitleMaxLine: 2,
cardMode: true,
),
TDCheckbox(
selectColor: TDTheme.of(context).errorColor3,
id: 'index:1',
title: '多选',
titleColor: Colors.green,
subTitle: '描述信息',
subTitleColor: Colors.blue,
titleMaxLine: 2,
subTitleMaxLine: 2,
cardMode: true,
),
],
),
);
}
@Demo(group: 'checkbox')
Widget _customFont(BuildContext context) {
return TDCheckboxGroupContainer(
contentDirection: TDContentDirection.right,
selectIds: const ['0'],
child: Column(
children: [
TDCheckbox(
id: '0',
title: '选项禁用-已选',
subTitle: '描述文本',
style: TDCheckboxStyle.circle,
enable: false,
titleFont: TDTheme.of(context).fontBodySmall,
subTitleFont: TDTheme.of(context).fontBodyExtraSmall,
),
TDCheckbox(
id: '1',
title: '选项禁用-默认',
subTitle: '描述文本',
style: TDCheckboxStyle.circle,
titleFont: TDTheme.of(context).fontBodySmall,
subTitleFont: TDTheme.of(context).fontBodyExtraSmall,
),
TDCheckbox(
id: 'index:0',
title: '多选',
subTitle: '描述信息',
titleMaxLine: 2,
subTitleMaxLine: 2,
cardMode: true,
titleFont: TDTheme.of(context).fontBodySmall,
subTitleFont: TDTheme.of(context).fontBodyExtraSmall,
),
],
),
);
}
Widget getAllIcon(bool checked, bool halfSelected) {
return Icon(
checked
? TDIcons.check_circle_filled
: halfSelected
? TDIcons.minus_circle_filled
: TDIcons.circle,
size: 24,
color: (checked || halfSelected)
? TDTheme.of(context).brandNormalColor
: TDTheme.of(context).grayColor4);
}
}