Skip to content

feat: Add pop_until_with_result to Page#6347

Open
brunobrown wants to merge 8 commits intoflet-dev:release/v0.85.0from
brunobrown:pop-until-with-result
Open

feat: Add pop_until_with_result to Page#6347
brunobrown wants to merge 8 commits intoflet-dev:release/v0.85.0from
brunobrown:pop-until-with-result

Conversation

@brunobrown
Copy link
Copy Markdown

@brunobrown brunobrown commented Mar 26, 2026

Description

Add Page.pop_until_with_result() method and ViewPopResultEvent to support popping multiple views from the navigation stack and returning a result to the destination view — the Flet equivalent of Flutter's Navigator.popUntilWithResult.

Fixes #6326

Test Code

import asyncio

import flet as ft


def main(page: ft.Page):
    page.title = "pop_until_with_result Example"

    result_text = ft.Text("No result yet", size=18)

    def route_change():
        page.views.clear()
        page.views.append(
            ft.View(
                route="/",
                controls=[
                    ft.AppBar(title=ft.Text("Home"), bgcolor=ft.Colors.SURFACE_BRIGHT),
                    result_text,
                    ft.Button(
                        "Start flow",
                        on_click=lambda _: asyncio.create_task(page.push_route("/step1")),
                    ),
                ],
            )
        )
        if page.route in ["/step1", "/step2"]:
            page.views.append(
                ft.View(
                    route="/step1",
                    controls=[
                        ft.AppBar(title=ft.Text("Step 1"), bgcolor=ft.Colors.SURFACE_BRIGHT),
                        ft.Button(
                            "Go to Step 2",
                            on_click=lambda _: asyncio.create_task(page.push_route("/step2")),
                        ),
                    ],
                )
            )
        if page.route == "/step2":
            page.views.append(
                ft.View(
                    route="/step2",
                    controls=[
                        ft.AppBar(title=ft.Text("Step 2 (Final)"), bgcolor=ft.Colors.SURFACE_BRIGHT),
                        ft.Text("Flow complete!"),
                        ft.Button(
                            "Finish and go Home",
                            on_click=lambda _: asyncio.create_task(
                                page.pop_until_with_result("/", result="Flow completed!")
                            ),
                        ),
                    ],
                )
            )
        page.update()

    def on_pop_result(e: ft.ViewPopResultEvent):
        result_text.value = f"Result: {e.result}"
        page.show_dialog(ft.SnackBar(ft.Text(f"Result: {e.result}")))
        page.update()

    async def view_pop(e: ft.ViewPopEvent):
        if e.view is not None:
            page.views.remove(e.view)
            top_view = page.views[-1]
            await page.push_route(top_view.route)

    page.on_route_change = route_change
    page.on_view_pop = view_pop
    page.on_view_pop_result = on_pop_result
    route_change()


if __name__ == "__main__":
    ft.run(main)

Type of change

  • New feature (non-breaking change which adds functionality)

Checklist

  • I signed the CLA.
  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • New and existing tests pass locally with my changes
  • I have made corresponding changes to the documentation (if applicable)

Screenshots

pop-until-with-result-example

Additional details

  • Flutter reference: Navigator.popUntilWithResultissue #30112 (79+ reactions, 6 years open)
  • No Dart-side changes needed — Python's page.update() syncs the view list and Flutter's Navigator rebuilds with correct transitions automatically
  • 12 unit tests covering: event creation, exports, method behavior, edge cases, event firing
  • Example app added at examples/apps/routing_navigation/pop_until_with_result.py

Files changed

File Change
sdk/python/packages/flet/src/flet/controls/page.py ViewPopResultEvent, pop_until_with_result(), on_view_pop_result, unified before_event
sdk/python/packages/flet/src/flet/__init__.py Export ViewPopResultEvent
sdk/python/packages/flet/tests/test_pop_until_with_result.py 12 unit tests
sdk/python/examples/apps/routing_navigation/pop_until_with_result.py Example app
sdk/python/packages/flet/docs/assets/navigation-routing/pop-until-with-result-example.gif Demo GIF

Summary by Sourcery

Add support for popping multiple views with a result and wire it into page navigation events.

New Features:

  • Introduce ViewPopResultEvent to carry navigation results back to the destination view when popping views.
  • Add Page.pop_until_with_result() to pop back to a target route while returning a result via on_view_pop_result.
  • Expose ViewPopResultEvent from the top-level flet package for direct import.
  • Provide an example routing app demonstrating pop_until_with_result usage.

Tests:

  • Add unit tests covering ViewPopResultEvent creation, typing, imports, the new on_view_pop_result handler, and pop_until_with_result behavior and edge cases.

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Mar 26, 2026

CLA assistant check
All committers have signed the CLA.

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've reviewed this pull request using the Sourcery rules engine

@brunobrown brunobrown force-pushed the pop-until-with-result branch from fb53c21 to 69312d1 Compare March 27, 2026 05:18
@FeodorFitsner
Copy link
Copy Markdown
Contributor

Thanks for the PR!

A few notes:

  • Rename pop_until_with_result() to pop_views_until() (with optional result).
  • Rename on_view_pop_result to on_views_pop_until.

@brunobrown brunobrown force-pushed the pop-until-with-result branch from 69312d1 to 5fb749e Compare March 31, 2026 01:21
@brunobrown brunobrown force-pushed the pop-until-with-result branch from 19a8a70 to f1b4fd3 Compare April 1, 2026 19:29
@FeodorFitsner
Copy link
Copy Markdown
Contributor

Hey @brunobrown, can you change the base of your PR to release/v0.85.0, pull the latest from the base branch and fix integration test(s). Thanks!

@brunobrown brunobrown force-pushed the pop-until-with-result branch from f1b4fd3 to c5fcfde Compare April 6, 2026 14:44
@brunobrown brunobrown changed the base branch from main to release/v0.85.0 April 6, 2026 14:44
@FeodorFitsner
Copy link
Copy Markdown
Contributor

Could you resolve conflicts please?

Add the Page.pop_until_with_result() method and ViewPopResultEvent to support popping multiple views and returning a result to the destination view — the Flet equivalent of Flutter's Navigator.popUntilWithResult.

Fixes flet-dev#6326
Add animated GIF demonstrating the pop_until_with_result feature
for the navigation-routing documentation assets.


Rename pop_until_with_result() to pop_views_until(),
on_view_pop_result to on_views_pop_until,
and ViewPopResultEvent to ViewsPopUntilEvent.

Requested by @FeodorFitsner in the PR review.
Remove standalone unit test file and add integration test to
test_routing_navigation.py following the existing test pattern,
as suggested by @ndonkoHenri in PR review.
…ation test

The SnackBar and result_text both contained "Result: Flow completed!",
causing find_by_text to match 2 elements instead of 1 in the CI
integration test.
Convert pop_views_until example from single file to subdirectory
with main.py + pyproject.toml, matching the new example structure
introduced in v0.84.0. Fix flet_app_main references to use
module.main format consistent with upstream.
Add new feature record to CHANGELOG.md for v0.85.0 as required
by the release branch CI check.
@brunobrown brunobrown force-pushed the pop-until-with-result branch from fd90622 to 42854d7 Compare April 8, 2026 05:33
Comment on lines +538 to +539
Called when [`pop_views_until`][flet.Page.pop_views_until] reaches
the destination view.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update your cross references in the modified/added docstrings to use the new style: https://github.com/flet-dev/flet/blob/main/.codex/skills/docs-conventions/SKILL.md?plain=1#L12

`route` is found, then delivers `result` via the
[`on_views_pop_until`][flet.Page.on_views_pop_until] event.

This is the Flet equivalent of Flutter's `Navigator.popUntilWithResult`.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Users dont usually have flutter knowledge, so we can remove this line.

Comment on lines +937 to +939
Pops views from the navigation stack until a view with the given
`route` is found, then delivers `result` via the
[`on_views_pop_until`][flet.Page.on_views_pop_until] event.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update cross refs here too.

CHANGELOG.md Outdated

* Add `ft.use_dialog()` hook for declarative dialog management from within `@ft.component` functions, with frozen-diff reactive updates and automatic open/close lifecycle ([#6335](https://github.com/flet-dev/flet/pull/6335)) by @FeodorFitsner.
* Add `scrollable`, `pin_leading_to_top`, and `pin_trailing_to_bottom` properties to `NavigationRail` for scrollable content with optional pinned leading/trailing controls ([#1923](https://github.com/flet-dev/flet/issues/1923), [#6356](https://github.com/flet-dev/flet/pull/6356)) by @ndonkoHenri.
* Add `Page.pop_views_until()` to pop multiple views and return a result to the destination view ([#6326](https://github.com/flet-dev/flet/issues/6326), [#6347](https://github.com/flet-dev/flet/pull/6347)) by [@brunobrown](https://github.com/brunobrown).
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Add `Page.pop_views_until()` to pop multiple views and return a result to the destination view ([#6326](https://github.com/flet-dev/flet/issues/6326), [#6347](https://github.com/flet-dev/flet/pull/6347)) by [@brunobrown](https://github.com/brunobrown).
* Add `Page.pop_views_until()` to pop multiple views and return a result to the destination view ([#6326](https://github.com/flet-dev/flet/issues/6326), [#6347](https://github.com/flet-dev/flet/pull/6347)) by @brunobrown.

Use :class:, :meth:, :attr: reST roles in docstrings instead of mkdocs-style cross-references, per docs-conventions guidelines.
Remove Flutter reference from pop_views_until docstring.
Fix CHANGELOG author format to plain.

Requested by @ndonkoHenri in PR review.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feature: Add pop_until_with_result to Page - pop multiple views and return a result

4 participants