Flutter:SingleChildScrollView不适用于列和网格视图,为什么?

3htmauhk  于 2023-01-14  发布在  Flutter
关注(0)|答案(3)|浏览(159)

我无法滚动查看下方的网格视图。这是为什么?我应该做些什么更改才能使其正常工作?请帮助!!!提前感谢您。

Scaffold(
            body: SingleChildScrollView(
              child: Container(
                child: Column(
                  children: <Widget>[
                    Text('GridView 1'),
                    GridView.count(
                      crossAxisCount: 3,
                      shrinkWrap: true,
                      children: List.generate(
                        9,
                        (index) {
                          return TouchableImageCard(
                            imagePath: 'assets/images/view_${index + 1}.jpg',
                          );
                        },
                      ),
                    ),
                    Text('GridView 2'),
                    GridView.builder(
                      itemCount: list_item.length,
                      shrinkWrap: true,
                      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                          crossAxisCount: 3),
                      itemBuilder: (BuildContext context, int index) {
                        return TouchableImageCard(
                          imagePath: 'assets/images/view_${index + 1}.jpg',
                          // width: 150,
                          // height: 150,
                        );
                      },
                    ),
                  ],
                ),
              ),
            ),
          );

谢谢你在扑翼课上帮我。

7gs2gvoe

7gs2gvoe1#

它确实有效,只是不是你需要的方式。
问题是GridView本身也是一个可滚动的小部件,所以当你试图滚动页面时,它实际上试图滚动那些GridView,而不是SingleChildScrollView。
要禁用GridView滚动功能,您需要再添加一个参数。

GridView.count(
                physics: NeverScrollableScrollPhysics(),
                ...
              ),
e37o9pze

e37o9pze2#

试试这个:

Center(
    child: SingleChildScrollView(
      padding: const EdgeInsets.all(8.0),
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[]
3duebb1j

3duebb1j3#

多个网格视图可滚动

Scaffold(
      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            Text('GridView 1'),
            Container(
              height: 400,
              child: GridView.count(
                crossAxisCount: 3,
                shrinkWrap: true,
                primary: false,
                children: List.generate(
                  20,
                  (index) {
                    return TouchableImageCard(
                      imagePath: 'assets/images/view_${index + 1}.jpg',
                    );
                  },
                ),
              ),
            ),
            Text('GridView 2'),
            Container(
              height: 400,
              child: GridView.builder(
                itemCount: 20,
                shrinkWrap: true,
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 3),
                itemBuilder: (BuildContext context, int index) {
                  return TouchableImageCard(
                    imagePath: 'assets/images/view_${index + 1}.jpg',
                    // width: 150,
                    // height: 150,
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );

相关问题