-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathflutter_flow_choice_chips.dart
More file actions
130 lines (121 loc) · 4.11 KB
/
flutter_flow_choice_chips.dart
File metadata and controls
130 lines (121 loc) · 4.11 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
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
const double _kChoiceChipsHeight = 40.0;
class ChipData {
const ChipData(this.label, [this.value = "", this.iconData]);
final String label;
final String value;
final IconData? iconData;
}
class ChipStyle {
const ChipStyle(
{required this.backgroundColor,
required this.textStyle,
required this.iconColor,
required this.iconSize,
this.labelPadding,
required this.elevation});
final Color backgroundColor;
final TextStyle textStyle;
final Color iconColor;
final double iconSize;
final EdgeInsetsGeometry? labelPadding;
final double elevation;
}
class FlutterFlowChoiceChips extends StatefulWidget {
const FlutterFlowChoiceChips({
this.initiallySelected,
required this.options,
required this.onChanged,
required this.selectedChipStyle,
required this.unselectedChipStyle,
required this.chipSpacing,
this.rowSpacing = 0.0,
required this.multiselect,
this.initialized = true,
this.alignment = WrapAlignment.start,
});
final List<String>? initiallySelected;
final List<ChipData> options;
final void Function(List<String>?) onChanged;
final ChipStyle selectedChipStyle;
final ChipStyle unselectedChipStyle;
final double chipSpacing;
final double rowSpacing;
final bool multiselect;
final bool initialized;
final WrapAlignment alignment;
@override
State<FlutterFlowChoiceChips> createState() => _FlutterFlowChoiceChipsState();
}
class _FlutterFlowChoiceChipsState extends State<FlutterFlowChoiceChips> {
late List<String> choiceChipValues;
@override
void initState() {
super.initState();
choiceChipValues = widget.initiallySelected ?? [];
if (!widget.initialized && choiceChipValues.isNotEmpty) {
SchedulerBinding.instance!.addPostFrameCallback(
(_) => widget.onChanged(choiceChipValues),
);
}
}
@override
Widget build(BuildContext context) => Wrap(
spacing: widget.chipSpacing,
runSpacing: widget.rowSpacing,
alignment: widget.alignment,
children: [
...widget.options.map(
(option) {
final String value = option.value.isNotEmpty ? option.value : option.label;
final selected = choiceChipValues.contains(value);
final style = selected
? widget.selectedChipStyle
: widget.unselectedChipStyle;
return Container(
height: _kChoiceChipsHeight,
child: ChoiceChip(
selected: selected,
onSelected: (isSelected) {
if (isSelected) {
widget.multiselect
? choiceChipValues.add(value)
: choiceChipValues = [value];
widget.onChanged(choiceChipValues);
setState(() {});
} else {
if (widget.multiselect) {
choiceChipValues.remove(value);
widget.onChanged(choiceChipValues);
setState(() {});
}
}
},
label: Text(
option.label,
style: style.textStyle,
),
labelPadding: style.labelPadding,
avatar: option.iconData != null
? FaIcon(
option.iconData,
size: style.iconSize,
color: style.iconColor,
)
: null,
elevation: style.elevation,
selectedColor: selected
? widget.selectedChipStyle.backgroundColor
: null,
backgroundColor: selected
? null
: widget.unselectedChipStyle.backgroundColor,
),
);
},
).toList(),
],
);
}