@@ -8,14 +8,38 @@ import 'package:ensemble/framework/action.dart' as framework;
8
8
import 'package:ensemble_ts_interpreter/invokables/invokable.dart' ;
9
9
import 'package:flutter/material.dart' ;
10
10
11
- class EnsembleTripleStateSwitch extends SwitchBase {
12
- static const type = 'TripleStateSwitch' ;
13
- EnsembleTripleStateSwitch ({super .key});
14
- }
15
-
11
+ /// A toggle switch (true/false) states
16
12
class EnsembleSwitch extends SwitchBase {
17
13
static const type = 'Switch' ;
18
14
EnsembleSwitch ({super .key});
15
+
16
+ @override
17
+ Map <String , Function > setters () => Map <String , Function >.from (super .setters ())
18
+ ..addAll ({
19
+ 'value' : (value) =>
20
+ _controller.value = Utils .getBool (value, fallback: false ),
21
+ });
22
+
23
+ void onToggle (bool newValue) {
24
+ setProperty ('value' , newValue);
25
+ }
26
+ }
27
+
28
+ /// A triple state switch (off/mixed/on) states
29
+ class EnsembleTripleSwitch extends SwitchBase {
30
+ static const type = 'TripleSwitch' ;
31
+ EnsembleTripleSwitch ({super .key});
32
+
33
+ @override
34
+ Map <String , Function > setters () => Map <String , Function >.from (super .setters ())
35
+ ..addAll ({
36
+ 'value' : (value) => _controller.value =
37
+ SwitchState .values.from (value)? .name ?? SwitchState .off.name
38
+ });
39
+
40
+ void onToggle (SwitchState newValue) {
41
+ setProperty ('value' , newValue.name);
42
+ }
19
43
}
20
44
21
45
abstract class SwitchBase extends StatefulWidget
@@ -50,8 +74,7 @@ abstract class SwitchBase extends StatefulWidget
50
74
'activeColor' : (color) => _controller.activeColor = Utils .getColor (color),
51
75
'inactiveColor' : (color) =>
52
76
_controller.inactiveColor = Utils .getColor (color),
53
- 'intermediateColor' : (color) =>
54
- _controller.intermediateColor = Utils .getColor (color),
77
+ 'mixedColor' : (color) => _controller.mixedColor = Utils .getColor (color),
55
78
'activeThumbColor' : (color) =>
56
79
_controller.activeThumbColor = Utils .getColor (color),
57
80
'inactiveThumbColor' : (color) =>
@@ -60,28 +83,9 @@ abstract class SwitchBase extends StatefulWidget
60
83
framework.EnsembleAction .fromYaml (definition, initiator: this )
61
84
};
62
85
}
63
-
64
- void onToggle (dynamic newValue) {
65
- setProperty ('value' , newValue);
66
- }
67
86
}
68
87
69
88
class SwitchBaseState extends FormFieldWidgetState <SwitchBase > {
70
- @override
71
- void initState () {
72
- super .initState ();
73
- setSwitchValue (widget._controller.value);
74
- }
75
-
76
- void setSwitchValue (dynamic value) {
77
- if (widget is EnsembleSwitch ) {
78
- widget._controller.value = Utils .getBool (value, fallback: false );
79
- } else if (widget is EnsembleTripleStateSwitch ) {
80
- widget._controller.value =
81
- SwitchState .values.from (value)? .name ?? SwitchState .off.name;
82
- }
83
- }
84
-
85
89
@override
86
90
Widget buildWidget (BuildContext context) {
87
91
// add leading/trailing text + the actual widget
@@ -94,7 +98,7 @@ class SwitchBaseState extends FormFieldWidgetState<SwitchBase> {
94
98
)));
95
99
}
96
100
97
- children.add (widget is EnsembleSwitch ? switchWidget : tripleStateSwitch );
101
+ children.add (widget is EnsembleSwitch ? switchWidget : tripleSwitch );
98
102
99
103
if (widget._controller.trailingText != null ) {
100
104
children.add (Expanded (
@@ -108,7 +112,7 @@ class SwitchBaseState extends FormFieldWidgetState<SwitchBase> {
108
112
return InputWrapper (
109
113
type: (widget is EnsembleSwitch )
110
114
? EnsembleSwitch .type
111
- : EnsembleTripleStateSwitch .type,
115
+ : EnsembleTripleSwitch .type,
112
116
controller: widget._controller,
113
117
widget: FormField <bool >(
114
118
key: validatorKey,
@@ -117,10 +121,9 @@ class SwitchBaseState extends FormFieldWidgetState<SwitchBase> {
117
121
widget._controller.required &&
118
122
! widget._controller.value;
119
123
120
- final tripleStateRequiredStatus =
121
- widget is EnsembleTripleStateSwitch &&
122
- widget._controller.required &&
123
- widget._controller.value == SwitchState .off.name;
124
+ final tripleStateRequiredStatus = widget is EnsembleTripleSwitch &&
125
+ widget._controller.required &&
126
+ widget._controller.value == SwitchState .off.name;
124
127
125
128
if (switchRequiredStatus || tripleStateRequiredStatus) {
126
129
return Utils .translateWithFallback (
@@ -146,34 +149,23 @@ class SwitchBaseState extends FormFieldWidgetState<SwitchBase> {
146
149
);
147
150
}
148
151
149
- void onToggle (dynamic newValue) {
150
- if (newValue is bool ) {
151
- widget.onToggle (newValue);
152
- widget._controller.value = newValue;
153
- } else if (newValue is String ) {
154
- final SwitchState switchState =
155
- SwitchState .values.from (newValue) ?? widget._controller.value;
156
- widget.onToggle (switchState.name);
157
- widget._controller.value = switchState.name;
158
- }
159
- if (widget._controller.onChange != null ) {
160
- ScreenController ().executeAction (context, widget._controller.onChange! ,
161
- event: EnsembleEvent (widget));
162
- }
163
- }
164
-
165
- Widget get tripleStateSwitch {
152
+ Widget get tripleSwitch {
166
153
final SwitchState switchState =
167
154
SwitchState .values.from (widget._controller.value) ?? SwitchState .off;
168
155
return TripleStateSwitch (
169
156
dotColor: widget._controller.activeThumbColor,
170
157
disableDotColor: widget._controller.inactiveThumbColor,
171
158
startBackgroundColor: widget._controller.activeColor,
172
- middleBackgroundColor: widget._controller.intermediateColor ,
159
+ middleBackgroundColor: widget._controller.mixedColor ,
173
160
endBackgroundColor: widget._controller.inactiveColor,
174
161
disable: widget._controller.enabled == false ,
175
162
state: switchState,
176
- onChanged: isEnabled () ? (value) => onToggle (value.name) : (_) {},
163
+ onChanged: isEnabled ()
164
+ ? (value) {
165
+ (widget as EnsembleTripleSwitch ? )? .onToggle (value);
166
+ onChange ();
167
+ }
168
+ : (_) {},
177
169
);
178
170
}
179
171
@@ -207,7 +199,19 @@ class SwitchBaseState extends FormFieldWidgetState<SwitchBase> {
207
199
trackColor: trackColor,
208
200
thumbColor: thumbColor,
209
201
value: widget._controller.value,
210
- onChanged: isEnabled () ? (value) => onToggle (value) : null );
202
+ onChanged: isEnabled ()
203
+ ? (value) {
204
+ (widget as EnsembleSwitch ? )? .onToggle (value);
205
+ onChange ();
206
+ }
207
+ : null );
208
+ }
209
+
210
+ void onChange () {
211
+ if (widget._controller.onChange != null ) {
212
+ ScreenController ().executeAction (context, widget._controller.onChange! ,
213
+ event: EnsembleEvent (widget));
214
+ }
211
215
}
212
216
}
213
217
@@ -219,7 +223,7 @@ class SwitchBaseController extends FormFieldController {
219
223
Color ? activeThumbColor;
220
224
Color ? inactiveColor;
221
225
Color ? inactiveThumbColor;
222
- Color ? intermediateColor ;
226
+ Color ? mixedColor ;
223
227
224
228
framework.EnsembleAction ? onChange;
225
229
}
0 commit comments