• Flutter(四):SingleChildScrollView、GridView


    SingleChildScrollView、GridView 遇到的问题

    以下代码会报错:

    在这里插入图片描述

    class GridViewPage extends StatefulWidget {
      const GridViewPage({super.key});
    
      
      State<GridViewPage> createState() => _GridViewPage();
    }
    
    class _GridViewPage extends State<GridViewPage> {
      
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: const Text('GridView 布局')),
          body: SingleChildScrollView(
            child: GridView.count(
              crossAxisCount: 1,
              children: [
                Container(color: Colors.red),
                Container(color: Colors.green),
                Container(color: Colors.blue),
                Container(color: Colors.black),
                Container(color: Colors.grey),
              ],
            ),
          ),
        );
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    修改方案一

    在这里插入图片描述

    存在问题:给定一个高度的方法虽然能解决报错问题,但是滚动区域会固定在所给高度内,无法自适应高度

    class GridViewPage extends StatefulWidget {
      const GridViewPage({super.key});
    
      
      State<GridViewPage> createState() => _GridViewPage();
    }
    
    class _GridViewPage extends State<GridViewPage> {
      
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: const Text('GridView 布局')),
          body: SizedBox(
            height: 500,
            child: GridView.count(
              crossAxisCount: 1,
              children: [
                Container(color: Colors.red),
                Container(color: Colors.green),
                Container(color: Colors.blue),
                Container(color: Colors.black),
                Container(color: Colors.grey),
              ],
            ),
          ),
        );
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    修改方案二

    在这里插入图片描述

    存在问题:页面不能滚动

    shrinkWrap: true: 自适应GridView高度

    class GridViewPage extends StatefulWidget {
      const GridViewPage({super.key});
    
      
      State<GridViewPage> createState() => _GridViewPage();
    }
    
    class _GridViewPage extends State<GridViewPage> {
      
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: const Text('GridView 布局')),
          body: SingleChildScrollView(
            child: GridView.count(
              crossAxisCount: 1,
              shrinkWrap: true,
              children: [
                Container(color: Colors.red),
                Container(color: Colors.green),
                Container(color: Colors.blue),
                Container(color: Colors.black),
                Container(color: Colors.grey),
              ],
            ),
          ),
        );
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    解决页面不能滚动问题

    在这里插入图片描述

    physics: const NeverScrollableScrollPhysics(): 禁用GridView自带的滚动

    class GridViewPage extends StatefulWidget {
      const GridViewPage({super.key});
    
      
      State<GridViewPage> createState() => _GridViewPage();
    }
    
    class _GridViewPage extends State<GridViewPage> {
      
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: const Text('GridView 布局')),
          body: SingleChildScrollView(
            child: GridView.count(
              crossAxisCount: 1,
              shrinkWrap: true,
              physics: const NeverScrollableScrollPhysics(),
              children: [
                Container(color: Colors.red),
                Container(color: Colors.green),
                Container(color: Colors.blue),
                Container(color: Colors.black),
                Container(color: Colors.grey),
              ],
            ),
          ),
        );
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    实现 GridView 布局

    在这里插入图片描述

    属性描述
    shrinkWrap自适应宽高
    scrollDirection布局方向
    childAspectRatio主轴、交叉轴 比
    mainAxisSpacing主轴间距
    crossAxisSpacing交叉轴间距
    crossAxisCount交叉轴列数
    maxCrossAxisExtent交叉轴列最尺寸
    import 'package:flutter/material.dart';
    
    class TitleWidget extends StatelessWidget {
      final String title;
    
      const TitleWidget(this.title, {super.key});
    
      
      Widget build(BuildContext context) {
        return Container(
          margin: const EdgeInsets.fromLTRB(10, 5, 10, 5),
          padding: const EdgeInsets.only(left: 5),
          decoration: const BoxDecoration(
            border: Border(left: BorderSide(color: Colors.lightBlue, width: 5)),
          ),
          child: Text(title),
        );
      }
    }
    
    class ContainerWidget extends StatelessWidget {
      final Color color;
      final String title;
    
      const ContainerWidget({super.key, required this.color, required this.title});
    
      
      Widget build(BuildContext context) {
        return Container(
          decoration: BoxDecoration(
            color: color,
            borderRadius: const BorderRadius.all(Radius.circular(5)),
          ),
          child: Center(child: Text(title)),
        );
      }
    }
    
    class GridViewPage extends StatefulWidget {
      const GridViewPage({super.key});
    
      
      State<GridViewPage> createState() => _GridViewPage();
    }
    
    class _GridViewPage extends State<GridViewPage> {
      List<ContainerWidget> containerWidgetList = const [
        ContainerWidget(color: Colors.red, title: 'RED'),
        ContainerWidget(color: Colors.orange, title: 'ORANGE'),
        ContainerWidget(color: Colors.yellow, title: 'YELLOW'),
        ContainerWidget(color: Colors.green, title: 'GREEN'),
        ContainerWidget(color: Colors.cyan, title: 'CYAN'),
        ContainerWidget(color: Colors.blue, title: 'BLUE'),
        ContainerWidget(color: Colors.purple, title: 'PURPLE'),
        ContainerWidget(color: Colors.grey, title: 'GREY'),
      ];
    
      
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: const Text('GridView 布局')),
          body: SingleChildScrollView(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const TitleWidget('GridView.count'),
                SizedBox(
                  height: 160,
                  child: GridView.count(
                    scrollDirection: Axis.horizontal,
                    crossAxisCount: 1,
                    padding: const EdgeInsets.only(left: 10, right: 10),
                    crossAxisSpacing: 0,
                    mainAxisSpacing: 10,
                    childAspectRatio: 0.6,
                    children: containerWidgetList,
                  ),
                ),
                const TitleWidget('GridView.extent'),
                GridView.extent(
                  shrinkWrap: true,
                  physics: const NeverScrollableScrollPhysics(),
                  scrollDirection: Axis.vertical,
                  maxCrossAxisExtent: 200,
                  childAspectRatio: 0.8,
                  padding: const EdgeInsets.only(left: 10, right: 10),
                  mainAxisSpacing: 10,
                  crossAxisSpacing: 10,
                  children: containerWidgetList,
                ),
                const TitleWidget('SliverGridDelegateWithFixedCrossAxisCount'),
                SizedBox(
                  height: 330,
                  child: GridView(
                    gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 2,
                      crossAxisSpacing: 10,
                      mainAxisSpacing: 10,
                      childAspectRatio: 0.6,
                    ),
                    scrollDirection: Axis.horizontal,
                    padding: const EdgeInsets.only(left: 10, right: 10),
                    children: containerWidgetList,
                  ),
                ),
                const TitleWidget('SliverGridDelegateWithMaxCrossAxisExtent'),
                GridView(
                  gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
                    maxCrossAxisExtent: 200,
                    childAspectRatio: 2,
                    mainAxisSpacing: 10,
                    crossAxisSpacing: 10,
                  ),
                  shrinkWrap: true,
                  physics: const NeverScrollableScrollPhysics(),
                  scrollDirection: Axis.vertical,
                  padding: const EdgeInsets.only(left: 10, right: 10, bottom: 10),
                  children: containerWidgetList,
                ),
              ],
            ),
          ),
        );
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
  • 相关阅读:
    Spring01
    ES6 入门教程 24 Module 的语法 24.4 import 命令 & 24.5 模块的整体加载
    C#中实现按位域操作
    注解配置SpringMVC
    数据结构之拓扑排序
    [模拟][模电][面试][运放]仪表放大器
    Linux工具——gdb
    第五篇文章:String,StringBuffer,StringBuilder
    简单讲解Android Fragment(一)
    红黑树2——怎么画红黑树
  • 原文地址:https://blog.csdn.net/weixin_43526371/article/details/136363338