Skip to content

feat: ft.use_page_size() hook; Dropdown fills the width in stretching parents - #7115

Open
FeodorFitsner wants to merge 2 commits into
flet-1.2from
feature/page-size-hook
Open

FeodorFitsner wants to merge 2 commits into
flet-1.2from
feature/page-size-hook

Conversation

@FeodorFitsner

@FeodorFitsner FeodorFitsner commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Description

Two layout gaps that came up while building a reference app for the Flet Studio agent. Both are things every adaptive app needs.

ft.use_page_size()

Until now, a component that adapts to the window width (a bottom bar on phones, a rail on desktop) had to set page.on_resize in an effect. on_resize holds one handler, so the app needed a context to share the result, and any component that set it replaced someone else's handler.

size = ft.use_page_size()                                  # Size; re-renders on every resize
compact = ft.use_page_size(lambda size: size.width < 600)  # bool; re-renders only when it flips
  • BasePage keeps a list of resize listeners and calls them from before_event for PageResizeEvent. The client already sends resize to Python whether or not on_resize is set (it is debounced to 100 ms), so page.on_resize keeps working alongside the hook.
  • With select, the selected value lives in use_state, so a resize that doesn't change it doesn't re-render. A new select on a later render (a different breakpoint) applies at once, from the last known size.
  • The effect catches up with any resize between the first render and the subscription, and its returned cleanup removes the listener on unmount.
  • Typed with overloads: use_page_size() is Size, and use_page_size(lambda s: s.width < 600) is bool (checked with mypy and pyright).

Dropdown in a stretched column

Flutter's DropdownMenu sizes itself to its widest option and aligns the field to the start under a tight width. So a Dropdown in a Column(horizontal_alignment=STRETCH) stayed narrow while the TextFields around it filled the form. The only way to fill was expand=True, which in a Column also makes it Expanded vertically.

Now a Dropdown with no width and no expand passes expandedInsets: EdgeInsets.zero when its parent stretches it: a Column or View with horizontal_alignment=STRETCH, or a vertical ListView. Control.parent already skips component wrappers, so this works in declarative code too.

This is decided from the parent's properties, the same direct-parent rule expand uses (host_expanded), and not from a LayoutBuilder. AlertDialog measures its content with IntrinsicWidth, which a LayoutBuilder can't answer, so that approach would break dropdowns inside dialogs.

This changes how existing apps look: a dropdown that is a direct child of a stretched column or a ListView gets wider. It's in the 1.2.0 changelog.

Not changed: TextField's 300 px default

I looked at making TextField fill its column's width by default, as Flutter does. That would widen every page-level TextField in existing apps (page.add(ft.TextField()) sits in the page's start-aligned column), so I left it alone. horizontal_alignment=STRETCH already makes it fill, and with this PR the same setting now covers Dropdown as well.

Test code

import flet as ft

OPTS = [ft.DropdownOption(k, t) for k, t in [("a", "Apple"), ("b", "Banana")]]


def dd(label, **kw):
    return ft.Dropdown(label=label, options=OPTS, value="a", **kw)


@ft.component
def SizeBadge():
    size = ft.use_page_size()
    compact = ft.use_page_size(lambda s: s.width < 600)
    return ft.Text(f"{int(size.width)}x{int(size.height)} compact={compact}")


@ft.component
def App():
    show, set_show = ft.use_state(False)
    ft.use_dialog(
        ft.AlertDialog(
            title=ft.Text("Dialog"),
            content=dd("In dialog"),
            actions=[ft.TextButton("Close", on_click=lambda: set_show(False))],
        )
        if show
        else None
    )
    return ft.Column(
        scroll=ft.ScrollMode.AUTO,
        expand=True,
        controls=[
            SizeBadge(),
            ft.Column(
                horizontal_alignment=ft.CrossAxisAlignment.STRETCH,
                controls=[ft.TextField(label="TextField"), dd("Stretched"), dd("width=200", width=200)],
            ),
            ft.Column(controls=[dd("Start")]),
            ft.ListView(height=70, controls=[dd("In ListView")]),
            ft.Button("Open dialog", on_click=lambda: set_show(True)),
        ],
    )


ft.run(lambda page: page.render(App))

Checked with the local Flutter web client from this branch (flutter run -d web-server) in headless Chrome:

  • At 900 px the badge reads 900x760 compact=False; at 420 px it reads 420x760 compact=True.
  • "Stretched" and "In ListView" fill the width, and so does the open menu.
  • "width=200" and "Start" keep their own width.
  • The dropdown in the AlertDialog renders at its own width, with no errors in either log.

Tests

tests/test_use_page_size.py:

  • the size and a re-render on resize;
  • select re-rendering only when its value flips;
  • a new select applying without a resize;
  • the real resize event path reaching several components, with and without on_resize (the handler still runs);
  • unmount removing the listener;
  • a missing size reading as 0.

Full suite: 565 passed, 9 skipped.

Summary by Sourcery

Improve adaptive layouts with reusable page-size subscriptions and width-aware Dropdown sizing.

New Features:

  • Add the ft.use_page_size() hook for responsive components to observe page dimensions or selected breakpoint values without taking over page.on_resize.

Enhancements:

  • Make width-less Dropdown controls fill stretched parents while preserving explicit widths and intrinsic sizing elsewhere.

Documentation:

  • Document the new use_page_size hook and add it to the documentation navigation.
  • Record the new hook and stretched-parent Dropdown behavior in the 1.2.0 changelog.

Tests:

  • Add coverage for page-size reads, resize-driven updates, breakpoint selection, listener cleanup, event coexistence, and missing dimensions.

… parents

- use_page_size() returns the page Size and re-renders on resize;
  use_page_size(select) re-renders only when select(size) changes.
  Listeners hang off BasePage.before_event for "resize", so any number of
  components can use it and page.on_resize stays free.
- Dropdown without a width uses expandedInsets: zero when its parent is a
  Column/View with horizontal_alignment=STRETCH or a vertical ListView,
  matching TextField. Decided from the parent's properties, not a
  LayoutBuilder, so dropdowns in AlertDialog (IntrinsicWidth) keep working.
- Docs page for use_page_size, Dropdown docstring, 1.2.0 changelog.
@FeodorFitsner FeodorFitsner added this to the 1.2.0 milestone Nov 21, 2026

@sourcery-ai sourcery-ai Bot left a comment

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.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying flet-website-v2 with  Cloudflare Pages  Cloudflare Pages

Latest commit: 1820292
Status:🚫  Build failed.

View logs

This branch has not been deployed

No deployments
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.

1 participant