首页 > 解决方案 > Flutter GridView.builder 不可滚动

问题描述

我有一个包含不同组件的屏幕,其中一个是 GridView.builder,无论我做什么都不会滚动。

我尝试了不同的方法,但根本没有任何效果,所以我将分享我目前正在尝试的方法:

@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,
                        ),
                      ],
                    );
                  }
                ),
              ),
            ],
          ),
        ],
      ),
    ),
  ),   
}

GridView 部分填充了图像,这很好,但它不可滚动。我可以向下滚动到它,但是一旦我到达这个部分,我就失去了完全滚动的能力,我不知道为什么。

标签: fluttergridviewscrollview

解决方案


您可以在下面复制粘贴运行完整代码
您可以使用NeverScrollableScrollPhysics()在您GridViewListView
情况下,您可以在没有的情况下工作ListView

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

    

工作演示

在此处输入图像描述

完整代码

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,
                      ),
                    ],
                  );
                }),
          ]))
        ])));
  }
}

推荐阅读