Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions sdk/python/packages/flet/docs/types/cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
::: flet.cache
1 change: 1 addition & 0 deletions sdk/python/packages/flet/docs/types/control.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
::: flet.control
3 changes: 3 additions & 0 deletions sdk/python/packages/flet/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,9 @@ nav:
- Tooltip: types/tooltip.md
- Url: types/url.md
- UnderlineTabIndicator: types/underlinetabindicator.md
- Decorators:
- cache: types/cache.md
- control: types/control.md
- Enums:
- AnimatedSwitcherTransition: types/animatedswitchertransition.md
- AnimationCurve: types/animationcurve.md
Expand Down
30 changes: 20 additions & 10 deletions sdk/python/packages/flet/src/flet/controls/base_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,40 @@ def skip_field():

@dataclass_transform()
def control(
cls_or_type_name: Optional[Union[type[T], str]] = None,
dart_widget_name: Optional[Union[type[T], str]] = None,
*,
isolated: Optional[bool] = None,
post_init_args: int = 1,
**dataclass_kwargs,
) -> Union[type[T], Callable[[type[T]], type[T]]]:
"""Decorator to optionally set 'type' and 'isolated' while behaving like @dataclass.

- Supports `@control` (without parentheses)
- Supports `@control("custom_type")` (with optional arguments)
- Supports `@control("custom_type", post_init_args=1, isolated=True)` to
specify the number of `InitVar` arguments and isolation
"""
Decorator to optionally set widget name and 'isolated' while behaving
like @dataclass.

Parameters:
dart_widget_name: The name of widget on Dart side.
isolated: If `True`, marks the control as isolated. An isolated control
is excluded from page updates when its parent control is updated.
post_init_args: Number of InitVar arguments to pass to __post_init__.
**dataclass_kwargs: Additional keyword arguments passed to @dataclass.

Usage:
- Supports `@control` (without parentheses)
- Supports `@control("WidgetName")` (with optional arguments)
- Supports `@control("WidgetName", post_init_args=1, isolated=True)` to
specify the number of `InitVar` arguments and isolation
"""

# Case 1: If used as `@control` (without parentheses)
if isinstance(cls_or_type_name, type):
if isinstance(dart_widget_name, type):
return _apply_control(
cls_or_type_name, None, isolated, post_init_args, **dataclass_kwargs
dart_widget_name, None, isolated, post_init_args, **dataclass_kwargs
)

# Case 2: If used as `@control("custom_type", post_init_args=N, isolated=True)`
def wrapper(cls: type[T]) -> type[T]:
return _apply_control(
cls, cls_or_type_name, isolated, post_init_args, **dataclass_kwargs
cls, dart_widget_name, isolated, post_init_args, **dataclass_kwargs
)

return wrapper
Expand Down
4 changes: 2 additions & 2 deletions sdk/python/packages/flet/tests/test_object_diff_frozen.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ class AppState:
)


def test_data_view_with_cache():
def test_view_with_cache():
@ft.cache(freeze=True)
def user_details(user: User):
return ft.Card(
Expand Down Expand Up @@ -778,7 +778,7 @@ def users_list(users):
assert len(removed_controls) == 6


def test_empty_data_view():
def test_empty_view():
@ft.cache
def my_view():
return None
Expand Down