Skip to content

Commit 554fe7f

Browse files
committed
fix: switch on web (#14)
1 parent 0c1ce6f commit 554fe7f

File tree

7 files changed

+68
-12
lines changed

7 files changed

+68
-12
lines changed

example/widgetbook/pages/components/badges_widgetbook.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Widget tagsUseCase(BuildContext context) => WidgetbookTestWidget(
7474
direction: context.knobs.list(
7575
label: 'Direction',
7676
options: ZetaTagDirection.values,
77-
labelBuilder: (value) => value.name.split('.').last.capitalize(),
77+
labelBuilder: enumLabelBuilder,
7878
),
7979
),
8080
],
@@ -90,12 +90,12 @@ Widget workcloudIndicatorsUseCase(BuildContext context) {
9090
label: context.knobs.string(label: 'Label', initialValue: 'Label'),
9191
prioritySize: context.knobs.list(
9292
label: 'Size',
93-
labelBuilder: (value) => value.name.split('.').last.capitalize(),
93+
labelBuilder: enumLabelBuilder,
9494
options: ZetaWidgetSize.values,
9595
),
9696
priorityType: context.knobs.list(
9797
label: 'Type',
98-
labelBuilder: (value) => value.name.split('.').last.capitalize(),
98+
labelBuilder: enumLabelBuilder,
9999
options: ZetaWorkcloudIndicatorType.values,
100100
),
101101
icon: iconKnob(context, rounded: rounded, nullable: true),

example/widgetbook/pages/components/banner_widgetbook.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Widget bannerUseCase(BuildContext context) {
1414
type: context.knobs.list(
1515
label: 'Type',
1616
options: ZetaSystemBannerStatus.values,
17-
labelBuilder: (value) => value.name.split('.').last.capitalize(),
17+
labelBuilder: enumLabelBuilder,
1818
),
1919
leadingIcon: iconKnob(context, rounded: rounded, nullable: true),
2020
titleStart: context.knobs.boolean(label: 'Center title'),

example/widgetbook/pages/components/snack_bar_widgetbook.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Widget snackBarUseCase(BuildContext context) {
2222
final type = context.knobs.listOrNull<ZetaSnackBarType>(
2323
label: 'Type',
2424
options: [null, ...ZetaSnackBarType.values],
25-
labelBuilder: (type) => type?.name ?? '',
25+
labelBuilder: enumLabelBuilder,
2626
);
2727

2828
final leadingIcon = iconKnob(

example/widgetbook/pages/components/switch_widgetbook.dart

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:widgetbook/widgetbook.dart';
33
import 'package:zeta_flutter/zeta_flutter.dart';
44

55
import '../../test/test_components.dart';
6+
import '../../utils/utils.dart';
67

78
Widget switchUseCase(BuildContext context) {
89
bool? isOn = false;
@@ -21,6 +22,11 @@ Widget switchUseCase(BuildContext context) {
2122
ZetaSwitch(
2223
value: isOn,
2324
onChanged: onChanged,
25+
variant: context.knobs.listOrNull(
26+
label: 'Variant',
27+
options: ZetaSwitchType.values,
28+
labelBuilder: enumLabelBuilder,
29+
),
2430
),
2531
Text(isOn == true ? 'On' : 'Off'),
2632
],

example/widgetbook/utils/utils.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ String iconLabelBuilder(IconData? value, [bool rounded = true]) {
1616

1717
List<IconData> iconOptions(rounded) => rounded ? iconsRounded.values.toList() : iconsSharp.values.toList();
1818

19-
String enumLabelBuilder(Enum value) => value.name.split('.').last.capitalize();
19+
String enumLabelBuilder(Enum? value) => value?.name.split('.').last.capitalize() ?? '';
2020

2121
IconData? iconKnob(BuildContext context,
2222
{bool rounded = true, bool nullable = false, String name = 'Icon', final IconData? initial}) {

lib/src/components/switch/material_switch.dart

+11-4
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ class MaterialSwitch extends StatefulWidget {
4545
this.focusNode,
4646
this.onFocusChange,
4747
this.autofocus = false,
48-
// Zeta change: added optional parameter `showHover`.
48+
// Zeta change: added optional parameter `showHover` and `thumbSize`.
4949
this.showHover = false,
50+
this.thumbSize,
5051
}) : assert(activeThumbImage != null || onActiveThumbImageError == null),
5152
assert(inactiveThumbImage != null || onInactiveThumbImageError == null);
5253

@@ -75,8 +76,9 @@ class MaterialSwitch extends StatefulWidget {
7576
final FocusNode? focusNode;
7677
final ValueChanged<bool>? onFocusChange;
7778
final bool autofocus;
78-
// Zeta change: added optional parameter `showHover`.
79+
// Zeta change: added optional parameters `showHover` and `thumbSize`.
7980
final bool showHover;
81+
final Size? thumbSize;
8082
final Size size;
8183

8284
@override
@@ -369,7 +371,9 @@ class _MaterialSwitchState extends State<MaterialSwitch> with TickerProviderStat
369371
..inactiveIcon = effectiveInactiveIcon
370372
..iconTheme = IconTheme.of(context)
371373
..thumbShadow = _switchConfig.thumbShadow
372-
..positionController = positionController,
374+
..positionController = positionController
375+
// Zeta change: pass thumbsize
376+
.._thumbSize = widget.thumbSize,
373377
),
374378
),
375379
);
@@ -685,6 +689,8 @@ class _SwitchPainter extends ToggleablePainter {
685689
ImageProvider? _cachedThumbImage;
686690
ImageErrorListener? _cachedThumbErrorListener;
687691
BoxPainter? _cachedThumbPainter;
692+
// Zeta change: add `_thumbSize`.
693+
Size? _thumbSize;
688694

689695
ShapeDecoration _createDefaultThumbDecoration(Color color, ImageProvider? image, ImageErrorListener? errorListener) {
690696
return ShapeDecoration(
@@ -730,7 +736,8 @@ class _SwitchPainter extends ToggleablePainter {
730736
_pressedThumbExtension = 0;
731737
}
732738

733-
Size? thumbSize = Size.fromRadius(pressedThumbRadius);
739+
// Zeta change: `_thumbSize` override.
740+
Size? thumbSize = _thumbSize ?? Size.fromRadius(pressedThumbRadius);
734741

735742
// The thumb contracts slightly during the animation in Material 2.
736743
final double inset = thumbOffset == null ? 0 : 1.0 - (currentValue - thumbOffset!).abs() * 2.0;

lib/src/components/switch/zeta_switch.dart

+45-2
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,33 @@ import 'material_switch.dart';
88

99
const _sizeAndroid = Size(48, 24);
1010
const _sizeIOS = Size(56, 32);
11+
const _sizeWeb = Size(64, 32);
12+
13+
/// Variants of [ZetaSwitch].
14+
enum ZetaSwitchType {
15+
/// 64 x 32
16+
web,
17+
18+
/// 48 x 24
19+
android,
20+
21+
/// 56 x 32
22+
ios,
23+
}
1124

1225
/// Zeta Switch.
1326
///
1427
/// Switch can turn an option on or off.
28+
///
29+
/// Switch has styles for Android, iOS and Web.
30+
// TODO(switch): Add web icon support.
1531
class ZetaSwitch extends StatelessWidget {
1632
/// Constructor for [ZetaSwitch].
1733
const ZetaSwitch({
1834
super.key,
1935
this.value = false,
2036
this.onChanged,
37+
this.variant,
2138
});
2239

2340
/// Determines whether the switch is on or off.
@@ -26,19 +43,44 @@ class ZetaSwitch extends StatelessWidget {
2643
/// Called when the value of the switch should change.
2744
final ValueChanged<bool?>? onChanged;
2845

46+
/// Variant of switch for different platforms.
47+
///
48+
/// Defaults to match the platform, or falls back to web.
49+
final ZetaSwitchType? variant;
50+
2951
@override
3052
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
3153
super.debugFillProperties(properties);
3254
properties
3355
..add(FlagProperty('value', value: value, ifTrue: 'on', ifFalse: 'off', showName: true))
34-
..add(ObjectFlagProperty<ValueChanged<bool>>('onChanged', onChanged, ifNull: 'disabled'));
56+
..add(ObjectFlagProperty<ValueChanged<bool>>('onChanged', onChanged, ifNull: 'disabled'))
57+
..add(EnumProperty<ZetaSwitchType?>('variant', variant));
58+
}
59+
60+
ZetaSwitchType get _variant {
61+
if (variant != null) return variant!;
62+
if (kIsWeb) return ZetaSwitchType.web;
63+
return switch (Platform.operatingSystem) {
64+
'ios' => ZetaSwitchType.ios,
65+
'android' => ZetaSwitchType.android,
66+
_ => ZetaSwitchType.web,
67+
};
68+
}
69+
70+
Size get _size {
71+
return switch (_variant) {
72+
ZetaSwitchType.ios => _sizeIOS,
73+
ZetaSwitchType.android => _sizeAndroid,
74+
_ => _sizeWeb,
75+
};
3576
}
3677

3778
@override
3879
Widget build(BuildContext context) {
3980
final zetaColors = Zeta.of(context).colors;
81+
4082
return MaterialSwitch(
41-
size: Platform.isIOS ? _sizeIOS : _sizeAndroid,
83+
size: _size,
4284
trackOutlineWidth: const MaterialStatePropertyAll(0),
4385
trackOutlineColor: const MaterialStatePropertyAll(Colors.transparent),
4486
trackColor: MaterialStateProperty.resolveWith((states) {
@@ -53,6 +95,7 @@ class ZetaSwitch extends StatelessWidget {
5395
),
5496
value: value ?? false,
5597
onChanged: onChanged,
98+
thumbSize: _variant == ZetaSwitchType.web ? const Size.square(ZetaSpacing.m) : null,
5699
);
57100
}
58101
}

0 commit comments

Comments
 (0)