Repository: flutter
Issue: [IOS] GridView.builder scrolling is laggy on adding a Gesturedetector
Labels: platform-ios,framework,c: performance,f: scrolling,has reproducible steps,P2,found in release: 2.8,found in release: 2.10,team-ios,triaged-ios

I am building a shopping application, the products are laid out in a grid view and contain a few buttons (to see details, or add it to the cart, etc.).
As soon as there are a lot of Products in the GridView(>300), with onPressed functions that write to the shared preferences, the scrolling becomes very choppy, although the framerate seems to stay stable. 
The onPressed function seems to be a big factor, as a simple print does no harm to the scrolling performance.

I am wondering how the itemcount can have such a big influence since the GridView.builder should perform the same no matter how many items there are as I understood it.

The problem persists in stable and master channel, in release and debug mode.

Here is a video on iPhone XS Max in release mode of the original App (not the below minimal reproduction, but shows the same behavior): https://drive.google.com/file/d/1NX04z8O332t0tPvxc4wOCOXcIGtpzEVl/view?usp=sharing


Tested Platfors / Devices:
**Target Platform: IOS, Android**
**Devices: iPhone XS Max, Redmi note 7, IOS Simulator**

On the web, it all works fine.



minimal reproduction Code main.dart: 

```dart
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
  List shoppingCartList = [];

  Future<List> getFuture(int number) async {
    List list = [];
    for (int i = 0; i < number; i++) {
      list.add(i);
    }
    return list;
  }

  iconButtonFunction(snapshot, index) async {
    final SharedPreferences prefs = await _prefs;
    HapticFeedback.heavyImpact();
    setState(() {
      shoppingCartList.add([
        snapshot.data[index],
        snapshot.data[index],
        snapshot.data[index],
        snapshot.data[index],
        snapshot.data[index],
        snapshot.data[index]
      ]);
    });

    await prefs.setString('currentShoppingCart', jsonEncode(shoppingCartList));

    //updateProductList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: ListView(
        children: [
          FutureBuilder(
            future: getFuture(500),
            builder: (context, AsyncSnapshot snapshot) {
              return GridView.builder(
                primary: false,
                shrinkWrap: true,
                itemCount: snapshot.data?.length ?? 0,
                gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 2),
                itemBuilder: (context, index) {
                  return Container(
                    margin: EdgeInsets.all(10),
                    color: Colors.green,
                    child: Column(
                      children: [
                        Row(
                          children: [
                            IconButton(
                              onPressed: () {
                                iconButtonFunction(snapshot, index);
                              },
                              icon: Icon(Icons.add),
                            ),
                          ],
                        ),
                        GestureDetector(
                          onTap: () {
                            print('gesture detected');
                          },
                          child: Column(
                            children: [
                              Text(snapshot.data[index].toString()),
                            ],
                          ),
                        )
                      ],
                    ),
                  );
                },
              );
            },
          ),
        ],
      ),
    );
  }
}


```

flutter doctor -v
```

[✓] Flutter (Channel stable, 2.8.1, on macOS 12.1 21C52 darwin-x64, locale de-DE)
    • Flutter version 2.8.1 at /Users/maxdeboer/Dev/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 77d935af4d (vor 7 Wochen), 2021-12-16 08:37:33 -0800
    • Engine revision 890a5fca2e
    • Dart version 2.15.1

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /Users/maxdeboer/Library/Android/sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7590822)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • 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 11.0.11+0-b60-7590822)

[✓] IntelliJ IDEA Community Edition (version 2020.1.2)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • 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

[✓] Connected device (3 available)
    • iPhone von Max (mobile) • 00008020-000249E82E50003A • ios            • iOS 15.2.1 19C63
    • macOS (desktop)         • macos                     • darwin-x64     • macOS 12.1 21C52 darwin-x64
    • Chrome (web)            • chrome                    • web-javascript • Google Chrome 97.0.4692.99
    ! Error: iPhone von Max is busy: Fetching debug symbols for iPhone von Max. Xcode will continue when iPhone von Max is finished. (code -10)

• No issues found!

```
flutter analyze
```
Analyzing github_reproduction...                                        
No issues found! (ran in 4.8s)

```





</details>

[verbose_output.txt](https://github.com/flutter/flutter/files/7992809/verbose_output.txt)
[dart-timeline-2022-2-3.json.zip](https://github.com/flutter/flutter/files/7993813/dart-timeline-2022-2-3.json.zip)