Skip to content

[web] Update the preserved view size when the viewport gets taller than it - #194020

Merged
auto-submit[bot] merged 2 commits into
flutter:masterfrom
BiAksoy:fix-web-keyboard-stale-view-size
Oct 16, 2026
Merged

auto-submit[bot] merged 2 commits into
flutter:masterfrom
BiAksoy:fix-web-keyboard-stale-view-size

Conversation

@BiAksoy

@BiAksoy BiAksoy commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

On mobile browsers, EngineFlutterView._handleBrowserResize keeps _physicalSize unchanged while text editing is active and reports the difference to the visual viewport as viewInsets. That relies on the preserved size being the full, keyboard-less size.

It isn't when the size was measured with the keyboard already up. On Android that happens when the keyboard is opened by an input that Flutter's text editing doesn't know about, such as one inside an iframe: text editing isn't active, so the resize is handled as a regular one and the view takes the keyboard-reduced size. When a TextField then takes focus, that size is preserved, and when the keyboard is dismissed the view keeps the reduced height and viewInsets.bottom goes negative (the assert in _computeOnScreenKeyboardInsets fails in debug builds).

The keyboard can only make the viewport shorter, so a viewport that is taller than the preserved size means the preserved size is stale. This PR updates _physicalSize (and _viewPadding) in that case and keeps computing the insets the way they are computed during text editing. Every other case goes through the same assignments as before.

Measured on a physical Android phone (Android 16, Chrome 154), in CSS pixels, with the sample from the issue extended with a second, numeric TextField and a hook that exposes its readout of the view metrics to JavaScript. "Before" is master at 9b57590, "after" is this branch built with a local web SDK.

Step visualViewport.height View height, before viewInsets.bottom, before View height, after viewInsets.bottom, after
Start 692 692 0 692 0
iframe input focused 318 318 0 318 0
TextField focused 382 318 -64 382 0
Keyboard closed 692 318 -374 692 0

Sequences with only Flutter text fields, and with only the iframe input, give the same numbers before and after. An Android 14 emulator with Chrome 113 gives the same result. Safari on an iOS 27 simulator is unchanged: the view stays at 714 and the inset goes 0 → 330 → 0.

Notes for review:

  • The comparison also applies to a view embedded in a custom element. A host element that grows while text is being edited on mobile is now followed, where the size used to stay stale until editing ended. The insets of those views are always zero.
  • On the new path the insets are still computed for the editing case, so that iOS keeps reporting the keyboard (the non-editing case measures against documentElement.clientHeight there). The tests can't observe that difference, because in a desktop test browser both cases give the same value.
  • If the viewport gets taller and then shorter again while editing, e.g. when a split-screen divider is moved, the taller size stays preserved and the difference is reported as an inset until editing ends, like any other shortening during editing.

The keyboard resize behavior group in window_test.dart gets three tests that go through the browser's resize event: a shorter viewport keeps the size, a taller one updates it (this one fails on master with the negative insets assert), and a rotation still updates it.

I ran the engine test suites on Chrome with dart2js and dart2wasm. felt could not launch Firefox and Safari on my machine, so those suites will only run on CI.

Fixes #193639

Pre-launch Checklist

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

If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance.

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.

…an it

On mobile browsers, `_handleBrowserResize` keeps `_physicalSize` unchanged
while text editing is active and reports the difference to the visual viewport
as the keyboard inset. That assumes the preserved size is the full,
keyboard-less size.

The assumption doesn't hold when `_physicalSize` was measured while the
keyboard was already up. On Android this happens when the keyboard is opened by
an input that Flutter's text editing doesn't know about, such as one inside an
iframe: text editing isn't active, so the resize is handled as a regular one
and the view takes the keyboard-reduced size. Once a Flutter text field takes
focus, that size is preserved. When the keyboard is then dismissed, the
viewport grows back, but the view keeps the reduced height and
`viewInsets.bottom` becomes negative, which fails the assert in
`_computeOnScreenKeyboardInsets` in debug builds.

The keyboard can only make the viewport shorter, so a viewport that is taller
than the preserved size means the preserved size is stale. The size, and the
safe area that depends on it, is now updated in that case, while the insets are
still computed the way they are during text editing.

