Skip to content

Add alwaysSizeToContent argument to Overlay.#182009

Merged
knopp merged 2 commits into
flutter:masterfrom
knopp:overlay_always_size_to_content
Mar 30, 2026
Merged

Add alwaysSizeToContent argument to Overlay.#182009
knopp merged 2 commits into
flutter:masterfrom
knopp:overlay_always_size_to_content

Conversation

@knopp
Copy link
Copy Markdown
Member

@knopp knopp commented Feb 6, 2026

Fixes #182008.

This PR introduces alwaysSizeToContent argument to Overlay. When set to true, this ensures the Overlay
is always sized to content, even if the constraints are finite.

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

@github-actions github-actions Bot added the framework flutter/packages/flutter repository. See also f: labels. label Feb 6, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces an alwaysSizeToContent argument to the Overlay widget. When this is set to true, the overlay will size itself to its content even when given finite constraints, which is useful for scenarios where an overlay should wrap its content. The implementation correctly propagates this new property down to the render object and adds a new, specific error message for when alwaysSizeToContent is true but no suitable child is available to determine the size. The changes are accompanied by relevant tests. My feedback includes a minor documentation typo fix and a suggestion to make the new error message more actionable for developers.

Comment thread packages/flutter/lib/src/widgets/overlay.dart Outdated
Comment on lines +1500 to +1506
ErrorDescription(
'When created with alwaysSizeToContent=true the Overlay requires at least one '
'non-positioned OverlayEntry with canSizeOverlay set to true',
),
ErrorHint(
'Try removing alwaysSizeToContent=true or provide a suitable child that can size the Overlay',
),
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.

medium

The error description and hint could be more precise and actionable to better guide the developer, in line with the style guide's principle of providing useful error messages.

        ErrorDescription(
          'When `alwaysSizeToContent` is true, the Overlay requires at least one '
          'non-positioned `OverlayEntry` with `canSizeOverlay` set to true to determine its size.',
        ),
        ErrorHint(
          'Try removing `alwaysSizeToContent: true`, or add an `OverlayEntry` with `canSizeOverlay: true` that is not a `Positioned` widget.',
        ),
References
  1. Error messages should be useful. This change makes the error message more actionable and easier to understand for developers. (link)

///
/// Normally overlay will only size itself to content if the the incoming
/// constraints are infinite and there is a [OverlayEntry] that can size
/// the overlay. Setting this to `true` will force this behavior even for
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.

also mention [OverlayEntry.canSizeOverlay] set to true

///
/// Setting this to true requires an [OverlayEntry] that can size the overlay
/// based on itself.
final bool alwaysSizeToContent;
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.

does it worth to make this a tri state? either enum or bool?, where always size by content, always not size by content, and size depends on infinite constraint

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Not sure, if there is consensus tri-state would be better I'll can rework this.

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.

thinking a bit more, I think this is fine for now.

/// the overlay. Setting this to `true` will force this behavior even for
/// finite (but possibly loose) constraints.
///
/// Setting this to true requires an [OverlayEntry] that can size the overlay
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.

should also mention it throws when there isn't a overlay entry that can size the overlay

@justinmc justinmc self-requested a review February 10, 2026 23:14
@knopp knopp force-pushed the overlay_always_size_to_content branch from 74cc34f to f83dee7 Compare February 11, 2026 16:07
@chunhtai chunhtai self-requested a review February 11, 2026 16:29
Copy link
Copy Markdown
Contributor

@chunhtai chunhtai left a comment

Choose a reason for hiding this comment

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

besides the doc related comments, otherwise LGTM

///
/// Setting this to true requires an [OverlayEntry] that can size the overlay
/// based on itself.
final bool alwaysSizeToContent;
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.

thinking a bit more, I think this is fine for now.

@Piinks
Copy link
Copy Markdown
Contributor

Piinks commented Mar 17, 2026

Greetings from triage! Is this a change we want to move forward with?

