Flutter 网格视图不滚动

thigvfpy  于 2023-01-18  发布在  Flutter
关注(0)|答案(7)|浏览(203)

我正在网格视图中添加一个标题。标题是滚动的,但当触摸网格视图时,它不滚动。我想滚动标题和网格视图。我已经使用了SingleChildScrollViewExpanded。如何解决请帮助我。
我的代码如下所示

Widget ItemGridview() {
    return Container(
        color: Colors.white,
    padding: EdgeInsets.all(10),
    child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
            new Expanded(
            child: SingleChildScrollView(
            child: Column(
                children: <Widget>[
                new Text(
            'Items of products',
            style: TextStyle(fontWeight: FontWeight.w700, fontSize: 18.0),
            textAlign: TextAlign.left,
        ),
                GridView.count(
            shrinkWrap: true,
            primary: true,
            padding: EdgeInsets.only(top:15.0),
            crossAxisCount: 3,
            childAspectRatio: 0.60, //1.0
            mainAxisSpacing: 0.2, //1.0
            crossAxisSpacing: 4.0, //1.0
            children: createCategoryList(),
            ),
                ],
            ),
            )
        )
        ]
    ),
    );
}

在我的代码产品项目是标题。

List<Widget> createCategoryList() {

  List<Widget> createCategoryList = List<Widget>();

  for (int i = 0; i < documents.length; i++) {
    createCategoryList
        .add(makeGridCell(documents[i].data['title'], "name", 8,documents[i].data['id']));
  }
  return createCategoryList;
}

  Container makeGridCell(String name, String image, int count, String id) {
  return Container(

      child: new GestureDetector(
        onTap: () {
        },
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          mainAxisSize: MainAxisSize.min,
          verticalDirection: VerticalDirection.down,
          children: <Widget>[
            new Container(
              child: Image.asset('assets/' + image + ".jpg"),

            ),
            new Container(
              color: Colors.white,
              padding: EdgeInsets.only(left: 5),
              child: new Text(name,
                  style: TextStyle(
                      fontWeight: FontWeight.w500, fontSize: 18.0)), 
            ),

          ],
        ),
      ));
}

createCategoryList()是小部件中写入的网格中的项目列表。

vhipe2zx

vhipe2zx1#

我有一个类似的小部件树,就像你一样,gridview.count() Package 在SingleChildScrollView中,添加

physics: ScrollPhysics(),

GridView.count()小部件解决了我的问题
来源:https://github.com/flutter/flutter/issues/19205

bqf10yzr

bqf10yzr2#

添加physics: ScrollPhysics()属性到网格视图。它将滚动。

d4so4syb

d4so4syb3#

只需在GridView中添加一些属性

Widget _buildFields(BuildContext context) {
return Container(
    color: Colors.white,
    child: GridView.count(
      crossAxisCount: 2,
      crossAxisSpacing: 2.0,
      mainAxisSpacing: 2.0,
      shrinkWrap: true,
      scrollDirection: Axis.vertical,
      physics: NeverScrollableScrollPhysics(),
      children: List.generate(choices.length, (index) {
        return Center(
          child: new Column(
            children: [
              new Expanded(
                child: SelectCard(choice: choices[index]),//your card wight
              ),
            ],
          ),
        );
      }),
    ));
}

像这样使用

class _Dashboard extends State<Dashboard> {
  @override
  Widget build(BuildContext context) {
    return OrientationBuilder(builder: (context, orientation) {
      return ListView(
        children: <Widget>[
          Container(
            height: 200,
            child: Image.network(
                "https://www.gizbot.com/img/2013/11/23-weekend-deals-top-10-latest-smartphones.jpg"),
          ),
          _buildFields(context),
        ],
      );
    });
  }
}
bq9c1y66

bq9c1y664#

您有一些与小工具滚动相关的问题,您可以使用Wrap减少Widgets的数量,如下所示:

Container(
            color: Colors.white,
            padding: EdgeInsets.all(10),
            child: SingleChildScrollView(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  new Text(
                    'Items of products',
                    style: TextStyle(fontWeight: FontWeight.w700, fontSize: 18.0),
                    textAlign: TextAlign.left,
                  ),
                  Padding(
                    padding: const EdgeInsets.only(top: 15.0),
                    child: Wrap(
                      spacing: 20.0,
                      alignment: WrapAlignment.spaceEvenly,
                      children:createCategoryList(),
                ),
                    ],
                ),
                )
            )
            ]
        ),
        );

将约束宽度或固定宽度添加到项目的Widget:

return Container(
          constraints:
                BoxConstraints(maxWidth: MediaQuery.of(context).size.width / 4),
          child: new GestureDetector(
31moq8wy

31moq8wy5#

我认为你需要使用一些自定义滚动视图

CustomScrollView(
  primary: false,
  slivers: <Widget>[
    SliverPadding(
      padding: const EdgeInsets.all(20.0),
      sliver: SliverGrid.count(
        crossAxisSpacing: 10.0,
        crossAxisCount: 2,
        children: <Widget>[
          const Text('He\'d have you all unravel at the'),
          const Text('Heed not the rabble'),
          const Text('Sound of screams but the'),
          const Text('Who scream'),
          const Text('Revolution is coming...'),
          const Text('Revolution, they...'),
        ],
      ),
    ),
  ],
)
s5a0g9ez

s5a0g9ez6#

我自己也遇到过这种情况,将GridView的primary参数更改为false,给予看。

bwitn5fc

bwitn5fc7#

在Gridview.builder滚动是不工作的小分辨率如平板电脑模式,移动的模式,然后只是 Package 下的listview.builder小部件的Gridview.builder。

SizedBox(
                  width: screenSize.width,
                  height: screenSize.height * entry.contentHeightFactor,
                  child: ListView.builder(
                    itemCount: 1,
                    itemBuilder: (context, index) {
                      return Card(
                        child: Container(
                          width: screenSize.width * 0.8,
                          height: screenSize.height * 0.72,
                          padding: const EdgeInsets.all(10),
                          child: GridView.builder(
                            scrollDirection: Axis.vertical,
                            gridDelegate:
                                const SliverGridDelegateWithFixedCrossAxisCount(
                              crossAxisCount: 3,
                            ),
                            padding: const EdgeInsets.all(5),
                            itemCount: 30,
                            itemBuilder: (BuildContext context, int index) {
                              return Padding(
                                padding: const EdgeInsets.all(8.0),
                                child:Card(....),
                              );
                            },
                          ),
                        ),
                      );
                    },
                  ),
                ),

相关问题