Skip to content

Commit ed054bb

Browse files
committed
feat: add height, width properties to switch and update schema
1 parent cee5c53 commit ed054bb

File tree

2 files changed

+71
-33
lines changed

2 files changed

+71
-33
lines changed

assets/schema/ensemble_schema.json

+21-8
Original file line numberDiff line numberDiff line change
@@ -1877,7 +1877,7 @@
18771877
}
18781878
]
18791879
},
1880-
"TripleStateSwitch-payload": {
1880+
"TripleSwitch-payload": {
18811881
"type": "object",
18821882
"allOf": [
18831883
{
@@ -1887,24 +1887,37 @@
18871887
"type": "object",
18881888
"properties": {
18891889
"value": {
1890-
"type": "boolean"
1890+
"type": "string",
1891+
"enum": [
1892+
"off",
1893+
"mixed",
1894+
"on"
1895+
]
18911896
},
18921897
"leadingText": {
18931898
"type": "string"
18941899
},
18951900
"trailingText": {
18961901
"type": "string"
18971902
},
1903+
"height": {
1904+
"type": "integer",
1905+
"description": "The height of the switch"
1906+
},
1907+
"width": {
1908+
"type": "integer",
1909+
"description": "The width of the switch"
1910+
},
18981911
"styles": {
18991912
"allOf": [
19001913
{
19011914
"$ref": "#/$defs/switchStyles"
19021915
},
19031916
{
19041917
"properties": {
1905-
"intermediateColor": {
1918+
"mixedColor": {
19061919
"$ref": "#/$defs/type-color",
1907-
"description": "The middle state color of the Switch Widget."
1920+
"description": "The mixed state color of the Switch Widget."
19081921
}
19091922
}
19101923
}
@@ -5977,13 +5990,13 @@
59775990
}
59785991
},
59795992
{
5980-
"title": "TripleStateSwitch",
5993+
"title": "TripleSwitch",
59815994
"required": [
5982-
"TripleStateSwitch"
5995+
"TripleSwitch"
59835996
],
59845997
"properties": {
5985-
"TripleStateSwitch": {
5986-
"$ref": "#/$defs/TripleStateSwitch-payload",
5998+
"TripleSwitch": {
5999+
"$ref": "#/$defs/TripleSwitch-payload",
59876000
"description": "Kitchen Sink Example: https://studio.ensembleui.com/app/e24402cb-75e2-404c-866c-29e6c3dd7992/screen/"
59886001
}
59896002
}

lib/widget/switch.dart

+50-25
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ class EnsembleTripleSwitch extends SwitchBase {
3434
Map<String, Function> setters() => Map<String, Function>.from(super.setters())
3535
..addAll({
3636
'value': (value) => _controller.value =
37-
SwitchState.values.from(value)?.name ?? SwitchState.off.name
37+
SwitchState.values.from(value)?.name ?? SwitchState.off.name,
38+
'custom': (value) =>
39+
_controller.custom = Utils.getBool(value, fallback: false),
40+
'height': (value) => _controller.height = Utils.optionalDouble(value),
41+
'width': (value) => _controller.width = Utils.optionalDouble(value),
3842
});
3943

4044
void onToggle(SwitchState newValue) {
@@ -158,14 +162,23 @@ class SwitchBaseState extends FormFieldWidgetState<SwitchBase> {
158162
startBackgroundColor: widget._controller.activeColor,
159163
middleBackgroundColor: widget._controller.mixedColor,
160164
endBackgroundColor: widget._controller.inactiveColor,
165+
width: widget._controller.width,
166+
height: widget._controller.height,
161167
disable: widget._controller.enabled == false,
162168
state: switchState,
163169
onChanged: isEnabled()
164170
? (value) {
165-
(widget as EnsembleTripleSwitch?)?.onToggle(value);
166-
onChange();
171+
if (!widget._controller.custom) {
172+
(widget as EnsembleTripleSwitch?)?.onToggle(value);
173+
}
174+
onChange(value.name);
167175
}
168176
: (_) {},
177+
onThumbTapped: (value) {
178+
if (widget._controller.custom) {
179+
onChange(value.name);
180+
}
181+
},
169182
);
170183
}
171184

@@ -202,20 +215,25 @@ class SwitchBaseState extends FormFieldWidgetState<SwitchBase> {
202215
onChanged: isEnabled()
203216
? (value) {
204217
(widget as EnsembleSwitch?)?.onToggle(value);
205-
onChange();
218+
onChange(value);
206219
}
207220
: null);
208221
}
209222

210-
void onChange() {
223+
void onChange(dynamic value) {
211224
if (widget._controller.onChange != null) {
212225
ScreenController().executeAction(context, widget._controller.onChange!,
213-
event: EnsembleEvent(widget));
226+
event: EnsembleEvent(widget,
227+
data:
228+
(widget._controller.custom && widget is EnsembleTripleSwitch)
229+
? {'value': value}
230+
: null));
214231
}
215232
}
216233
}
217234

218235
class SwitchBaseController extends FormFieldController {
236+
bool custom = false;
219237
dynamic value;
220238
String? leadingText;
221239
String? trailingText;
@@ -224,6 +242,8 @@ class SwitchBaseController extends FormFieldController {
224242
Color? inactiveColor;
225243
Color? inactiveThumbColor;
226244
Color? mixedColor;
245+
double? width = 100;
246+
double? height = 40;
227247

228248
framework.EnsembleAction? onChange;
229249
}
@@ -248,6 +268,7 @@ class TripleStateSwitch extends StatelessWidget {
248268
Key? key,
249269
required this.onChanged,
250270
required this.state,
271+
this.onThumbTapped,
251272
this.startBackgroundColor,
252273
this.middleBackgroundColor,
253274
this.endBackgroundColor,
@@ -262,6 +283,7 @@ class TripleStateSwitch extends StatelessWidget {
262283
}) : super(key: key);
263284

264285
final Function(SwitchState) onChanged;
286+
final Function(SwitchState)? onThumbTapped;
265287
final double? width;
266288
final double? height;
267289
final BorderRadius? borderRadius;
@@ -343,26 +365,29 @@ class TripleStateSwitch extends StatelessWidget {
343365
: state == SwitchState.mixed
344366
? AlignmentDirectional.center
345367
: AlignmentDirectional.centerEnd,
346-
child: child ??
347-
Container(
348-
height: height,
349-
width: height,
350-
decoration: BoxDecoration(
351-
shape: BoxShape.circle,
352-
color: !disable!
353-
? dotColor ?? SwitchColors.dotColor
354-
: SwitchColors.disableDotColor,
355-
boxShadow: !disable!
356-
? [
357-
const BoxShadow(
358-
color: Colors.black,
359-
blurRadius: 10,
360-
spreadRadius: -5,
361-
),
362-
]
363-
: null,
368+
child: InkWell(
369+
onTap: () => onThumbTapped?.call(state),
370+
child: child ??
371+
Container(
372+
height: height,
373+
width: height,
374+
decoration: BoxDecoration(
375+
shape: BoxShape.circle,
376+
color: !disable!
377+
? dotColor ?? SwitchColors.dotColor
378+
: SwitchColors.disableDotColor,
379+
boxShadow: !disable!
380+
? [
381+
const BoxShadow(
382+
color: Colors.black,
383+
blurRadius: 10,
384+
spreadRadius: -5,
385+
),
386+
]
387+
: null,
388+
),
364389
),
365-
),
390+
),
366391
),
367392
],
368393
),

0 commit comments

Comments
 (0)