@knopp knopp force-pushed the overlay_always_size_to_content branch from f83dee7 to 65cb9e2 Compare March 30, 2026 15:25
@knopp knopp enabled auto-merge March 30, 2026 15:25
@knopp knopp added the CICD Run CI/CD label Mar 30, 2026
@knopp knopp added this pull request to the merge queue Mar 30, 2026
Merged via the queue into flutter:master with commit 939c21c Mar 30, 2026
76 of 77 checks passed
@knopp knopp deleted the overlay_always_size_to_content branch March 30, 2026 16:42
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 30, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 30, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 30, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 30, 2026
bkonyi pushed a commit to bkonyi/flutter that referenced this pull request Mar 30, 2026
Fixes flutter#182008.

This PR introduces `alwaysSizeToContent` argument to `Overlay`. When set
to true, this ensures the Overlay
is always sized to content, even if the constraints are finite.

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 31, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 31, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 31, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 1, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 1, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 1, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 1, 2026
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request Apr 2, 2026
Roll Flutter from fb03253e32ce to 3d69471c0bf9 (69 revisions)

flutter/flutter@fb03253...3d69471

2026-04-01 engine-flutter-autoroll@skia.org Roll Dart SDK from 1308a3076402 to 043a2bfd56ff (1 revision) (flutter/flutter#184453)
2026-04-01 engine-flutter-autoroll@skia.org Roll Skia from a657b5446209 to c2363c39c283 (2 revisions) (flutter/flutter#184448)
2026-04-01 104349824+huycozy@users.noreply.github.com Fix layout overflowed in small screen in SensitiveContent's example (flutter/flutter#184179)
2026-04-01 engine-flutter-autoroll@skia.org Roll Skia from e0b25041a5d5 to a657b5446209 (1 revision) (flutter/flutter#184445)
2026-04-01 engine-flutter-autoroll@skia.org Roll Dart SDK from 75a089eb6bf9 to 1308a3076402 (3 revisions) (flutter/flutter#184444)
2026-04-01 jesswon@google.com [AGP 9] Bumping KGP error minimum to 2.0.0 (flutter/flutter#184385)
2026-04-01 bkonyi@google.com [ Tool ] Migrate `flutter analyze` to use LSP (flutter/flutter#183785)
2026-04-01 30870216+gaaclarke@users.noreply.github.com Adds uber sdf shader gradients with blend (flutter/flutter#184090)
2026-04-01 sys.int64@gmail.com Add bottom safe area padding to licenses package license page (flutter/flutter#182425)
2026-04-01 ahmedsameha1@gmail.com Handle#6537 third grouped tests (flutter/flutter#183059)
2026-04-01 engine-flutter-autoroll@skia.org Roll Skia from f37239a7a689 to e0b25041a5d5 (9 revisions) (flutter/flutter#184436)
2026-03-31 jason-simmons@users.noreply.github.com [Impeller] Do not log an error when wrapping an empty texture as a TextureGLES (flutter/flutter#184377)
2026-03-31 jason-simmons@users.noreply.github.com Remove the default_git_folder GN argument (flutter/flutter#184152)
2026-03-31 jason-simmons@users.noreply.github.com Remove the cupertino_icons dependency from the spell_check integration test (flutter/flutter#184398)
2026-03-31 engine-flutter-autoroll@skia.org Roll Fuchsia Linux SDK from pM94cWC9cSgao0CG0... to fV-JIWUt4FQGeDtEe... (flutter/flutter#184383)
2026-03-31 engine-flutter-autoroll@skia.org Roll Dart SDK from eaeccf98848d to 75a089eb6bf9 (1 revision) (flutter/flutter#184379)
2026-03-31 mdebbar@google.com [web] Fix autofill in iOS 26 Safari (flutter/flutter#182024)
2026-03-31 engine-flutter-autoroll@skia.org Roll Fuchsia GN SDK from SEfYx3xgueX3aFAY3... to JLBh4Z9PKsjIJcqDU... (flutter/flutter#184368)
2026-03-31 loic.peron@inetum.com [Windows] Restore and enable IAccessibleEx implementation (flutter/flutter#175406)
2026-03-31 30870216+gaaclarke@users.noreply.github.com Revert "Even more awaits (#184042)" (flutter/flutter#184429)
2026-03-31 engine-flutter-autoroll@skia.org Roll Skia from dfd8f8002800 to f37239a7a689 (2 revisions) (flutter/flutter#184374)
2026-03-31 jacksongardner@google.com Remove workaround for fake impeller images in iOS simulator. (flutter/flutter#184264)
2026-03-31 victorsanniay@gmail.com Even more awaits (flutter/flutter#184042)
2026-03-31 engine-flutter-autoroll@skia.org Roll Packages from 582f0e7 to b04f3e5 (6 revisions) (flutter/flutter#184393)
2026-03-30 30870216+gaaclarke@users.noreply.github.com Fixes a flake in reload shaders tests (flutter/flutter#184268)
2026-03-30 jason-simmons@users.noreply.github.com Remove an obsolete script for setting up remote GDB sessions on Android devices (flutter/flutter#184357)
2026-03-30 engine-flutter-autoroll@skia.org Roll Skia from 8dcde79fef2a to dfd8f8002800 (10 revisions) (flutter/flutter#184363)
2026-03-30 engine-flutter-autoroll@skia.org Roll Dart SDK from 0aaccc3c8004 to eaeccf98848d (2 revisions) (flutter/flutter#184362)
2026-03-30 bkonyi@google.com [ Tool ] Remove `flutter running-apps` command (flutter/flutter#183742)
2026-03-30 137456488+flutter-pub-roller-bot@users.noreply.github.com Roll pub packages (flutter/flutter#184045)
2026-03-30 jmccandless@google.com Rick roll triagers on/near April 1st (flutter/flutter#184355)
2026-03-30 49699333+dependabot[bot]@users.noreply.github.com Bump codecov/codecov-action from 5.5.0 to 6.0.0 in the all-github-actions group (flutter/flutter#184364)
2026-03-30 1961493+harryterkelsen@users.noreply.github.com fix(web): call ui.Picture.onDispose for the original picture only (flutter/flutter#184348)
2026-03-30 engine-flutter-autoroll@skia.org Roll Skia from e001e6901e3b to 8dcde79fef2a (7 revisions) (flutter/flutter#184356)
2026-03-30 jason-simmons@users.noreply.github.com [web] Make it safe to call dispose multiple times on a CkSurface (flutter/flutter#184270)
2026-03-30 jason-simmons@users.noreply.github.com Roll HarfBuzz to 13.2.1 (flutter/flutter#184210)
2026-03-30 srawlins@google.com web_ui: Remove unused parameters in a few places (flutter/flutter#183156)
2026-03-30 saurabhmirajkar000@gmail.com Update TabBar documentation to clarify indicatorWeight behavior (flutter/flutter#184104)
2026-03-30 36861262+QuncCccccc@users.noreply.github.com Add title evaluation (flutter/flutter#184084)
2026-03-30 47866232+chunhtai@users.noreply.github.com fixes crash when invisible semantics nodes dropped from semantics tree (flutter/flutter#184226)
2026-03-30 engine-flutter-autoroll@skia.org Roll Skia from cdaae3e3fdef to e001e6901e3b (4 revisions) (flutter/flutter#184345)
2026-03-30 engine-flutter-autoroll@skia.org Roll Packages from 7ae082a to 582f0e7 (8 revisions) (flutter/flutter#184341)
2026-03-30 matej.knopp@gmail.com Add alwaysSizeToContent argument to Overlay. (flutter/flutter#182009)
2026-03-30 engine-flutter-autoroll@skia.org Roll Dart SDK from 598088a8a67f to 0aaccc3c8004 (1 revision) (flutter/flutter#184331)
2026-03-30 engine-flutter-autoroll@skia.org Roll Skia from 6d7ade938643 to cdaae3e3fdef (2 revisions) (flutter/flutter#184329)
2026-03-30 engine-flutter-autoroll@skia.org Roll Fuchsia Linux SDK from EnoD6zNQebz4EYuLk... to pM94cWC9cSgao0CG0... (flutter/flutter#184323)
...
ahmedsameha1 pushed a commit to ahmedsameha1/flutter that referenced this pull request Apr 14, 2026
Fixes flutter#182008.

This PR introduces `alwaysSizeToContent` argument to `Overlay`. When set
to true, this ensures the Overlay
is always sized to content, even if the constraints are finite.

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Overlay can't size to child when the constraints are loose

4 participants