flet icon indicating copy to clipboard operation
flet copied to clipboard

Add auto generator `on_change` events

Open bleudev opened this issue 1 year ago • 0 comments

Please Describe The Problem To Be Solved

Add in Flet argument for auto generating on_change events in NavigationBar, NavigationDrawer and NavigationRail. And add func argument for destinations. For example,

def first(page2: ft.Page):
    page.add(ft.Text('First page!'))
def second(page2: ft.Page):
    page.add(ft.Text('Second page!'))

page.drawer=NavigationDrawer(controls=[
    ft.NavigationDrawerDestination(label='fist page', icon='settings', func=first),
    ft.NavigationDrawerDestination(label='second page', icon='settings', func=second)
],
selected_index=1, # Second page
auto_generate_on_change=True # If True -> auto generate on_change function
)

When you start app will be called function page.drawer.controls[page.drawer.selected_index].func(page) with page argument. In this example, it will be second page. When user will switch selected_index all controls of page will clear (page.controls.clear()) and will called page.drawer.controls[page.drawer.selected_index].func(page) function

(Optional): Suggest A Solution

class NavigationBar:
    def __init__(self, ..., auto_generate_on_change: bool = False) -> None:
        ...
        if auto_generate_on_change:
            def change(_):
                page.controls.clear()
                self.destinations[self.selected_index].func(page) # idk how get page in flet core
                page.update()
            self.on_change = change
            self.destinations[self.selected_index].func(page)
        ...

bleudev avatar Apr 24 '24 21:04 bleudev