Skip to content

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

Closed

Description

@BiAksoy

Steps to reproduce

  1. Create a web app from the code sample below (flutter create, then flutter pub add web). It has a text input inside an iframe (HtmlElementView) and a Flutter TextField. To see where the Flutter view ends, give the page a background in web/index.html, for example <body style="background:#c62828">.
  2. flutter build web --release, serve build/web, and open it in Chrome on Android.
  3. Tap the iframe input. The numeric keypad opens.
  4. Tap the Flutter TextField. The keyboard switches to the text keyboard.
  5. Close the keyboard with the system Back button or gesture. The TextField keeps focus.

The order TextField → iframe input → TextField → Back gives the same result, which is the usual order on a form that mixes Flutter fields with an embedded payment field.

Expected results

After the keyboard closes, the Flutter view fills the viewport again and viewInsets.bottom is 0.

Actual results

The Flutter view stays at the height it had while the keyboard was open for the iframe input, and viewInsets.bottom is negative. The page background shows below the view.

Measured on the page with the sample (CSS pixels; <flutter-view> is its getBoundingClientRect().height, the last two columns are what the framework sees through MediaQueryData.fromView):

Step visualViewport.height <flutter-view> height view size viewInsets.bottom
Start 692.3 692.3 384x692 0
3. iframe input focused 317.9 317.9 384x318 0
4. TextField focused 381.9 317.9 384x318 -64
5. Keyboard closed 692.3 317.9 384x318 -374

The view stays at 318 after tapping outside the field, and recovers the next time the keyboard opens for a Flutter text field.

Same result with stable 3.47.5 and with master at 4e5a092.

Not triggered by:

  • Flutter fields only (TextField → Back): the view stays at 692.
  • The iframe input only (iframe input → Back): the view follows the viewport, 318 and back to 692.

Cause

_handleBrowserResize keeps _physicalSize unchanged and reports the difference as viewInsets while isMobile && textEditing.isEditing. That relies on the preserved size being the full, keyboard-less size, the expectation #179581 describes:

bool get _shouldPreservePhysicalSizeOnResize => isMobile && textEditing.isEditing;

void _handleBrowserResize(ui.Size? _) {
StyleManager.scaleSemanticsHost(dom.semanticsHost, devicePixelRatio);
final ui.Size newPhysicalSize = _computePhysicalSize();
if (_shouldPreservePhysicalSizeOnResize && !_isRotation(newPhysicalSize)) {
_computeOnScreenKeyboardInsets(true);
} else {
_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);
}
platformDispatcher.invokeOnMetricsChanged();
}

That assumption does not hold when the keyboard is already open at the moment editing starts:

  • Step 3: the iframe input has focus, so Flutter's text editing is not active. The resize caused by the keyboard is handled as a real resize and _physicalSize becomes the keyboard-reduced size (318).
  • Step 4: the TextField gets focus and isEditing becomes true. From here _physicalSize is preserved at 318, and the inset is computed against it: 318 - 382 = -64 instead of 692 - 382 = 310.
  • Step 5: the viewport returns to 692 while the field still has focus, so the size is still preserved and the inset becomes 318 - 692 = -374.

Where it happens

  • A physical Android phone (Android 16, Chrome 154.0.8037.57): the numbers and screenshots here.
  • An Android 14 emulator (API 34) with Chrome 113 and Chromium 157: same result. There the iframe's keypad is shorter than the text keyboard, so the inset at step 4 is positive but too small (44 instead of 274) and turns negative at step 5.
  • iOS is not affected. In Safari on an iOS 27 simulator the view stays at 714 through the same steps and the inset goes 0 → 330 → 0.
  • I first saw it with Flutter 3.44.0 on a form that has a Stripe card field (an iframe) next to Flutter text fields.

Related

Possible fix

While the size is preserved, a viewport taller than _physicalSize cannot be explained by a keyboard, so that resize could update _physicalSize instead of producing a negative inset. Patching that condition into the compiled engine of a debug build fixes the sequence above on the same phone and leaves the Flutter-only and iframe-only sequences unchanged. I can send a PR with a test in window_test.dart if that direction sounds right.

Code sample

Code sample
import 'dart:js_interop';
import 'dart:ui_web' as ui_web;

import 'package:flutter/material.dart';
import 'package:web/web.dart' as web;

void main() {
  // A non-Flutter text input, hosted in an iframe the way payment fields,
  // captchas and embedded forms are.
  ui_web.platformViewRegistry.registerViewFactory('iframe-input', (int id) {
    return web.HTMLIFrameElement()
      ..style.border = '0'
      ..style.width = '100%'
      ..style.height = '100%'
      ..srcdoc =
          '<input inputmode="numeric" placeholder="iframe input" '
                  'style="width:90%;height:56px;font-size:20px">'
              .toJS;
  });
  runApp(const App());
}

class App extends StatefulWidget {
  const App({super.key});

  @override
  State<App> createState() => _AppState();
}

