flet-dev / flet

Flet enables developers to easily build realtime web, mobile and desktop apps in Python. No frontend experience required.
https://flet.dev
Apache License 2.0
9.59k stars 372 forks source link

Add `*Destination.page_controls` property to ease Navigation #3316

Open ndonkoHenri opened 2 weeks ago

ndonkoHenri commented 2 weeks ago

Description

*Destination.page_controls is a list of controls to be displayed in the page when a destination is clicked. It functions by simply replacing the current page.controls by the provided page_controls, provided page_controls is not None..

Test code

import flet as ft

def main(page: ft.Page):
    page.window_always_on_top = True
    page.horizontal_alignment = ft.CrossAxisAlignment.CENTER
    page.vertical_alignment = ft.MainAxisAlignment.CENTER
    page.navigation_bar = ft.NavigationBar(
        on_change=lambda _: None,  # callback must be provided - can we make it work without?
        selected_index=1,
        destinations=[
            ft.NavigationDestination(
                icon=ft.icons.EXPLORE,
                label="Explore",
                page_controls=[ft.Text("Explore")],
            ),
            ft.NavigationDestination(
                icon=ft.icons.COMMUTE,
                label="Commute",
                page_controls=[ft.Text("Commute")],
            ),
            ft.NavigationDestination(
                icon=ft.icons.BOOKMARK_BORDER,
                selected_icon=ft.icons.BOOKMARK,
                label="Explore",
                page_controls=[ft.Text("Bookmark")],
            ),
        ],
    )
    page.add(ft.Text("Commute"))

ft.app(target=main)

Enhancements

The current implementation works as expected... provided on_change callback is set. Without it being set, the page_controls won't be displayed when a destination is clicked.

Related Issue

Closes #3092

bleudev commented 1 week ago

Oh. thank you very much for fixing my issue :) I am very happy! But i wanted to make function because my project needs to changing page properties and other. Can you also add func property to NavigationDestination with type Callable[[Page], None]?

bleudev commented 1 week ago

@ndonkoHenri