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

Introduce Control.on_update() overridable method #2578

Merged
merged 2 commits into from
Feb 8, 2024

Conversation

FeodorFitsner
Copy link
Contributor

  • Set control.page as soon as possible, so its instance is available in control.on_update() method.

Code sample where on_update() is used in a custom control:

import flet as ft


def main(page):
    class AdaptiveIconButton(ft.IconButton):
        def __init__(self, ios_icon, android_icon):
            super().__init__()
            self._ios_icon = ios_icon
            self._android_icon = android_icon

        def on_update(self):
            self.icon = (
                self._ios_icon
                if self.page.platform == ft.PagePlatform.IOS
                or self.page.platform == ft.PagePlatform.MACOS
                else self._android_icon
            )

    def set_android(e):
        page.platform = ft.PagePlatform.ANDROID
        page.update()
        print("New platform:", page.platform)

    def set_ios(e):
        page.platform = "ios"
        page.update()
        print("New platform:", page.platform)

    add_icon = AdaptiveIconButton(
        ios_icon=ft.cupertino_icons.AIRPLANE, android_icon=ft.icons.ADD
    )
    page.add(add_icon)
    page.adaptive = True

    page.add(
        ft.Switch(label="Switch A", adaptive=True),
        ft.ElevatedButton(
            "Set Android", on_click=set_android, content=ft.Text("Set Android")
        ),
        ft.ElevatedButton("Set iOS", on_click=set_ios, content=ft.Text("Set iOS")),
    )

    print("Default platform:", page.platform)


ft.app(target=main)

@FeodorFitsner FeodorFitsner merged commit bf108f3 into main Feb 8, 2024
2 checks passed
@FeodorFitsner FeodorFitsner deleted the control-on-update branch February 8, 2024 19:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant