Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Flutter IconData class #4696

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/flet/lib/src/controls/icon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class IconControl extends StatelessWidget {
return constrainedControl(
context,
Icon(
parseIcon(control.attrString("name", "")!),
parseIcon(control.attrString("icon", "")!),
size: control.attrDouble("size"),
color: control.attrColor("color", context),
semanticLabel: control.attrString("semanticsLabel"),
Expand Down
27 changes: 24 additions & 3 deletions packages/flet/lib/src/utils/icons.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,32 @@ import 'cupertino_icons.dart';
import 'material_icons.dart';
import 'material_state.dart';

IconData? parseIcon(String? iconName, [IconData? defaultIcon]) {
if (iconName == null) {
IconData? parseIcon(String? icon, [IconData? defaultIcon]) {
if (icon == null) {
return defaultIcon;
}
return materialIcons[iconName.toLowerCase()] ?? cupertinoIcons[iconName.toLowerCase()];

final j1 = json.decode(icon);
return iconDataFromJson(j1, defaultIcon);
}


IconData? iconDataFromJson(dynamic json, [IconData? defaultIcon]) {
if (json == null) {
return defaultIcon;
} else if (json is String) {
String icon = json.toLowerCase();
return materialIcons[icon] ?? cupertinoIcons[icon];
}
return IconData(
json['code_point'] as int,
fontFamily: json['font_family'] as String?,
fontPackage: json['font_package'] as String?,
matchTextDirection: json['match_text_direction'] as bool? ?? false,
fontFamilyFallback: (json['font_family_fallback'] as List<dynamic>?)
?.map((item) => item as String)
.toList(),
);
}

WidgetStateProperty<Icon?>? parseWidgetStateIcon(
Expand Down
10 changes: 5 additions & 5 deletions sdk/python/packages/flet/src/flet/core/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,12 @@ def _set_attr_json(self, name: str, value: V, wrap_attr_dict: bool = False) -> N
if ov != nv:
self._set_attr(name, nv)

def _set_attr_icon(self, name: str, value) -> None:
self._set_attr_json(name, value.value if isinstance(value, Enum) else value)

def _convert_attr_json(self, value: V) -> Optional[str]:
return (
json.dumps(value, cls=EmbedJsonEncoder, separators=(",", ":"))
if value is not None
else None
)
if value is not None:
return json.dumps(value, cls=EmbedJsonEncoder, separators=(",", ":"))

def _wrap_attr_dict(self, value: Optional[Union[Dict, Any]]) -> Optional[Dict]:
if value is None or isinstance(value, Dict):
Expand Down
2 changes: 1 addition & 1 deletion sdk/python/packages/flet/src/flet/core/cupertino_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def icon(self):
@icon.setter
def icon(self, value):
self.__icon = value
self._set_enum_attr("icon", value, IconEnums)
self._set_attr_icon("icon", value)

# icon_color
@property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def trailing_icon(self) -> Optional[IconValue]:
@trailing_icon.setter
def trailing_icon(self, value: Optional[IconValue]):
self.__trailing_icon = value
self._set_enum_attr("trailingIcon", value, IconEnums)
self._set_attr_icon("trailingIcon", value)

# text
@property
Expand Down
4 changes: 2 additions & 2 deletions sdk/python/packages/flet/src/flet/core/date_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ def switch_to_calendar_icon(self) -> Optional[IconValue]:
@switch_to_calendar_icon.setter
def switch_to_calendar_icon(self, value: Optional[IconValue]):
self.__switch_to_calendar_icon = value
self._set_enum_attr("switchToCalendarEntryModeIcon", value, IconEnums)
self._set_attr_icon("switchToCalendarEntryModeIcon", value)

# switch_to_input_icon
@property
Expand All @@ -331,7 +331,7 @@ def switch_to_input_icon(self) -> Optional[IconValue]:
@switch_to_input_icon.setter
def switch_to_input_icon(self, value: Optional[IconValue]):
self.__switch_to_input_icon = value
self._set_enum_attr("switchToInputEntryModeIcon", value, IconEnums)
self._set_attr_icon("switchToInputEntryModeIcon", value)

# on_change
@property
Expand Down
2 changes: 1 addition & 1 deletion sdk/python/packages/flet/src/flet/core/dropdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ def select_icon(self) -> Optional[IconValueOrControl]:
@select_icon.setter
def select_icon(self, value: Optional[IconValueOrControl]):
self.__select_icon = value
self._set_enum_attr("selectIcon", value, IconEnums)
self._set_attr_icon("selectIcon", value)

# icon_content
@property
Expand Down
2 changes: 1 addition & 1 deletion sdk/python/packages/flet/src/flet/core/elevated_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def icon(self) -> Optional[IconValue]:
@icon.setter
def icon(self, value: Optional[IconValue]):
self.__icon = value
self._set_enum_attr("icon", value, IconEnums)
self._set_attr_icon("icon", value)

# icon_color
@property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def icon(self) -> Optional[IconValue]:
@icon.setter
def icon(self, value: Optional[IconValue]):
self.__icon = value
self._set_enum_attr("icon", value, IconEnums)
self._set_attr_icon("icon", value)

# bgcolor
@property
Expand Down
12 changes: 6 additions & 6 deletions sdk/python/packages/flet/src/flet/core/form_field_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,11 @@ def before_update(self):
)
self._set_attr_json("sizeConstraints", self.__size_constraints)
if isinstance(self.__suffix_icon, str):
self._set_attr("suffixIcon", self.__suffix_icon)
self._set_attr_icon("suffixIcon", self.__suffix_icon)
if isinstance(self.__prefix_icon, str):
self._set_attr("prefixIcon", self.__prefix_icon)
self._set_attr_icon("prefixIcon", self.__prefix_icon)
if isinstance(self.__icon, str):
self._set_attr("icon", self.__icon)
self._set_attr_icon("icon", self.__icon)
if isinstance(self.__label, str):
self._set_attr("label", self.__label)

Expand Down Expand Up @@ -300,7 +300,7 @@ def icon(self) -> Optional[IconValueOrControl]:
@icon.setter
def icon(self, value: Optional[IconValueOrControl]):
self.__icon = value
self._set_enum_attr("icon", value, IconEnums)
self._set_attr_icon("icon", value)

# border
@property
Expand Down Expand Up @@ -653,7 +653,7 @@ def prefix_icon(self) -> Optional[IconValueOrControl]:
@prefix_icon.setter
def prefix_icon(self, value: Optional[IconValueOrControl]):
self.__prefix_icon = value
self._set_enum_attr("prefixIcon", value, IconEnums)
self._set_attr_icon("prefixIcon", value)

# prefix_text
@property
Expand Down Expand Up @@ -690,7 +690,7 @@ def suffix_icon(self) -> Optional[IconValueOrControl]:
@suffix_icon.setter
def suffix_icon(self, value: Optional[IconValueOrControl]):
self.__suffix_icon = value
self._set_enum_attr("suffixIcon", value, IconEnums)
self._set_attr_icon("suffixIcon", value)

# suffix_text
@property
Expand Down
14 changes: 7 additions & 7 deletions sdk/python/packages/flet/src/flet/core/icon.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def main(page: ft.Page):

def __init__(
self,
name: Optional[IconValue] = None,
icon: Optional[IconValue] = None,
color: Optional[ColorValue] = None,
size: OptionalNumber = None,
semantics_label: Optional[str] = None,
Expand Down Expand Up @@ -135,13 +135,13 @@ def before_update(self):

# name
@property
def name(self) -> Optional[IconValue]:
return self.__name
def icon(self) -> Optional[IconValue]:
return self.__icon

@name.setter
def name(self, value: Optional[IconValue]):
self.__name = value
self._set_enum_attr("name", value, IconEnums)
@icon.setter
def icon(self, value: Optional[IconValue]):
self.__icon = value
self._set_attr_icon("icon", value)

# color
@property
Expand Down
4 changes: 2 additions & 2 deletions sdk/python/packages/flet/src/flet/core/icon_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def icon(self) -> Optional[IconValue]:
@icon.setter
def icon(self, value: Optional[IconValue]):
self.__icon = value
self._set_enum_attr("icon", value, IconEnums)
self._set_attr_icon("icon", value)

# selected_icon
@property
Expand All @@ -233,7 +233,7 @@ def selected_icon(self) -> Optional[IconValue]:
@selected_icon.setter
def selected_icon(self, value: Optional[IconValue]):
self.__selected_icon = value
self._set_enum_attr("selectedIcon", value, IconEnums)
self._set_attr_icon("selectedIcon", value)

# icon_size
@property
Expand Down
11 changes: 11 additions & 0 deletions sdk/python/packages/flet/src/flet/core/icon_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from dataclasses import dataclass
from typing import Optional, List


@dataclass
class IconData:
code_point: int
font_family: Optional[str] = None
font_package: Optional[str] = None
match_text_direction: bool = False
font_family_fallback: Optional[List[str]] = None
4 changes: 2 additions & 2 deletions sdk/python/packages/flet/src/flet/core/navigation_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def icon(self) -> Optional[IconValueOrControl]:
@icon.setter
def icon(self, value: Optional[IconValueOrControl]):
self.__icon = value
self._set_enum_attr("icon", value, IconEnums)
self._set_attr_icon("icon", value)

# icon_content
@property
Expand Down Expand Up @@ -136,7 +136,7 @@ def selected_icon(self) -> Optional[IconValueOrControl]:
@selected_icon.setter
def selected_icon(self, value: Optional[IconValueOrControl]):
self.__selected_icon = value
self._set_enum_attr("selectedIcon", value, IconEnums)
self._set_attr_icon("selectedIcon", value)

# selected_icon_content
@property
Expand Down
4 changes: 2 additions & 2 deletions sdk/python/packages/flet/src/flet/core/navigation_drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def icon(self) -> Optional[IconValueOrControl]:
@icon.setter
def icon(self, value: Optional[IconValueOrControl]):
self.__icon = value
self._set_enum_attr("icon", value, IconEnums)
self._set_attr_icon("icon", value)

# icon_content
@property
Expand Down Expand Up @@ -120,7 +120,7 @@ def selected_icon(self) -> Optional[IconValueOrControl]:
@selected_icon.setter
def selected_icon(self, value: Optional[IconValueOrControl]):
self.__selected_icon = value
self._set_enum_attr("selectedIcon", value, IconEnums)
self._set_attr_icon("selectedIcon", value)

# selected_icon_content
@property
Expand Down
4 changes: 2 additions & 2 deletions sdk/python/packages/flet/src/flet/core/navigation_rail.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def icon(self) -> Optional[IconValueOrControl]:
@icon.setter
def icon(self, value: Optional[IconValueOrControl]):
self.__icon = value
self._set_enum_attr("icon", value, IconEnums)
self._set_attr_icon("icon", value)

# icon_content
@property
Expand Down Expand Up @@ -130,7 +130,7 @@ def selected_icon(self) -> Optional[IconValueOrControl]:
@selected_icon.setter
def selected_icon(self, value: Optional[IconValueOrControl]):
self.__selected_icon = value
self._set_enum_attr("selectedIcon", value, IconEnums)
self._set_attr_icon("selectedIcon", value)

# selected_icon_content
@property
Expand Down
2 changes: 1 addition & 1 deletion sdk/python/packages/flet/src/flet/core/outlined_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def icon(self) -> Optional[IconValue]:
@icon.setter
def icon(self, value: Optional[IconValue]):
self.__icon = value
self._set_enum_attr("icon", value, IconEnums)
self._set_attr_icon("icon", value)

# icon_color
@property
Expand Down
4 changes: 2 additions & 2 deletions sdk/python/packages/flet/src/flet/core/popup_menu_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def icon(self) -> Optional[IconValue]:
@icon.setter
def icon(self, value: Optional[IconValue]):
self.__icon = value
self._set_enum_attr("icon", value, IconEnums)
self._set_attr_icon("icon", value)

# text
@property
Expand Down Expand Up @@ -379,7 +379,7 @@ def icon(self) -> Optional[IconValue]:
@icon.setter
def icon(self, value: Optional[IconValue]):
self.__icon = value
self._set_enum_attr("icon", value, IconEnums)
self._set_attr_icon("icon", value)

# icon_color
@property
Expand Down
2 changes: 1 addition & 1 deletion sdk/python/packages/flet/src/flet/core/tabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def before_update(self):
super().before_update()
self._set_attr_json("iconMargin", self.__icon_margin)
if isinstance(self.__icon, IconValue):
self._set_enum_attr("icon", self.__icon, IconEnums)
self._set_attr_icon("icon", self.__icon)

# text
@property
Expand Down
2 changes: 1 addition & 1 deletion sdk/python/packages/flet/src/flet/core/text_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def icon(self) -> Optional[IconValue]:
@icon.setter
def icon(self, value: Optional[IconValue]):
self.__icon = value
self._set_enum_attr("icon", value, IconEnums)
self._set_attr_icon("icon", value)

# icon_color
@property
Expand Down
3 changes: 2 additions & 1 deletion sdk/python/packages/flet/src/flet/core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from flet.core.cupertino_colors import CupertinoColors, cupertino_colors
from flet.core.cupertino_icons import CupertinoIcons, cupertino_icons
from flet.core.event import Event
from flet.core.icon_data import IconData
from flet.core.icons import Icons, icons
from flet.core.margin import Margin
from flet.core.padding import Padding
Expand Down Expand Up @@ -406,7 +407,7 @@ class LocaleConfiguration:

# Icons
IconEnums = (icons, Icons, cupertino_icons, CupertinoIcons)
IconValue = Union[str, icons, Icons, cupertino_icons, CupertinoIcons]
IconValue = Union[str, icons, Icons, cupertino_icons, CupertinoIcons, IconData]
IconValueOrControl = Union[IconValue, "Control"]

# ControlState
Expand Down