Skip to content

Commit 3b829ae

Browse files
authored
add thumb_icon to Switch (#2116)
1 parent 7f89312 commit 3b829ae

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

package/lib/src/controls/switch.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import '../models/control.dart';
88
import '../protocol/update_control_props_payload.dart';
99
import '../utils/buttons.dart';
1010
import '../utils/colors.dart';
11+
import '../utils/icons.dart';
1112
import 'create_control.dart';
1213
import 'list_tile.dart';
1314

@@ -110,6 +111,8 @@ class _SwitchControlState extends State<SwitchControl> {
110111
widget.control.attrString("inactiveTrackColor", "")!),
111112
thumbColor: parseMaterialStateColor(
112113
Theme.of(context), widget.control, "thumbColor"),
114+
thumbIcon: parseMaterialStateIcon(
115+
Theme.of(context), widget.control, "thumbIcon"),
113116
trackColor: parseMaterialStateColor(
114117
Theme.of(context), widget.control, "trackColor"),
115118
value: _value,

package/lib/src/utils/icons.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
1+
import 'dart:convert';
2+
13
import 'package:flutter/material.dart';
24

5+
import '../models/control.dart';
36
import 'material_icons.dart';
7+
import 'material_state.dart';
48

59
IconData? getMaterialIcon(String iconName) {
610
return materialIcons[iconName.toLowerCase()];
711
}
12+
13+
MaterialStateProperty<Icon?>? parseMaterialStateIcon(
14+
ThemeData theme, Control control, String propName) {
15+
var v = control.attrString(propName, null);
16+
if (v == null) {
17+
return null;
18+
}
19+
20+
final j1 = json.decode(v);
21+
22+
return getMaterialStateProperty<Icon?>(
23+
j1, (jv) => Icon(getMaterialIcon(jv as String)), null);
24+
}

sdk/python/packages/flet-core/src/flet_core/switch.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def __init__(
9494
inactive_thumb_color: Optional[str] = None,
9595
inactive_track_color: Optional[str] = None,
9696
thumb_color: Union[None, str, Dict[MaterialState, str]] = None,
97+
thumb_icon: Union[None, str, Dict[MaterialState, str]] = None,
9798
track_color: Union[None, str, Dict[MaterialState, str]] = None,
9899
on_change=None,
99100
on_focus=None,
@@ -137,6 +138,7 @@ def __init__(
137138
self.inactive_thumb_color = inactive_thumb_color
138139
self.inactive_track_color = inactive_track_color
139140
self.thumb_color = thumb_color
141+
self.thumb_icon = thumb_icon
140142
self.track_color = track_color
141143
self.on_change = on_change
142144
self.on_focus = on_focus
@@ -148,6 +150,7 @@ def _get_control_name(self):
148150
def _before_build_command(self):
149151
super()._before_build_command()
150152
self._set_attr_json("thumbColor", self.__thumb_color)
153+
self._set_attr_json("thumbIcon", self.__thumb_icon)
151154
self._set_attr_json("trackColor", self.__track_color)
152155

153156
# value
@@ -238,6 +241,15 @@ def thumb_color(self) -> Union[None, str, Dict[MaterialState, str]]:
238241
def thumb_color(self, value: Union[None, str, Dict[MaterialState, str]]):
239242
self.__thumb_color = value
240243

244+
# thumb_icon
245+
@property
246+
def thumb_icon(self) -> Union[None, str, Dict[MaterialState, str]]:
247+
return self.__thumb_icon
248+
249+
@thumb_icon.setter
250+
def thumb_icon(self, value: Union[None, str, Dict[MaterialState, str]]):
251+
self.__thumb_icon = value
252+
241253
# track_color
242254
@property
243255
def track_color(self) -> Union[None, str, Dict[MaterialState, str]]:

0 commit comments

Comments
 (0)