Skip to content

Commit 0800967

Browse files
committed
Refactor icon handling and improve docstrings
Updated icon construction logic in navigation bar controls to use buildIconOrWidget for consistency. Improved and clarified docstrings for Icon and NavigationBarDestination classes in Python, including parameter explanations and accessibility notes. Refactored theme parsing for switch themes to simplify argument usage. Minor formatting and doc improvements in CupertinoNavigationBar and IconData.
1 parent f4ed796 commit 0800967

File tree

8 files changed

+73
-77
lines changed

8 files changed

+73
-77
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import '../extensions/control.dart';
55
import '../models/control.dart';
66
import '../utils/borders.dart';
77
import '../utils/colors.dart';
8-
import '../utils/icons.dart';
98
import '../utils/numbers.dart';
109
import '../widgets/flet_store_mixin.dart';
1110
import 'base_controls.dart';
@@ -55,7 +54,7 @@ class _CupertinoNavigationBarControlState
5554
return BottomNavigationBarItem(
5655
tooltip: !dest.disabled ? dest.getString("tooltip") : null,
5756
backgroundColor: dest.getColor("bgcolor", context),
58-
icon: dest.buildWidget("icon") ?? Icon(dest.getIconData("icon")),
57+
icon: dest.buildIconOrWidget("icon")!,
5958
activeIcon: dest.buildIconOrWidget("selected_icon"),
6059
label: dest.getString("label", "")!);
6160
}).toList());

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class NavigationBarDestinationControl extends StatelessWidget {
1919
var child = NavigationDestination(
2020
enabled: !control.disabled,
2121
tooltip: !control.disabled ? control.getString("tooltip") : null,
22-
icon: control.buildWidget("icon") ?? Icon(control.getIconData("icon")),
22+
icon: control.buildIconOrWidget("icon")!,
2323
selectedIcon: control.buildWidget("selected_icon") ??
2424
(selectedIcon != null ? Icon(selectedIcon) : null),
2525
label: control.getString("label", "")!);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ IconData? parseIconData(int? value, FletBackend backend,
1515
}
1616
}
1717

18-
return null;
18+
return defaultValue;
1919
}
2020

