Repository navigation
feat: ft.use_page_size() hook; Dropdown fills the width in stretching parents - #7115
Open
FeodorFitsner wants to merge 2 commits into
Open
FeodorFitsner wants to merge 2 commits into
FeodorFitsner wants to merge 2 commits into
Conversation
… 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.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_resizein an effect.on_resizeholds one handler, so the app needed a context to share the result, and any component that set it replaced someone else's handler.BasePagekeeps a list of resize listeners and calls them frombefore_eventforPageResizeEvent. The client already sendsresizeto Python whether or noton_resizeis set (it is debounced to 100 ms), sopage.on_resizekeeps working alongside the hook.select, the selected value lives inuse_state, so a resize that doesn't change it doesn't re-render. A newselecton a later render (a different breakpoint) applies at once, from the last known size.use_page_size()isSize, anduse_page_size(lambda s: s.width < 600)isbool(checked with mypy and pyright).Dropdownin a stretched columnFlutter's
DropdownMenusizes itself to its widest option and aligns the field to the start under a tight width. So aDropdownin aColumn(horizontal_alignment=STRETCH)stayed narrow while theTextFields around it filled the form. The only way to fill wasexpand=True, which in aColumnalso makes itExpandedvertically.Now a
Dropdownwith nowidthand noexpandpassesexpandedInsets: EdgeInsets.zerowhen its parent stretches it: aColumnorViewwithhorizontal_alignment=STRETCH, or a verticalListView.Control.parentalready skips component wrappers, so this works in declarative code too.This is decided from the parent's properties, the same direct-parent rule
expanduses (host_expanded), and not from aLayoutBuilder.AlertDialogmeasures its content withIntrinsicWidth, which aLayoutBuildercan'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
ListViewgets wider. It's in the 1.2.0 changelog.Not changed:
TextField's 300 px defaultI looked at making
TextFieldfill its column's width by default, as Flutter does. That would widen every page-levelTextFieldin existing apps (page.add(ft.TextField())sits in the page's start-aligned column), so I left it alone.horizontal_alignment=STRETCHalready makes it fill, and with this PR the same setting now coversDropdownas well.Test code
Checked with the local Flutter web client from this branch (
flutter run -d web-server) in headless Chrome:900x760 compact=False; at 420 px it reads420x760 compact=True.AlertDialogrenders at its own width, with no errors in either log.Tests
tests/test_use_page_size.py:selectre-rendering only when its value flips;selectapplying without a resize;resizeevent path reaching several components, with and withouton_resize(the handler still runs);Full suite: 565 passed, 9 skipped.
Summary by Sourcery
Improve adaptive layouts with reusable page-size subscriptions and width-aware Dropdown sizing.
New Features:
ft.use_page_size()hook for responsive components to observe page dimensions or selected breakpoint values without taking overpage.on_resize.Enhancements:
Documentation:
use_page_sizehook and add it to the documentation navigation.Tests: