When I close the AlertDialog,the "page.on_view_pop" will run
I just set a AlertDialog.When i close it,the "page.on_view_pop" will run.
could you tell me the relationship between the view and alertDialog. When I use the alertDialog and two views, I face many question. Thank you.
@ndvanforeest
Can you share some code as required by the issue template?
I have same issue with from flet v0.26.0 to 0.27.6. Windows.
Step to reproduce.
- Push "Visit Store" and go to /store view
- Push "dialog" and open ft.AlertDialog control
- Mash "mash this button"
- Then, page.on_view_pop will be run without pushing back button
Code
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.ON_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.ON_SURFACE_VARIANT),
ft.ElevatedButton("dialog", on_click=open_dialog),
ft.ElevatedButton("Go Home", on_click=lambda _: page.go("/")),
],
)
)
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)
def open_dialog(e):
def handle_close(e):
page.close(dlg_modal)
dlg_modal = ft.AlertDialog(
title=ft.Text("Dialog"),
actions=[
ft.ElevatedButton("mash this button", on_click=handle_close),
],
)
page.open(dlg_modal)
ft.app(target=main)
An example of functional routing is here:https://github.com/flet-dev/flet/discussions/4495#discussioncomment-11636178
Your problem is that you both are using on_route_change to manage routing with the help of on_view_pop. When you exit a view in flet, on_view_pop can be fired depending on the context because of the close event of a view. Also the on_route_change event is not working for me as a routing event, only works to do something when the routing is already done with other methods.
You should only use on_view_pop to manage special routing events like the android back button.
To make routing in general cases you should use lambda _: "function"(arguments) on the on_click event of the control that you use to make user travel. "Function" should make the desired routing adding a view on top of the existent with:
page.views.append(new_view)
page.update()
You need to eliminate old views at some point so you dont have 500 views in the page.views list, but you have to leave at least 2 always, including actual.
If you need something more please tell me.
An example of functional routing is here:#4495 (reply in thread)
Your problem is that you both are using on_route_change to manage routing with the help of on_view_pop. When you exit a view in flet, on_view_pop can be fired depending on the context because of the close event of a view. Also the on_route_change event is not working for me as a routing event, only works to do something when the routing is already done with other methods.
You should only use on_view_pop to manage special routing events like the android back button.
To make routing in general cases you should use lambda _: "function"(arguments) on the on_click event of the control that you use to make user travel. "Function" should make the desired routing adding a view on top of the existent with:
page.views.append(new_view)page.update()You need to eliminate old views at some point so you dont have 500 views in the page.views list, but you have to leave at least 2 always, including actual.
Thank you @SIRGPrice. I understand that this approach works well.
However, my code is the official Flet sample code. I just added an AlertDialog.
If your way is not a hack, It seems to me that the document should be modified.
I try with flet documentation examples every time I do something new, but sometimes it just does not work properly depending on the aproach or code architecture. So I had to manage to find a solution.
In any case this solution is compatible with flet documentation because there are several oficial ways in wich you can use routing.
First is yours, using the View.route property, second is using page.views.append(view), third is page.on_view_pop: lambda_: f(args), ...etc.
The issue is no longer reproducible in Flet v1.
A working example:
import asyncio
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(
route="/",
controls=[
ft.AppBar(
title=ft.Text("Flet app"), bgcolor=ft.Colors.ON_SURFACE_VARIANT
),
ft.Button(
"Visit Store",
on_click=lambda: asyncio.create_task(page.push_route("/store")),
),
],
)
)
if page.route == "/store":
page.views.append(
ft.View(
route="/store",
controls=[
ft.AppBar(
title=ft.Text("Store"), bgcolor=ft.Colors.ON_SURFACE_VARIANT
),
ft.Button("Show dialog", on_click=open_dialog),
ft.Button(
"Go Home",
on_click=lambda: asyncio.create_task(page.push_route("/")),
),
],
)
)
page.update()
async def view_pop(e):
if len(page.views) > 1:
page.views.pop()
top_view = page.views[-1]
await page.push_route(top_view.route)
def open_dialog(e):
def handle_close(e):
page.pop_dialog()
dlg_modal = ft.AlertDialog(
title=ft.Text("Dialog"),
actions=[
ft.Button("mash this button", on_click=handle_close),
],
)
page.show_dialog(dlg_modal)
page.on_route_change = route_change
page.on_view_pop = view_pop
route_change(page.route)
ft.run(main)
Hi @FeodorFitsner , could you please check it one more time? Your "A working example" code now(0.86.5) working with little problem if we have more than 2+ views and some dialog opened
for you code (on mobile phone) in previous comment do:
- open app
- click 'Visit Store' button
- click 'Show dialog' button
- now when you have 2 views and dialog opened press 'Back' button(from bottom navigation bar or from back gesture or hardware phone button) on device()
result: The dialog is closed and the last view is also closed.
expected: if you have any active dialog - only that dialog is closed at step 4. if no active dialogs - last view closed.
I have that problem on Android(16) device.
Thank you!