-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeep_checkbox.dart
168 lines (146 loc) · 4.24 KB
/
deep_checkbox.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
/// A checkbox updating itself according to the state of its [child] if it
/// contains another [DeepCheckbox].
///
/// Example:
/// ```dart
/// class CheckboxesPage extends StatelessWidget {
/// const CheckboxesPage({Key? key}) : super(key: key);
///
/// @override
/// Widget build(BuildContext context) {
/// return Scaffold(
/// body: DeepCheckbox(
/// label: 'Level 1',
/// child: Padding(
/// padding: const EdgeInsets.only(left: 20),
/// child: Column(
/// crossAxisAlignment: CrossAxisAlignment.start,
/// mainAxisSize: MainAxisSize.min,
/// children: const [
/// DeepCheckbox(label: 'Level 2-1'),
/// DeepCheckbox(label: 'Level 2-2'),
/// ],
/// ),
/// ),
/// ),
/// );
/// }
/// }
/// ```
class DeepCheckbox extends StatefulWidget {
final String label;
final bool initialValue;
final ValueChanged<bool?>? onChanged;
final Widget? child;
const DeepCheckbox({
Key? key,
required this.label,
this.child,
this.initialValue = false,
this.onChanged,
}) : super(key: key);
static DeepCheckboxState? of(BuildContext context) {
final scope = context.dependOnInheritedWidgetOfExactType<_CheckboxScope>();
return scope?._deepCheckboxState;
}
@override
State<DeepCheckbox> createState() => DeepCheckboxState();
}
class DeepCheckboxState extends State<DeepCheckbox> {
final _checkboxes = <DeepCheckboxState>{};
int _generation = 0;
bool? _selected;
@override
void initState() {
super.initState();
_selected = widget.initialValue;
}
@override
void deactivate() {
DeepCheckbox.of(context)?._unregister(this);
super.deactivate();
}
@override
Widget build(BuildContext context) {
DeepCheckbox.of(context)?._register(this);
final child = widget.child;
return _CheckboxScope(
deepCheckboxState: this,
generation: _generation,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Row(
mainAxisSize: MainAxisSize.min,
children: [
Checkbox(
tristate: true,
value: _selected,
onChanged: _onChanged,
),
Text(widget.label),
],
),
if (child != null) child,
],
),
);
}
void _onChanged(bool? value) {
setState(() => _selected = value ?? false);
// Notify the parent if it exists
DeepCheckbox.of(context)?._fieldDidChange(_selected);
// Update all registered childrens
for (final checkboxState in _checkboxes) {
checkboxState._onChanged(value);
}
}
void _fieldDidChange(bool? value) {
widget.onChanged?.call(value);
final hasSelectedBox =
_checkboxes.any((box) => (box._selected ?? false) == true);
final hasNullBox = _checkboxes.any((box) => box._selected == null);
final hasUnselectedBox =
_checkboxes.any((box) => (box._selected ?? false) == false);
final bool? selected;
if (hasSelectedBox && !hasNullBox && !hasUnselectedBox) {
selected = true;
} else if (!hasSelectedBox && !hasNullBox && hasUnselectedBox) {
selected = false;
} else {
selected = null;
}
setState(() {
_selected = selected;
++_generation;
});
// Notify the parent if it exists.
DeepCheckbox.of(context)?._fieldDidChange(selected);
}
void _register(DeepCheckboxState checkbox) {
_checkboxes.add(checkbox);
}
void _unregister(DeepCheckboxState checkbox) {
_checkboxes.remove(checkbox);
}
}
class _CheckboxScope extends InheritedWidget {
final DeepCheckboxState _deepCheckboxState;
/// Incremented every time a checkbox has changed. This lets us know when
/// to rebuild.
final int _generation;
const _CheckboxScope({
Key? key,
required Widget child,
required DeepCheckboxState deepCheckboxState,
required int generation,
}) : _deepCheckboxState = deepCheckboxState,
_generation = generation,
super(
key: key,
child: child,
);
@override
bool updateShouldNotify(_CheckboxScope old) => _generation != old._generation;
}