I'm learning Flet module for Python and I'm stuck with navigation and routing. To add a new page/layer I use page.views.append() to create elements on a new page. But how do I add elements to the page I've just created outside this instruction? Can I interract with current page somehow? For example: inside page.views.append() I create a button "Show current route". It's on_click function must add ft.Text(f"Current route is: {page.route}") to this new page. The button appears on page but function doesn't work. Code examaple (I took it from https://flet.dev/docs/guides/python/navigation-and-routing and modified it to add new button):
import flet as ft
def main(page: ft.Page):
page.title = "Routes Example"
def route_change(route):
page.views.clear()
page.views.append(
ft.View(
"/",
[
ft.AppBar(title=ft.Text("Flet app"), bgcolor=ft.colors.SURFACE_VARIANT),
ft.ElevatedButton("Visit Store", on_click=lambda _: page.go("/store")),
],
)
)
if page.route == "/store":
page.views.append(
ft.View(
"/store",
[
ft.AppBar(title=ft.Text("Store"), bgcolor=ft.colors.SURFACE_VARIANT),
ft.ElevatedButton("Go Home", on_click=lambda _: page.go("/")),
# This button below appears normally
ft.ElevatedButton(text="Show current route", on_click=show_route_button)
],
)
)
page.update()
def show_route_button(e):
"""This on_click function doesn't add text to current page"""
page.add(ft.Text(f"Current route: {page.route}"))
page.update()
def view_pop(view):
page.views.pop()
top_view = page.views[-1]
page.go(top_view.route)
page.on_route_change = route_change
page.on_view_pop = view_pop
page.go(page.route)
ft.app(target=main)
I tried to use page.add() but it doesn't work if I have multiple layers. I can only change page.title(). Is there any way to access current page and add some elements to it?