How to use NavigationBar to change pages? #1059
-
Can I get an example of how to use the navigation bar code from the documentation to switch between different pages? I'm unsure how you define where each of the button presses go to. I read the Navigation and routing doc but still don't know how it would apply here. import flet as ft
def main(page: ft.Page):
page.title = "NavigationBar Example"
page.navigation_bar = ft.NavigationBar(
destinations=[
ft.NavigationDestination(icon=ft.icons.EXPLORE, label="Explore"),
ft.NavigationDestination(icon=ft.icons.COMMUTE, label="Commute"),
ft.NavigationDestination(
icon=ft.icons.BOOKMARK_BORDER,
selected_icon=ft.icons.BOOKMARK,
label="Explore",
),
]
)
page.add(ft.Text("Body!"))
ft.app(target=main) Update So I had initially put how to "change views" but I'm not sure if that is right because that would just clear the screen. Still don't think "pages" is technically correct either. I just want to the switch content in between. I'm learning Flet by cloning apps I use, so I am trying to clone the headspace app. I was able to work it out but please let me know if this is a good way to go about it. Still not sure how to make it so that the navbar starts with the content index 0 though. I can use import flet as ft
def main(page: ft.Page):
page.title = "Headspace Clone"
def change_content(e):
page.controls.clear()
nav_dest = e.control.selected_index
if nav_dest == 0:
nav_content = ft.Container(
content=ft.Text(value="Today")
)
page.add(nav_content)
if nav_dest == 1:
nav_content = ft.Container(
content=ft.Text(value="Explore")
)
page.add(nav_content)
if nav_dest == 2:
nav_content = ft.Container(
content=ft.Text(value="You")
)
page.add(nav_content)
page.navigation_bar = ft.NavigationBar(
destinations=[
ft.NavigationDestination(icon=ft.icons.LANDSCAPE_OUTLINED, selected_icon=ft.icons.LANDSCAPE, label="Today"),
ft.NavigationDestination(icon=ft.icons.SEARCH, label="Explore"),
ft.NavigationDestination(icon=ft.icons.PERSON_OUTLINED, selected_icon=ft.icons.PERSON, label="You")
],
on_change=change_content
)
page.update()
ft.app(target=main) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
@Dekubaka could you elaborate a little more on what you're trying to do? |
Beta Was this translation helpful? Give feedback.
-
Correct me if I'm wrong, you want the If that's the case, how about you invoke a synthetic event, meaning, programmatically call it's Here's the code: import flet as ft
def main(page: ft.Page):
page.title = "Headspace clone"
def change_content(e):
page.controls.clear()
nav_dest = e.control.selected_index
if nav_dest == 0:
nav_content = ft.Container(
content=ft.Text(value="Today")
)
page.add(nav_content)
if nav_dest == 1:
nav_content = ft.Container(
content=ft.Text(value="Explore")
)
page.add(nav_content)
if nav_dest == 2:
nav_content = ft.Container(
content=ft.Text(value="You")
)
page.add(nav_content)
page.navigation_bar = ft.NavigationBar(
destinations=[
ft.NavigationDestination(
icon=ft.icons.LANDSCAPE_OUTLINED,
selected_icon=ft.icons.LANDSCAPE,
label="Today"
),
ft.NavigationDestination(
icon=ft.icons.SEARCH,
label="Explore"
),
ft.NavigationDestination(
icon=ft.icons.PERSON_OUTLINED,
selected_icon=ft.icons.PERSON,
label="You"
)
],
on_change=change_content,
selected_index=0
)
# a control's did_mount() is invoked right after it has been mounted
page.navigation_bar.did_mount = lambda: synthetic_event(
page=page, control=page.navigation_bar
)
# call the did_mount() once manually if you mess up the order of page.update()
# page.navigation_bar.did_mount()
page.update()
def synthetic_event(page: ft.Page, control: ft.NavigationBar):
"""
Calls the control's event handler
"""
control.on_change(
ft.ControlEvent(
target=control.uid,
name="change",
data=str(control.selected_index),
control=control,
page=page
)
)
ft.app(target=main) |
Beta Was this translation helpful? Give feedback.
Correct me if I'm wrong, you want the
navigation_bar
to render theControl
that corresponds to theselected_index
instead of merely highlighting theselected_index
?If that's the case, how about you invoke a synthetic event, meaning, programmatically call it's
on_change
event handler?Here's the code: