Repository: flutter
Issue: App becomes slow when screen got scrolled back by closing keyboard
Labels: framework,c: performance,f: scrolling,f: gestures,perf: speed,has reproducible steps,P2,found in release: 2.1,team-framework,triaged-framework

<!-- Thank you for using Flutter!

     If you are looking for support, please check out our documentation
     or consider asking a question on Stack Overflow:
      * https://flutter.dev/
      * https://api.flutter.dev/
      * https://stackoverflow.com/questions/tagged/flutter?sort=frequent

     If you have found a performance problem, then fill out the template below.
     Please read our guide to filing a bug first: https://flutter.dev/docs/resources/bug-reports
-->

There is an issue with widgets which react to onTapDown/onTapUp such as buttons when focussing a textfield which leads the screen to scroll. I were able to track it down to the screen being scrolled by closing the keyboard after focussing a textfield.

EDIT:
onTapDown and onTapUp get fired properly, when the bug is active, so its more a visual/performance problem. Bug appears when screen gets scrolled (dont matter in which direction) by keyboard opening. I wonder whats the difference between scrolling the screen back manually (no bug then!) and automatic scroll back by the framework. Hopefully someone can come up with a workaround/quickfix.

Here is a minimal example / reproduction of the issue including instructions to reproduce the "lag/slowdown".


<details>
<summary>code sample</summary>

```dart
import 'package:flutter/material.dart';
 
void main() {
  runApp(MyApp());
}
 
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}
 
class MyButton extends StatefulWidget {
  @override
  _MyButtonState createState() => _MyButtonState();
}
 
class _MyButtonState extends State<MyButton> {
  Color myColor = Colors.blue;
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTapDown: (_) {
        setState(() {
          myColor = Colors.red;
        });
      },
      onTapUp: (_) {
        setState(() {
          myColor = Colors.blue;
        });
      },
      onTapCancel: () {
        setState(() {
          myColor = Colors.blue;
        });
      },
      child: Container(
        alignment: Alignment.center,
        width: 200,
        height: 50,
        color: myColor,
        child: Text("Tap me"),
      ),
    );
  }
}
 
class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;
 
  @override
  _MyHomePageState createState() => _MyHomePageState();
}
 
class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
        child: SingleChildScrollView(
          child: Container(
            padding: EdgeInsets.all(50),
            height: MediaQuery.of(context).size.height,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                Text(
                  '1. Tap the button very quick, youll see it blink.\n2. Focus the textfield so the keyboard comes up.\n3. Close the keyboard\n4. Tap the button again very quick, it kinda laggs.\n\nTo fix the bug:\n1: Open the keyboard\n2: Scroll the page back down (so the keyboard overlap the screen as if it never scrolled)\n3: Close the keyboard\n4: Button works as supposed again',
                ),
                Expanded(child: SizedBox()),
                MyButton(),
                SizedBox(
                  height: 40,
                ),
                TextField(),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
```
</details>



<!--
1.  Please tell us exactly how to reproduce the problem you are running into.

2.  Please attach a small application (ideally just one main.dart file) that
     reproduces the problem. You could use https://gist.github.com/ for this.

3.  Switch flutter to master channel and run this app on a physical device
     using profile mode with Skia tracing enabled, as follows:
       flutter channel master
       flutter run --profile --trace-skia

     The bleeding edge master channel is encouraged here because Flutter is
     constantly fixing bugs and improving its performance. Your problem in an
     older Flutter version may have already been solved in the master channel.

4.  Record a video of the performance issue using another phone so we
     can have an intuitive understanding of what happened. Don’t use
     "adb screenrecord", as that affects the performance of the profile run.

5.  Open Observatory and save a timeline trace of the performance issue
     so we know which functions might be causing it. See "How to Collect
     and Read Timeline Traces" on this blog post:
       https://medium.com/flutter/profiling-flutter-applications-using-the-timeline-a1a434964af3#a499
     Make sure the performance overlay is turned OFF when recording the
     trace as that may affect the performance of the profile run.
     (Pressing ‘P’ on the command line toggles the overlay.)
-->

Only tried it on Android yet. Web isnt a problem, as there is no keyboard popping up.

**Target Platform:** Android
**Target OS version/browser:** /
**Devices:** OnePlus 6T

## Logs

<details>
<summary>Logs</summary>

<!--
     Run `flutter analyze` and attach any output of that command below.
     If there are any analysis errors, try resolving them before filing this issue.
-->

```
no issues found
```

<!-- Finally, paste the output of running `flutter doctor -v` here, with your device plugged in. -->

```
[√] Flutter (Channel stable, 2.0.0, on Microsoft Windows [Version 10.0.19041.804], locale de-DE)
    • Flutter version 2.0.0 at C:\src\flutter
    • Framework revision 60bd88df91 (2 hours ago), 2021-03-03 09:13:17 -0800
    • Engine revision 40441def69
    • Dart version 2.12.0

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at C:\Users\xxx\AppData\Local\Android\sdk
    • Platform android-30, build-tools 30.0.2
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Android Studio (version 4.0)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[√] VS Code (version 1.53.2)
    • VS Code at C:\Usersxx\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.20.0

[√] Connected device (3 available)
    • ONEPLUS A6013 (mobile) • 95a75628 • android-arm64  • Android 10 (API 29)
    • Chrome (web)           • chrome   • web-javascript • Google Chrome 88.0.4324.190
    • Edge (web)             • edge     • web-javascript • Microsoft Edge 88.0.705.74
```

</details>