MCPcopy
hub / github.com/flet-dev/flet / Router

Function Router

sdk/python/packages/flet/src/flet/components/router.py:594–902  ·  view source on GitHub ↗

Top-level router component that matches the current page route against a tree of :class:`~flet.Route` definitions and renders the matched component chain. The Router subscribes to :attr:`~flet.Page.on_route_change` and re-renders automatically when the route changes. Navig

(
    routes: list[Route],
    not_found: Callable | None = None,
    manage_views: bool = False,
)

Source from the content-addressed store, hash-verified

592
593@component
594def Router(
595 routes: list[Route],
596 not_found: Callable | None = None,
597 manage_views: bool = False,
598) -> Control:
599 """
600 Top-level router component that matches the current page route against
601 a tree of :class:`~flet.Route` definitions and renders the matched
602 component chain.
603
604 The Router subscribes to :attr:`~flet.Page.on_route_change`
605 and re-renders automatically when the route changes.
606
607 Navigation is done via :meth:`~flet.Page.push_route` or
608 :meth:`~flet.Page.navigate`.
609
610 When `manage_views` is `True`, the Router returns a list of
611 :class:`~flet.View` objects (one per path level) instead of a single
612 component tree. This enables swipe-back gestures, system back button,
613 and :class:`~flet.AppBar` implicit back button on mobile.
614 Must be used with :meth:`~flet.Page.render_views`.
615
616 Args:
617 routes: List of top-level :class:`~flet.Route` definitions.
618 not_found: Optional component to render when no route matches (404).
619 manage_views: When `True`, produce a list of
620 :class:`~flet.View` objects (one per path level) instead of a
621 single component tree. Route components should return
622 :class:`~flet.View` instances with `route` and `appbar`
623 set. Use with :meth:`~flet.Page.render_views`.
624
625 Example:
626 ```python
627 @ft.component
628 def App():
629 return ft.Router(
630 [
631 ft.Route(index=True, component=Home),
632 ft.Route(path="about", component=About),
633 ft.Route(path="products/:pid", component=ProductDetails),
634 ],
635 manage_views=True,
636 )
637 ```
638 """
639 page = context.page
640 location, set_location = use_state(page.route or "/")
641 prev_route_handler_ref = use_ref(None)
642 prev_pop_handler_ref = use_ref(None)
643 # Last non-modal location the Router saw. Used as the base for
644 # global modal routes (declared at the top level) — when the user
645 # navigates from /apps/X to /settings (modal=True), the modal is
646 # rendered ON TOP OF the chain matched against /apps/X, so closing
647 # it reveals the original stack instead of rebuilding it.
648 prev_non_modal_location_ref = use_ref(None)
649 # Snapshot of the chain emitted for the current location. Used by
650 # the default pop handler to compute the parent URL structurally
651 # (`chain[-2].resolved_path`), which survives shared `view.route`

Callers

nothing calls this directly

Calls 13

use_stateFunction · 0.90
use_refFunction · 0.90
use_effectFunction · 0.90
ViewClass · 0.90
_match_routesFunction · 0.85
LocationInfoClass · 0.85
_build_outlet_chainFunction · 0.85
build_view_contentFunction · 0.85
indexMethod · 0.80
appendMethod · 0.80
_normalize_pathFunction · 0.70

Tested by

no test coverage detected