2121
WidgetStateProperty<Icon?>? parseWidgetStateIcon(

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ ThemeData parseTheme(
160160
checkboxTheme: parseCheckboxTheme(value?["checkbox_theme"], theme),
161161
radioTheme: parseRadioTheme(value?["radio_theme"], theme),
162162
badgeTheme: parseBadgeTheme(value?["badge_theme"], theme),
163-
switchTheme: parseSwitchTheme(value?["switch_theme"], theme, context),
163+
switchTheme: parseSwitchTheme(value?["switch_theme"], context),
164164
dividerTheme: parseDividerTheme(value?["divider_theme"], theme),
165165
snackBarTheme: parseSnackBarTheme(value?["snackbar_theme"], theme),
166166
bannerTheme: parseBannerTheme(value?["banner_theme"], theme),
@@ -661,10 +661,10 @@ BadgeThemeData? parseBadgeTheme(Map<dynamic, dynamic>? value, ThemeData theme,
661661
}
662662

663663
SwitchThemeData? parseSwitchTheme(
664-
Map<dynamic, dynamic>? value, ThemeData theme, BuildContext context,
664+
Map<dynamic, dynamic>? value, BuildContext context,
665665
[SwitchThemeData? defaultValue]) {
666666
if (value == null) return defaultValue;
667-
667+
var theme = Theme.of(context);
668668
return theme.switchTheme.copyWith(
669669
thumbColor: parseWidgetStateColor(value["thumb_color"], theme),
670670
trackColor: parseWidgetStateColor(value["track_color"], theme),
@@ -1392,10 +1392,9 @@ extension ThemeParsers on Control {
13921392
return parseBadgeTheme(get(propertyName), theme, defaultValue);
13931393
}
13941394

1395-
SwitchThemeData? getSwitchTheme(
1396-
String propertyName, ThemeData theme, BuildContext context,
1395+
SwitchThemeData? getSwitchTheme(String propertyName, BuildContext context,
13971396
[SwitchThemeData? defaultValue]) {
1398-
return parseSwitchTheme(get(propertyName), theme, context, defaultValue);
1397+
return parseSwitchTheme(get(propertyName), context, defaultValue);
13991398
}
14001399

14011400
DividerThemeData? getDividerTheme(String propertyName, ThemeData theme,

sdk/python/packages/flet/src/flet/controls/core/icon.py

Lines changed: 35 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
@control("Icon")
1212
class Icon(ConstrainedControl):
1313
"""
14-
Displays a Material icon.
14+
A control that displays an icon from a built-in or custom icon set.
15+
16+
Icons can be customized in color, size, and visual style using various
17+
parameters such as stroke weight, fill level, and shadows.
1518
1619
Raises:
1720
AssertionError: If [`fill`][(c).] is less than `0.0` or greater than `1.0`.
@@ -21,111 +24,88 @@ class Icon(ConstrainedControl):
2124

2225
icon: IconData
2326
"""
24-
The icon to display.
27+
The icon to display, selected from a predefined icon set.
2528
26-
You can search through the list of all available icons using our
27-
[Icons browser](https://gallery.flet.dev/icons-browser/) app
28-
[written in Flet](https://github.com/flet-dev/examples/blob/main/python/apps/icons-browser/main.py).
29+
You can explore available icons using the
30+
[Flet Icons Browser](https://gallery.flet.dev/icons-browser/).
2931
"""
3032

3133
color: Optional[ColorValue] = None
3234
"""
33-
Icon color.
35+
The color to use when drawing the icon.
3436
"""
3537

3638
size: Optional[Number] = None
3739
"""
38-
The icon's size.
39-
40-
Icons occupy a square with width and height equal to `size`.
41-
42-
Defaults to the nearest [`IconTheme.size`][flet.IconTheme.size].
40+
The size (width and height) of the square area the icon will occupy.
4341
44-
If this `Icon` is being placed inside an [`IconButton`][flet.IconButton], then use
45-
[`IconButton.icon_size`][flet.IconButton.icon_size] instead, so that the
46-
`IconButton` can make the splash area the appropriate size as well.
47-
The `IconButton` uses an [`IconTheme`][flet.IconTheme] to pass down the
48-
size to the `Icon`.
42+
If not set, a default size will be used. When placing this icon
43+
inside other controls (such as buttons), those controls may also affect sizing.
4944
"""
5045

5146
semantics_label: Optional[str] = None
5247
"""
53-
The semantics label for this icon.
48+
An accessibility label for the icon.
5449
55-
It is not shown to the in the UI, but is announced in accessibility modes
56-
(e.g. TalkBack/VoiceOver).
50+
This text is not displayed visually but may be announced by screen readers
51+
or other assistive technologies.
5752
"""
5853

5954
shadows: Optional[BoxShadowValue] = None
6055
"""
61-
A list of Shadows that will be painted underneath the icon.
56+
A list of shadows to apply beneath the icon.
6257
63-
Multiple shadows are supported to replicate lighting from multiple light sources.
64-
65-
Shadows must be in the same order for Icon to be considered as equivalent as order
66-
produces differing transparency.
58+
Use multiple shadows to simulate complex lighting effects.
59+
The order of shadows matters for how transparency is blended.
6760
"""
6861

6962
fill: Optional[Number] = None
7063
"""
71-
The fill for drawing the icon.
72-
73-
Requires the underlying icon font to support the `FILL` FontVariation axis,
74-
otherwise has no effect. Variable font filenames often indicate the supported axes.
75-
Must be between `0.0` (unfilled) and `1.0` (filled), inclusive.
64+
The fill amount of the icon, between `0.0` (outline) and `1.0` (solid).
7665
77-
Can be used to convey a state transition for animation or interaction.
66+
This feature requires the icon's font to support fill variation.
67+
It can be used to indicate state transitions or selection visually.
7868
"""
7969

8070
apply_text_scaling: Optional[bool] = None
8171
"""
82-
Whether to scale the size of this widget using the ambient MediaQuery's TextScaler.
72+
Whether to scale the icon based on the system or user's preferred text size.
8373
84-
This is specially useful when you have an icon associated with a text,
85-
as scaling the text without scaling the icon would result in a confusing interface.
74+
Useful when placing icons alongside text, ensuring both scale consistently
75+
for better readability and accessibility.
8676
"""
8777

8878
grade: Optional[Number] = None
8979
"""
90-
The grade (granular stroke weight) for drawing the icon.
80+
A fine-tuning adjustment for the stroke thickness of the icon.
9181
92-
Requires the underlying icon font to support the `GRAD` FontVariation axis,
93-
otherwise has no effect. Variable font filenames often indicate the supported axes.
94-
Can be negative.
95-
96-
Grade and weight both affect a symbol's stroke weight (thickness),
97-
but grade has a smaller impact on the size of the symbol.
98-
99-
Grade is also available in some text fonts. One can match grade levels between
100-
text and symbols for a harmonious visual effect. For example, if the text font
101-
has a -25 grade value, the symbols can match it with a suitable value, say -25.
82+
This requires support from the icon's font. Grade values can be negative or
83+
positive.
84+
It allows precise visual adjustments without changing icon size.
10285
"""
10386

10487
weight: Optional[Number] = None
10588
"""
106-
The stroke weight for drawing the icon.
89+
The stroke weight (thickness) of the icon's lines.
10790
108-
Requires the underlying icon font to support the `wght` FontVariation axis,
109-
otherwise has no effect. Variable font filenames often indicate the supported axes.
91+
This requires the icon font to support weight variation.
11092
Must be greater than `0`.
11193
"""
11294

11395
optical_size: Optional[Number] = None
11496
"""
115-
The optical size for drawing the icon.
97+
Adjusts the icon's visual style for different sizes to maintain clarity and balance.
11698
117-
Requires the underlying icon font to support the `opsz` FontVariation axis,
118-
otherwise has no effect. Variable font filenames often indicate the supported axes.
99+
This requires the icon font to support optical sizing.
119100
Must be greater than `0`.
120-
121-
For an icon to look the same at different sizes, the stroke weight (thickness)
122-
must change as the icon size scales. Optical size offers a way to automatically
123-
adjust the stroke weight as icon size changes.
124101
"""
125102

126103
blend_mode: Optional[BlendMode] = BlendMode.SRC_OVER
127104
"""
128-
The BlendMode to apply to the foreground of the icon.
105+
The blend mode used when rendering the icon.
106+
107+
Blend modes control how the icon's color interacts with the background.
108+
The default is normal blending (`SRC_OVER`).
129109
"""
130110

131111
def before_update(self):

sdk/python/packages/flet/src/flet/controls/cupertino/cupertino_navigation_bar.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class CupertinoNavigationBar(ConstrainedControl):
2323
destinations in an app.
2424
2525
Raises:
26-
AssertionError: If [`destinations`][(c).] does not contain at least two visible [`NavigationBarDestination`][flet.NavigationBarDestination]s.
26+
AssertionError: If [`destinations`][(c).] does not contain at least two visible
27+
[`NavigationBarDestination`][flet.NavigationBarDestination]s.
2728
IndexError: If [`selected_index`][(c).] is out of range.
2829
"""
2930

@@ -33,15 +34,15 @@ class CupertinoNavigationBar(ConstrainedControl):
3334
3435
Note:
3536
Must be a list of two or more [`NavigationBarDestination`][flet.NavigationBarDestination]s.
36-
"""
37+
""" # noqa: E501
3738

3839
selected_index: int = 0
3940
"""
40-
The index into [`destinations`][flet.CupertinoNavigationBar.destinations] for the currently
41-
selected [`NavigationBarDestination`][flet.NavigationBarDestination].
41+
The index into [`destinations`][flet.CupertinoNavigationBar.destinations] for the
42+
currently selected [`NavigationBarDestination`][flet.NavigationBarDestination].
4243
4344
Note:
44-
Must be a value between `0` and the length of visible
45+
Must be a value between `0` and the length of visible
4546
[`destinations`][flet.CupertinoNavigationBar.destinations], inclusive.
4647
"""
4748

@@ -87,5 +88,6 @@ def before_update(self):
8788
if not (0 <= self.selected_index < visible_destinations_count):
8889
raise IndexError(
8990
f"selected_index ({self.selected_index}) is out of range. "
90-
f"Expected a value between 0 and {visible_destinations_count - 1} inclusive."
91+
f"Expected a value between 0 and {visible_destinations_count - 1} "
92+
"inclusive."
9193
)

sdk/python/packages/flet/src/flet/controls/icon_data.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,28 @@ class IconData(IntEnum):
3131
"""
3232

3333
def __new__(cls, value):
34+
"""
35+
Create a new IconData enum member.
36+
37+
Args:
38+
value: The encoded integer representing the icon.
39+
40+
Returns:
41+
An instance of the enum with the encoded value.
42+
"""
3443
obj = int.__new__(cls, value)
3544
obj._value_ = value
3645
return obj
3746

3847
@classmethod
3948
def __init_subclass__(cls, **kwargs):
49+
"""
50+
Hook called when a subclass is defined. Used to attach metadata.
51+
52+
Keyword Args:
53+
package_name: The Flutter package where the icon set is defined.
54+
class_name: The name of Flutter class with icon definitions.
55+
"""
4056
cls._package_name = kwargs.pop("package_name", "")
4157
cls._class_name = kwargs.pop("class_name", "")
4258
super().__init_subclass__(**kwargs)

sdk/python/packages/flet/src/flet/controls/material/navigation_bar.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,7 @@ class NavigationBarDestination(AdaptiveControl):
3939
The value must be a list of two or more NavigationBarDestination instances.
4040
"""
4141

42-
label: Optional[str] = None
43-
"""
44-
The text label that appears below the icon of this `NavigationBarDestination`.
45-
"""
46-
47-
icon: Optional[IconDataOrControl] = None
42+
icon: IconDataOrControl
4843
"""
4944
The [name of the icon](https://flet.dev/docs/reference/icons) or `Control` of the
5045
destination.
@@ -66,6 +61,11 @@ class NavigationBarDestination(AdaptiveControl):
6661
should be set to the stroked version and `selected_icon` to the filled version.
6762
"""
6863

64+
label: Optional[str] = None
65+
"""
66+
The text label that appears below the icon of this `NavigationBarDestination`.
67+
"""
68+
6969
selected_icon: Optional[IconDataOrControl] = None
7070
"""
7171
The [name](https://flet.dev/docs/reference/icons) of alternative icon or `Control`

0 commit comments

Comments
 (0)