class _AppState extends State<App> with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  // Scaffold hides the bottom inset from its body, so listen to the view
  // metrics directly to keep the readout current.
  @override
  void didChangeMetrics() => setState(() {});

  @override
  Widget build(BuildContext context) {
    final MediaQueryData mq = MediaQueryData.fromView(View.of(context));
    final String metrics =
        'size=${mq.size.width.toStringAsFixed(0)}x'
        '${mq.size.height.toStringAsFixed(0)} '
        'insets.bottom=${mq.viewInsets.bottom.toStringAsFixed(0)}';
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        backgroundColor: Colors.white,
        body: Column(
          children: <Widget>[
            const SizedBox(
              height: 100,
              child: HtmlElementView(viewType: 'iframe-input'),
            ),
            const SizedBox(
              height: 80,
              child: Padding(
                padding: EdgeInsets.symmetric(horizontal: 16),
                child: TextField(
                  decoration: InputDecoration(labelText: 'Flutter TextField'),
                ),
              ),
            ),
            Text(metrics),
            const Spacer(),
            Container(
              height: 48,
              width: double.infinity,
              color: Colors.green,
              alignment: Alignment.center,
              child: const Text('bottom of the Flutter view'),
            ),
          ],
        ),
      ),
    );
  }
}

Screenshots or Video

Screenshots

Steps 3, 4 and 5. The green bar marks the bottom of the Flutter view and the red area is the page background. In step 4 the gap shows above the keyboard; in step 5 the keyboard is closed.

Image Image Image

Logs

Logs

With flutter build web --debug, steps 4 and 5 fail the engine's own assert in _computeOnScreenKeyboardInsets, before invokeOnMetricsChanged runs, so the framework is not told about those resizes:

Assertion failed: "ViewInsets cannot be negative. This is usually caused by an incorrect physicalSize calculation when the keyboard is being dismissed."
    at Object.wrapException (http://localhost:8765/main.dart.js:8388:47)
    at Object.assertThrow (http://localhost:8765/main.dart.js:8887:15)
    at EngineFlutterWindow._computeOnScreenKeyboardInsets$1 (http://localhost:8765/main.dart.js:70952:11)
    at EngineFlutterWindow._handleBrowserResize$1 (http://localhost:8765/main.dart.js:70923:15)

In a release build the assert is compiled out and the framework receives the negative inset shown in the table.

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.47.5, on macOS 27.0.1 26A434 darwin-arm64, locale tr-TR) [508ms]
    • Flutter version 3.47.5 on channel stable at /Users/bilalaksoy/development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 6a19cca564 (2 weeks ago), 2026-09-17 14:13:22 -0400
    • Engine revision af7e796e16
    • Dart version 3.13.4
    • DevTools version 2.60.0
    • Feature flags: enable-web, enable-linux-desktop, enable-macos-desktop, enable-windows-desktop, enable-android, enable-ios, cli-animations, enable-native-assets, enable-record-use, enable-swift-package-manager, omit-legacy-version-file, enable-lldb-debugging, enable-uiscene-migration

[✓] Android toolchain - develop for Android devices (Android SDK version 36.0.0) [1.807ms]
    • Android SDK at /Users/bilalaksoy/Library/Android/sdk
    • Emulator version 37.1.11.0 (build_id 15917651) (CL:N/A)
    • Platform android-36, build-tools 36.0.0
    • ANDROID_HOME = /Users/bilalaksoy/Library/Android/sdk
    • Java binary at: /Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home/bin/java
      This JDK is specified in your Flutter configuration.
      To change the current JDK, run: `flutter config --jdk-dir="path/to/jdk"`.
    • Java version OpenJDK Runtime Environment Temurin-17.0.20.1+1 (build 17.0.20.1+1)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 27.0) [1.080ms]
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 27A266a
    • CocoaPods version 1.17.0

[✓] Chrome - develop for the web [6ms]
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Connected device (3 available) [6,3s]
    • sdk gphone64 arm64 (mobile) • emulator-5554 • android-arm64  • Android 14 (API 34) (emulator)
    • macOS (desktop)             • macos         • darwin-arm64   • macOS 27.0.1 26A434 darwin-arm64
    • Chrome (web)                • chrome        • web-javascript • Google Chrome 153.0.8010.53
    ! Error: Browsing on the local area network for iPhone. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)

[✓] Network resources [433ms]
    • All expected network resources are available.

• No issues found!

Also reproduced with master: Flutter 3.49.0-0.1.pre, framework and engine revision 4e5a092 (2026-10-01), Dart 3.14.0-271.0.dev.

Activity

  1. added
    a: text input
    c: crash
    platform-android
    engine
    platform-web
    and removed
    platform-android
    on Oct 1, 2026
  2. mbcorona commented on Oct 1, 2026

    @mbcorona
    Member

    Hi @BiAksoy , thanks for the report.
    This also looks like the reproduction #187345 was closed for lacking, and a path the #179581 fix does not cover.
    Routing to team-web.

  3. added
    team-web
    on Oct 1, 2026
  4. self-assigned this
    on Oct 7, 2026
  5. added
    triaged-web
    P1
    on Oct 7, 2026
  6. BiAksoy commented on Oct 8, 2026

    @BiAksoy
    ContributorAuthor

    @flutter-zl thanks for picking this up. I offered a PR in the report: if it helps, I can still send one. If you're already on it, I'll leave it to you.

  7. flutter-zl commented on Oct 8, 2026

    @flutter-zl
    Contributor

    @flutter-zl thanks for picking this up. I offered a PR in the report: if it helps, I can still send one. If you're already on it, I'll leave it to you.

    thanks for the detailed report and the root cause analysis. Your direction sounds right to me, please go ahead and send the PR.

  8. added a commit that references this issue on Oct 16, 2026
    c59df99
  9. added a commit that references this issue on Oct 29, 2026
    317d041
  10. github-actions commented on Oct 30, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  11. locked as resolved and limited conversation to collaborators on Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P1a: text inputc: crashengineplatform-webteam-webtriaged-web

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions