Skip to content

Bottom insets are not stable when keyboard shows on screen on Android 11 #65821

@mehmetf

Description

@mehmetf

The following output is observed with the repro code found below:

I/flutter ( 3578): isKeyboardVisible: false, bottomPadding: 0.0
I/flutter ( 5555): isKeyboardVisible: false, bottomPadding: 0.0
I/flutter ( 5555): isKeyboardVisible: true, bottomPadding: 797.0     <---- Why?
I/flutter ( 5555): isKeyboardVisible: false, bottomPadding: 0.0
I/flutter ( 5555): isKeyboardVisible: false, bottomPadding: 0.0
I/flutter ( 5555): isKeyboardVisible: true, bottomPadding: 172.0
I/flutter ( 5555): isKeyboardVisible: true, bottomPadding: 332.0
I/flutter ( 5555): isKeyboardVisible: true, bottomPadding: 445.0
I/flutter ( 5555): isKeyboardVisible: true, bottomPadding: 530.0
I/flutter ( 5555): isKeyboardVisible: true, bottomPadding: 590.0
I/flutter ( 5555): isKeyboardVisible: true, bottomPadding: 640.0
I/flutter ( 5555): isKeyboardVisible: true, bottomPadding: 675.0
I/flutter ( 5555): isKeyboardVisible: true, bottomPadding: 706.0
I/flutter ( 5555): isKeyboardVisible: true, bottomPadding: 730.0
I/flutter ( 5555): isKeyboardVisible: true, bottomPadding: 751.0
I/flutter ( 5555): isKeyboardVisible: true, bottomPadding: 766.0
I/flutter ( 5555): isKeyboardVisible: true, bottomPadding: 778.0
I/flutter ( 5555): isKeyboardVisible: true, bottomPadding: 787.0
I/flutter ( 5555): isKeyboardVisible: true, bottomPadding: 792.0
I/flutter ( 5555): isKeyboardVisible: true, bottomPadding: 795.0
I/flutter ( 5555): isKeyboardVisible: true, bottomPadding: 797.0
I/flutter ( 5555): isKeyboardVisible: true, bottomPadding: 797.0

There's a sudden jump on bottom padding to max and then a gradual increase.

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

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

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

  @override
  void didChangeMetrics() {
    final bottomPadding = WidgetsBinding.instance.window.viewInsets.bottom;
    final isKeyboardVisible = bottomPadding > 0;
    print(
      'isKeyboardVisible: $isKeyboardVisible, bottomPadding: $bottomPadding',
    );
    if (!isKeyboardVisible && focusNode.hasFocus) {
       focusNode.unfocus();
    }
  }

  final focusNode = FocusNode();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              GestureDetector(
                behavior: HitTestBehavior.translucent,
                onTap: () => focusNode.unfocus(),
                child: Container(
                  height: 200,
                  color: Colors.red,
                  child:
                      Center(child: Text('Click here to unfocus text field')),
                ),
              ),
              TextField(focusNode: focusNode),
            ],
          ),
        ));
  }
}

(The repro code does not irrelevant things like trying to control the focus. This is due to the use case the client is trying to implement and (probably?) has nothing to do with the issue itself).

IIRC this bottomPadding trick is the only way we know and recommend for detecting keyboard open/close consistently. If there's another solution, let us know.

Metadata

Metadata

Assignees

Labels

P0Critical issues such as a build break or regressioncustomer: money (g3)e: OS-version specificAffects only some versions of the relevant operating systemengineflutter/engine related. See also e: labels.found in release: 1.21Found to occur in 1.21has reproducible stepsThe issue has been confirmed reproducible and is ready to work onplatform-androidAndroid applications specifically

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions