Skip to content

Commit 6c772c9

Browse files
committed
refactor: update value, color properties of switch and triple switch widgets
1 parent c64dd5c commit 6c772c9

File tree

2 files changed

+58
-54
lines changed

2 files changed

+58
-54
lines changed

lib/widget/switch.dart

+57-53
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,38 @@ import 'package:ensemble/framework/action.dart' as framework;
88
import 'package:ensemble_ts_interpreter/invokables/invokable.dart';
99
import 'package:flutter/material.dart';
1010

11-
class EnsembleTripleStateSwitch extends SwitchBase {
12-
static const type = 'TripleStateSwitch';
13-
EnsembleTripleStateSwitch({super.key});
14-
}
15-
11+
/// A toggle switch (true/false) states
1612
class EnsembleSwitch extends SwitchBase {
1713
static const type = 'Switch';
1814
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+
}
1943
}
2044

2145
abstract class SwitchBase extends StatefulWidget
@@ -50,8 +74,7 @@ abstract class SwitchBase extends StatefulWidget
5074
'activeColor': (color) => _controller.activeColor = Utils.getColor(color),
5175
'inactiveColor': (color) =>
5276
_controller.inactiveColor = Utils.getColor(color),
53-
'intermediateColor': (color) =>
54-
_controller.intermediateColor = Utils.getColor(color),
77+
'mixedColor': (color) => _controller.mixedColor = Utils.getColor(color),
5578
'activeThumbColor': (color) =>
5679
_controller.activeThumbColor = Utils.getColor(color),
5780
'inactiveThumbColor': (color) =>
@@ -60,28 +83,9 @@ abstract class SwitchBase extends StatefulWidget
6083
framework.EnsembleAction.fromYaml(definition, initiator: this)
6184
};
6285
}
63-
64-
void onToggle(dynamic newValue) {
65-
setProperty('value', newValue);
66-
}
6786
}
6887

6988
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-
8589
@override
8690
Widget buildWidget(BuildContext context) {
8791
// add leading/trailing text + the actual widget
@@ -94,7 +98,7 @@ class SwitchBaseState extends FormFieldWidgetState<SwitchBase> {
9498
)));
9599
}
96100

97-
children.add(widget is EnsembleSwitch ? switchWidget : tripleStateSwitch);
101+
children.add(widget is EnsembleSwitch ? switchWidget : tripleSwitch);
98102

99103
if (widget._controller.trailingText != null) {
100104
children.add(Expanded(
@@ -108,7 +112,7 @@ class SwitchBaseState extends FormFieldWidgetState<SwitchBase> {
108112
return InputWrapper(
109113
type: (widget is EnsembleSwitch)
110114
? EnsembleSwitch.type
111-
: EnsembleTripleStateSwitch.type,
115+
: EnsembleTripleSwitch.type,
112116
controller: widget._controller,
113117
widget: FormField<bool>(
114118
key: validatorKey,
@@ -117,10 +121,9 @@ class SwitchBaseState extends FormFieldWidgetState<SwitchBase> {
117121
widget._controller.required &&
118122
!widget._controller.value;
119123

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;
124127

125128
if (switchRequiredStatus || tripleStateRequiredStatus) {
126129
return Utils.translateWithFallback(
@@ -146,34 +149,23 @@ class SwitchBaseState extends FormFieldWidgetState<SwitchBase> {
146149
);
147150
}
148151

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 {
166153
final SwitchState switchState =
167154
SwitchState.values.from(widget._controller.value) ?? SwitchState.off;
168155
return TripleStateSwitch(
169156
dotColor: widget._controller.activeThumbColor,
170157
disableDotColor: widget._controller.inactiveThumbColor,
171158
startBackgroundColor: widget._controller.activeColor,
172-
middleBackgroundColor: widget._controller.intermediateColor,
159+
middleBackgroundColor: widget._controller.mixedColor,
173160
endBackgroundColor: widget._controller.inactiveColor,
174161
disable: widget._controller.enabled == false,
175162
state: switchState,
176-
onChanged: isEnabled() ? (value) => onToggle(value.name) : (_) {},
163+
onChanged: isEnabled()
164+
? (value) {
165+
(widget as EnsembleTripleSwitch?)?.onToggle(value);
166+
onChange();
167+
}
168+
: (_) {},
177169
);
178170
}
179171

@@ -207,7 +199,19 @@ class SwitchBaseState extends FormFieldWidgetState<SwitchBase> {
207199
trackColor: trackColor,
208200
thumbColor: thumbColor,
209201
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+
}
211215
}
212216
}
213217

@@ -219,7 +223,7 @@ class SwitchBaseController extends FormFieldController {
219223
Color? activeThumbColor;
220224
Color? inactiveColor;
221225
Color? inactiveThumbColor;
222-
Color? intermediateColor;
226+
Color? mixedColor;
223227

224228
framework.EnsembleAction? onChange;
225229
}

lib/widget/widget_registry.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class WidgetRegistry {
136136
PasswordInput.type: () => PasswordInput(),
137137
EnsembleCheckbox.type: () => EnsembleCheckbox(),
138138
EnsembleSwitch.type: () => EnsembleSwitch(),
139-
EnsembleTripleStateSwitch.type: () => EnsembleTripleStateSwitch(),
139+
EnsembleTripleSwitch.type: () => EnsembleTripleSwitch(),
140140
Dropdown.type: () => Dropdown(),
141141
Button.type: () => Button(),
142142
EnsembleIconButton.type: () => EnsembleIconButton(),

0 commit comments

Comments
 (0)