Search code examples
fluttergridviewscrollview

Flutter GridView.builder not scrollable


I have a screen with different components, one of which is a GridView.builder which will not scroll no matter what I do.

I have tried different methods, and nothing is working at all, so I will share what I am trying currently:

@override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomPadding: false,
      body:  Container(
        child: CustomScrollView(
          slivers: <Widget>[
            SliverList(
              delegate: SliverChildListDelegate(
                [
                  //Some widgets here, some images, some buttons.   This section scrolls perfectly.
                ],
            SliverList(
          delegate: SliverChildListDelegate(
            [
              GridView.builder(
                  //physics: ScrollPhysics(),
                  shrinkWrap: true,
                  itemCount: imageURLlist.length,
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 1,
                  ),
                  itemBuilder: (BuildContext context, int index) {
                    return ListView(
                      children: <Widget>[
                        Image.network(
                          imageURLlist[index],
                          height: 150,
                          width: MediaQuery.of(context).size.width * 0.5,
                          fit: BoxFit.fill,
                        ),
                      ],
                    );
                  }
                ),
              ),
            ],
          ),
        ],
      ),
    ),
  ),   
}

The GridView section populates with images, and that's great, but it is not scrollable. I can scroll down to it, but as soon as I reach this section I lose the ability to scroll completely and I cannot work out why.


Solution

  • You can copy paste run full code below
    You can use NeverScrollableScrollPhysics() in GridView and ListView
    In your case, you can work without ListView

    GridView.builder(
                    physics: NeverScrollableScrollPhysics()
    ...             
    ListView(
                    physics: NeverScrollableScrollPhysics()     
    
        
    

    working demo

    enter image description here

    full code

    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,
            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> {
      List<String> imageURLlist = [
        "https://picsum.photos/250?image=9",
        "https://picsum.photos/250?image=10",
        "https://picsum.photos/250?image=11",
        "https://picsum.photos/250?image=12"
      ];
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            resizeToAvoidBottomPadding: false,
            body: Container(
                child: CustomScrollView(slivers: <Widget>[
              SliverList(
                delegate: SliverChildListDelegate(
                  [
                    Container(color: Colors.red, height: 150.0),
                    Container(color: Colors.purple, height: 150.0),
                    Container(color: Colors.green, height: 150.0),
                  ],
                ),
              ),
              SliverList(
                  delegate: SliverChildListDelegate([
                GridView.builder(
                    physics: NeverScrollableScrollPhysics(),
                    shrinkWrap: true,
                    itemCount: imageURLlist.length,
                    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 1,
                    ),
                    itemBuilder: (BuildContext context, int index) {
                      return ListView(
                        physics: NeverScrollableScrollPhysics(),
                        children: <Widget>[
                          Image.network(
                            imageURLlist[index],
                            height: 150,
                            width: MediaQuery.of(context).size.width * 0.5,
                            fit: BoxFit.fill,
                          ),
                        ],
                      );
                    }),
              ]))
            ])));
      }
    }