Skip to content

Commit 53a33f5

Browse files
committed
Delegate creation of IconData to extensions
1 parent e27b1e3 commit 53a33f5

File tree

9 files changed

+63
-34
lines changed

9 files changed

+63
-34
lines changed

packages/flet/lib/src/controls/icon_button.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class _IconButtonControlState extends State<IconButtonControl>
125125
Widget? iconWidget;
126126
if (icon is Control) {
127127
iconWidget = ControlWidget(control: icon);
128-
} else if (icon is String) {
128+
} else if (icon is int) {
129129
iconWidget = Icon(
130130
widget.control.getIconData("icon"),
131131
color: iconColor,
@@ -138,7 +138,7 @@ class _IconButtonControlState extends State<IconButtonControl>
138138

139139
if (selectedIcon is Control) {
140140
selectedIconWidget = ControlWidget(control: selectedIcon);
141-
} else if (selectedIcon is String) {
141+
} else if (selectedIcon is int) {
142142
selectedIconWidget = Icon(
143143
widget.control.getIconData("selected_icon"),
144144
color: selectedIconColor,

packages/flet/lib/src/controls/navigation_drawer.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ class _NavigationDrawerControlState extends State<NavigationDrawerControl> {
6868
? ControlWidget(
6969
control: icon,
7070
)
71-
: Icon(parseIconData(icon)),
71+
: Icon(parseIconData(icon, widget.control.backend)),
7272
label: Text(dest.getString("label", "")!),
7373
selectedIcon: selectedIcon is Control
7474
? ControlWidget(
7575
control: selectedIcon,
7676
)
7777
: selectedIcon is int
78-
? Icon(parseIconData(selectedIcon))
78+
? Icon(parseIconData(selectedIcon, widget.control.backend))
7979
: null,
8080
);
8181
} else {

packages/flet/lib/src/extensions/control.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ extension WidgetFromControl on Control {
5050
if (c == null) return null;
5151
c.notifyParent = notifyParent;
5252
return ControlWidget(key: key, control: c);
53-
} else if (icon is String) {
53+
} else if (icon is int) {
5454
return Icon(getIconData(propertyName), color: color);
5555
}
5656
return null;

packages/flet/lib/src/flet_core_extension.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,26 @@ class FletCoreExtension extends FletExtension {
370370
}
371371
}
372372

373+
@override
374+
IconData? createIconData(iconCode) {
375+
int setId = (iconCode >> 16) & 0xFF;
376+
int codePoint = iconCode & 0xFFFF;
377+
String? fontFamily;
378+
String? fontPackage;
379+
380+
if (setId == 1) {
381+
fontFamily = "MaterialIcons";
382+
} else if (setId == 2) {
383+
fontFamily = "CupertinoIcons";
384+
fontPackage = "cupertino_icons";
385+
} else {
386+
return null;
387+
}
388+
389+
return IconData(codePoint,
390+
fontFamily: fontFamily, fontPackage: fontPackage);
391+
}
392+
373393
@override
374394
void ensureInitialized() {}
375395
}

packages/flet/lib/src/flet_extension.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import 'flet_service.dart';
44
import 'models/control.dart';
55

66
abstract class FletExtension {
7+
void ensureInitialized() {}
8+
79
Widget? createWidget(Key? key, Control control) {
810
return null;
911
}
@@ -12,5 +14,7 @@ abstract class FletExtension {
1214
return null;
1315
}
1416

15-
void ensureInitialized() {}
17+
IconData? createIconData(int iconCode) {
18+
return null;
19+
}
1620
}

packages/flet/lib/src/services/tester.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class TesterService extends FletService {
6262

6363
case "find_by_icon":
6464
var iconCode = args["icon"];
65-
var icon = parseIconData(iconCode);
65+
var icon = parseIconData(iconCode, control.backend);
6666
if (icon == null) {
6767
throw Exception("Icon not found: $iconCode");
6868
}
Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,44 @@
11
import 'package:flutter/material.dart';
22

3+
import '../flet_backend.dart';
34
import '../models/control.dart';
45
import 'material_state.dart';
56

6-
IconData? parseIconData(int? value, [IconData? defaultValue]) {
7+
IconData? parseIconData(int? value, FletBackend backend,
8+
[IconData? defaultValue]) {
79
if (value == null) return defaultValue;
810

9-
int setId = (value >> 16) & 0xFF;
10-
int codePoint = value & 0xFFFF;
11-
String? fontFamily;
12-
String? fontPackage;
13-
14-
if (setId == 1) {
15-
fontFamily = "MaterialIcons";
16-
} else if (setId == 2) {
17-
fontFamily = "CupertinoIcons";
18-
fontPackage = "cupertino_icons";
11+
for (var extension in backend.extensions) {
12+
var iconData = extension.createIconData(value);
13+
if (iconData != null) {
14+
return iconData;
15+
}
1916
}
2017

21-
return IconData(codePoint, fontFamily: fontFamily, fontPackage: fontPackage);
18+
return null;
2219
}
2320

2421
WidgetStateProperty<Icon?>? parseWidgetStateIcon(
2522
dynamic value,
23+
FletBackend backend,
2624
ThemeData theme, {
2725
Icon? defaultIcon,
2826
WidgetStateProperty<Icon?>? defaultValue,
2927
}) {
3028
if (value == null) return defaultValue;
3129
return getWidgetStateProperty<Icon?>(
32-
value, (jv) => Icon(parseIconData(jv as int)), defaultIcon);
30+
value, (jv) => Icon(parseIconData(jv as int, backend)), defaultIcon);
3331
}
3432

3533
extension IconParsers on Control {
3634
IconData? getIconData(String propertyName, [IconData? defaultValue]) {
37-
return parseIconData(get(propertyName), defaultValue);
35+
return parseIconData(get(propertyName), backend, defaultValue);
3836
}
3937

4038
WidgetStateProperty<Icon?>? getWidgetStateIcon(
4139
String propertyName, ThemeData theme,
4240
{Icon? defaultIcon, WidgetStateProperty<Icon?>? defaultValue}) {
43-
return parseWidgetStateIcon(get(propertyName), theme,
41+
return parseWidgetStateIcon(get(propertyName), backend, theme,
4442
defaultIcon: defaultIcon, defaultValue: defaultValue);
4543
}
4644
}

packages/flet/lib/src/utils/theme.dart

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:flutter/foundation.dart';
55
import 'package:flutter/material.dart';
66
import 'package:flutter/services.dart';
77

8+
import '../flet_backend.dart';
89
import '../models/control.dart';
910
import '../utils/transforms.dart';
1011
import 'alignment.dart';
@@ -159,7 +160,7 @@ ThemeData parseTheme(
159160
checkboxTheme: parseCheckboxTheme(value?["checkbox_theme"], theme),
160161
radioTheme: parseRadioTheme(value?["radio_theme"], theme),
161162
badgeTheme: parseBadgeTheme(value?["badge_theme"], theme),
162-
switchTheme: parseSwitchTheme(value?["switch_theme"], theme),
163+
switchTheme: parseSwitchTheme(value?["switch_theme"], theme, context),
163164
dividerTheme: parseDividerTheme(value?["divider_theme"], theme),
164165
snackBarTheme: parseSnackBarTheme(value?["snackbar_theme"], theme),
165166
bannerTheme: parseBannerTheme(value?["banner_theme"], theme),
@@ -192,8 +193,8 @@ ThemeData parseTheme(
192193
filledButtonTheme:
193194
parseFilledButtonTheme(value?["filled_button_theme"], theme),
194195
iconButtonTheme: parseIconButtonTheme(value?["icon_button_theme"], theme),
195-
segmentedButtonTheme:
196-
parseSegmentedButtonTheme(value?["segmented_button_theme"], theme),
196+
segmentedButtonTheme: parseSegmentedButtonTheme(
197+
value?["segmented_button_theme"], theme, context),
197198
iconTheme: parseIconTheme(value?["icon_theme"], theme),
198199
timePickerTheme: parseTimePickerTheme(value?["time_picker_theme"], theme),
199200
);
@@ -659,7 +660,8 @@ BadgeThemeData? parseBadgeTheme(Map<dynamic, dynamic>? value, ThemeData theme,
659660
);
660661
}
661662

662-
SwitchThemeData? parseSwitchTheme(Map<dynamic, dynamic>? value, ThemeData theme,
663+
SwitchThemeData? parseSwitchTheme(
664+
Map<dynamic, dynamic>? value, ThemeData theme, BuildContext context,
663665
[SwitchThemeData? defaultValue]) {
664666
if (value == null) return defaultValue;
665667

@@ -668,7 +670,8 @@ SwitchThemeData? parseSwitchTheme(Map<dynamic, dynamic>? value, ThemeData theme,
668670
trackColor: parseWidgetStateColor(value["track_color"], theme),
669671
overlayColor: parseWidgetStateColor(value["overlay_color"], theme),
670672
splashRadius: parseDouble(value["splash_radius"]),
671-
thumbIcon: parseWidgetStateIcon(value["thumb_icon"], theme),
673+
thumbIcon: parseWidgetStateIcon(
674+
value["thumb_icon"], FletBackend.of(context), theme),
672675
trackOutlineColor:
673676
parseWidgetStateColor(value["track_outline_color"], theme),
674677
trackOutlineWidth: parseWidgetStateDouble(value["track_outline_width"]),
@@ -1087,10 +1090,11 @@ NavigationBarThemeData? parseNavigationBarTheme(
10871090
}
10881091

10891092
SegmentedButtonThemeData? parseSegmentedButtonTheme(
1090-
Map<dynamic, dynamic>? value, ThemeData theme,
1093+
Map<dynamic, dynamic>? value, ThemeData theme, BuildContext context,
10911094
[SegmentedButtonThemeData? defaultValue]) {
10921095
if (value == null) return defaultValue;
1093-
var selectedIcon = parseIconData(value["selected_icon"]);
1096+
var selectedIcon =
1097+
parseIconData(value["selected_icon"], FletBackend.of(context));
10941098

10951099
return theme.segmentedButtonTheme.copyWith(
10961100
selectedIcon: selectedIcon != null ? Icon(selectedIcon) : null,
@@ -1388,9 +1392,10 @@ extension ThemeParsers on Control {
13881392
return parseBadgeTheme(get(propertyName), theme, defaultValue);
13891393
}
13901394

1391-
SwitchThemeData? getSwitchTheme(String propertyName, ThemeData theme,
1395+
SwitchThemeData? getSwitchTheme(
1396+
String propertyName, ThemeData theme, BuildContext context,
13921397
[SwitchThemeData? defaultValue]) {
1393-
return parseSwitchTheme(get(propertyName), theme, defaultValue);
1398+
return parseSwitchTheme(get(propertyName), theme, context, defaultValue);
13941399
}
13951400

13961401
DividerThemeData? getDividerTheme(String propertyName, ThemeData theme,
@@ -1479,9 +1484,10 @@ extension ThemeParsers on Control {
14791484
}
14801485

14811486
SegmentedButtonThemeData? getSegmentedButtonTheme(
1482-
String propertyName, ThemeData theme,
1487+
String propertyName, ThemeData theme, BuildContext context,
14831488
[SegmentedButtonThemeData? defaultValue]) {
1484-
return parseSegmentedButtonTheme(get(propertyName), theme, defaultValue);
1489+
return parseSegmentedButtonTheme(
1490+
get(propertyName), theme, context, defaultValue);
14851491
}
14861492

14871493
IconThemeData? getIconTheme(String propertyName, ThemeData theme,

sdk/python/packages/flet/integration_tests/test_finders.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import apps.finders as app
2+
import pytest
3+
24
import flet as ft
35
import flet.testing as ftt
4-
import pytest
56

67

78
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)