Fixes flutter#193639
@github-actions github-actions Bot added engine flutter/engine related. See also e: labels. platform-web Web applications specifically team-web Owned by Web platform team labels Oct 8, 2026

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request updates the browser resize handler in the Flutter Web engine to correctly update the physical size when the viewport becomes taller than the preserved size, such as when a keyboard is dismissed after being active before text editing started. It also adds unit tests to verify physical size behavior during viewport height changes and device rotations. Feedback on the changes suggests that preserving the entire physical size during mobile editing ignores width changes (e.g., during split-screen resizing), and proposes a code modification to update the width while preserving only the height.

Comment on lines +363 to 370
final bool isEditingOnMobile =
_shouldPreservePhysicalSizeOnResize && !_isRotation(newPhysicalSize);
if (!isEditingOnMobile || newPhysicalSize.height > physicalSize.height) {
_physicalSize = newPhysicalSize;
// The safe area moves with the size of the view, e.g. when the device is
// rotated, so it has to be recalculated too.
_viewPadding = dimensionsProvider.computeSafeAreaInsets();
// When physical size changes this value has to be recalculated.
_computeOnScreenKeyboardInsets(false);
}

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

When isEditingOnMobile is true, the current implementation preserves the entire _physicalSize (both width and height) unless the new height is strictly greater than the current height. However, the keyboard only affects the viewport's height, not its width.

If the viewport's width changes while editing on mobile (for example, due to split-screen resizing on a tablet/foldable, or browser window resizing during development/testing), the width change will be ignored, leading to horizontal layout clipping or distortion.

To prevent this, we should always update the width to newPhysicalSize.width, while only preserving the height if it is not taller than the current height.

    final bool isEditingOnMobile =
        _shouldPreservePhysicalSizeOnResize && !_isRotation(newPhysicalSize);
    if (!isEditingOnMobile) {
      _physicalSize = newPhysicalSize;
      _viewPadding = dimensionsProvider.computeSafeAreaInsets();
    } else {
      final double preservedHeight = _physicalSize.height;
      final double targetHeight = newPhysicalSize.height > preservedHeight
          ? newPhysicalSize.height
          : preservedHeight;
      if (newPhysicalSize.width != _physicalSize.width || targetHeight != preservedHeight) {
        _physicalSize = ui.Size(newPhysicalSize.width, targetHeight);
        _viewPadding = dimensionsProvider.computeSafeAreaInsets();
      }
    }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is how master behaves too: while text is being edited on mobile, any resize that isn't a rotation keeps the whole size, width included. This PR only adds the taller-viewport case, where both dimensions are updated.

Tracking the width separately would produce sizes that mix a new width with a preserved height and would change how the preserved size interacts with _isRotation, so I'd rather keep it out of this fix. I can look into it separately if the team thinks it's worth it.

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.

No need to change this — master already ignores width changes while editing, so that's out of scope for this fix.

@flutter-zl flutter-zl added the CICD Run CI/CD label Oct 8, 2026
@flutter-zl
flutter-zl self-requested a review October 8, 2026 21:09

@flutter-zl flutter-zl 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.

Thanks for the fix. I tested with demo apps. It works as expected.
Demo (Android Chrome: iframe input, then the TextField, then close the keyboard with Back):
- Before: https://flutter-demo-04-before.web.app
- After: https://flutter-demo-04-after.web.app

@flutter-zl
flutter-zl requested a review from mdebbar October 15, 2026 19:13
@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Oct 15, 2026
@flutter-zl flutter-zl added the CICD Run CI/CD label Oct 15, 2026

@mdebbar mdebbar 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.

Thanks for the fix!

Comment on lines +363 to 370
final bool isEditingOnMobile =
_shouldPreservePhysicalSizeOnResize && !_isRotation(newPhysicalSize);
if (!isEditingOnMobile || newPhysicalSize.height > physicalSize.height) {
_physicalSize = newPhysicalSize;
// The safe area moves with the size of the view, e.g. when the device is
// rotated, so it has to be recalculated too.
_viewPadding = dimensionsProvider.computeSafeAreaInsets();
// When physical size changes this value has to be recalculated.
_computeOnScreenKeyboardInsets(false);
}

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.

No need to change this — master already ignores width changes while editing, so that's out of scope for this fix.

@mdebbar mdebbar added the autosubmit Merge PR when tree becomes green via auto submit App label Oct 16, 2026
@auto-submit
auto-submit Bot added this pull request to the merge queue Oct 16, 2026
Merged via the queue into flutter:master with commit c59df99 Oct 16, 2026
41 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Oct 16, 2026
suryaKommana2662 pushed a commit to suryaKommana2662/flutter that referenced this pull request Oct 29, 2026
…an it (flutter#194020)

On mobile browsers, `EngineFlutterView._handleBrowserResize` keeps
`_physicalSize` unchanged while text editing is active and reports the
difference to the visual viewport as `viewInsets`. That relies on the
preserved size being the full, keyboard-less size.

It isn't when the size was measured with the keyboard already up. On
Android that happens when the keyboard is opened by an input that
Flutter's text editing doesn't know about, such as one inside an iframe:
text editing isn't active, so the resize is handled as a regular one and
the view takes the keyboard-reduced size. When a `TextField` then takes
focus, that size is preserved, and when the keyboard is dismissed the
view keeps the reduced height and `viewInsets.bottom` goes negative (the
assert in `_computeOnScreenKeyboardInsets` fails in debug builds).

The keyboard can only make the viewport shorter, so a viewport that is
taller than the preserved size means the preserved size is stale. This
PR updates `_physicalSize` (and `_viewPadding`) in that case and keeps
computing the insets the way they are computed during text editing.
Every other case goes through the same assignments as before.

Measured on a physical Android phone (Android 16, Chrome 154), in CSS
pixels, with the sample from the issue extended with a second, numeric
`TextField` and a hook that exposes its readout of the view metrics to
JavaScript. "Before" is master at 9b57590, "after" is this branch
built with a local web SDK.

| Step | `visualViewport.height` | View height, before |
`viewInsets.bottom`, before | View height, after | `viewInsets.bottom`,
after |
| --- | --- | --- | --- | --- | --- |
| Start | 692 | 692 | 0 | 692 | 0 |
| iframe input focused | 318 | 318 | 0 | 318 | 0 |
| `TextField` focused | 382 | 318 | -64 | 382 | 0 |
| Keyboard closed | 692 | 318 | -374 | 692 | 0 |

Sequences with only Flutter text fields, and with only the iframe input,
give the same numbers before and after. An Android 14 emulator with
Chrome 113 gives the same result. Safari on an iOS 27 simulator is
unchanged: the view stays at 714 and the inset goes 0 → 330 → 0.

Notes for review:

- The comparison also applies to a view embedded in a custom element. A
host element that grows while text is being edited on mobile is now
followed, where the size used to stay stale until editing ended. The
insets of those views are always zero.
- On the new path the insets are still computed for the editing case, so
that iOS keeps reporting the keyboard (the non-editing case measures
against `documentElement.clientHeight` there). The tests can't observe
that difference, because in a desktop test browser both cases give the
same value.
- If the viewport gets taller and then shorter again while editing, e.g.
when a split-screen divider is moved, the taller size stays preserved
and the difference is reported as an inset until editing ends, like any
other shortening during editing.

The `keyboard resize behavior` group in `window_test.dart` gets three
tests that go through the browser's resize event: a shorter viewport
keeps the size, a taller one updates it (this one fails on master with
the negative insets assert), and a rotation still updates it.

I ran the `engine` test suites on Chrome with dart2js and dart2wasm.
`felt` could not launch Firefox and Safari on my machine, so those
suites will only run on CI.

Fixes flutter#193639

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [AI contribution guidelines] and understand my
responsibilities, or I am not using AI tools.
- [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 in-code documentation (doc comments with
`///`).
- [x] If this PR introduces a new feature or capability, I created and
linked a website documentation issue or PR in [flutter/website] (or
verified none is needed).
- [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].

If this change needs to override an active code freeze, provide a
comment explaining why. The code freeze workflow can be overridden by
code reviewers. See pinned issues for any active code freezes with
guidance.

**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
[AI contribution guidelines]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines
[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
[flutter/website]: https://github.com/flutter/website
[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

Co-authored-by: zhongliugo <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD engine flutter/engine related. See also e: labels. platform-web Web applications specifically team-web Owned by Web platform team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[web][Android] View keeps a keyboard-reduced height and reports negative viewInsets after focus moves from an iframe input to a TextField

3 participants