Flutter学习之旅 -网格布局

GridView列表三种形式

  • 可以通过GridView.count实现网格布局
/*
格式:
GridView.count(
      crossAxisCount: 一行显示数量,
      children: [
        component(),
        ...
      ],
    )
*/
class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  
  Widget build(BuildContext context) {
    return GridView.count(
      crossAxisCount: 3,
      children: const [
        Icon(Icons.home),
        Icon(Icons.settings),
        Icon(Icons.all_inclusive),
        Icon(Icons.ac_unit)
      ],
    );
  }
}
  • 可以通过GridView.extent实现网格布局
class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  
  Widget build(BuildContext context) {
    return GridView.extent(
      maxCrossAxisExtent: 40, //主要。横轴子元素为固定最大长度(自动计算)
      children: const [
        Icon(Icons.home),
        Icon(Icons.settings),
        Icon(Icons.ac_unit),
        Icon(Icons.time_to_leave),
      ],
    );
  }
}
  • 可以通过GridView.builder实现网格布局

参数:可以通过SliveGridDelegateWithFiexdCrossAxisCount来设置GridView.count
参数: 可以通过SliveGridDelegateWithMaxCrossAxisExtent来设置GridView.extent

List listData = [
  {
    "imageUrl": "http://124.223.18.34:5555/static/images/gdyg/gdyg1_1.jpg",
    "name": "孤独摇滚第1集"
  },
  {
    "imageUrl": "http://124.223.18.34:5555/static/images/gdyg/gdyg1_2.jpg",
    "name": "孤独摇滚第2集"
  },
  {
    "imageUrl": "http://124.223.18.34:5555/static/images/gdyg/gdyg1_3.jpg",
    "name": "孤独摇滚第3集"
  }
];
class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  Widget _initDataWidget(context, index) {
    return Container(
      padding: const EdgeInsets.all(5),
      decoration: BoxDecoration(
          color: Colors.blue, borderRadius: BorderRadius.circular(5)),
      child: Column(
        children: [
          Image.network(listData[index]["imageUrl"]),
          Text(
            listData[index]["name"],
            style: const TextStyle(color: Colors.white),
          ),
        ],
      ),
    );
  }

  
  Widget build(BuildContext context) {
    return GridView.builder(
        padding: const EdgeInsets.all(10),
        itemCount: listData.length, //重点不然会报错(一直循环下去)
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2, crossAxisSpacing: 10),
        itemBuilder: _initDataWidget);
  }
}
常用属性
名称类型说明
scrollDirectionAxis滚动方法
paddingEdgeInsetsGeometry内边距
resolvebool组件反向排序
crossAxisSpacingdouble水平子Widget之间间距
mainAxisSpacingdouble垂直子Widget之间间距
crossAxisCountint,用在GridView.count一行的Widget数量
maxCrossAxisExtentdouble用在GridView.extent横轴子元素的最大长度
childAspectRatiodouble子Widget宽高比例
children[]
gridDelegateSliveGridDelegateWithFiexdCrossAxisCountSliveGridDelegateWithFiexdCrossAxisCount;SliveGridDelegateWithMaxCrossAxisExtent控制布局主要用在GridView.builder里面

小案例

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  
  Widget build(BuildContext context) {
    return GridView.count(
      padding: const EdgeInsets.all(15),
      mainAxisSpacing: 15, //垂直子widget之间的间距
      crossAxisSpacing: 15, //水平子Widget之间的间距
      crossAxisCount: 2,
      //childAspectRatio: 1.2,//宽高比
      children: [
        Container(
          // padding: EdgeInsets.,

          decoration: BoxDecoration(
              color: Colors.blue, borderRadius: BorderRadius.circular(5)),
          child: Image.network(
            "http://124.223.18.34:5555/static/images/gdyg/gdyg1_1.jpg",
            alignment: Alignment.topCenter,
            fit: BoxFit.contain,
          ),
        ),
        Container(
          decoration: BoxDecoration(
              color: Colors.blue, borderRadius: BorderRadius.circular(5)),
          child: Image.network(
            "http://124.223.18.34:5555/static/images/gdyg/gdyg1_2.jpg",
            alignment: Alignment.topCenter,
            fit: BoxFit.contain,
          ),
        ),
        Container(
          decoration: BoxDecoration(
              color: Colors.blue, borderRadius: BorderRadius.circular(5)),
          child: Image.network(
            "http://124.223.18.34:5555/static/images/gdyg/gdyg1_3.jpg",
            alignment: Alignment.topCenter,
            fit: BoxFit.contain,
          ),
        )
      ],
    );
  }
}

在这里插入图片描述

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  
  Widget build(BuildContext context) {
    return GridView.count(
      padding: const EdgeInsets.all(15),
      mainAxisSpacing: 15, //垂直子widget之间的间距
      crossAxisSpacing: 15, //水平子Widget之间的间距
      crossAxisCount: 2,
      //childAspectRatio: 1.2,//宽高比
      children: [
        Container(
            // padding: EdgeInsets.,

            decoration: BoxDecoration(
                color: Colors.blue, borderRadius: BorderRadius.circular(5)),
            child: Column(
              children: [
                Image.network(
                  "http://124.223.18.34:5555/static/images/gdyg/gdyg1_1.jpg",
                  alignment: Alignment.topCenter,
                  fit: BoxFit.contain,
                  width: 530,
                  height: 110,
                ),
                const Text(
                  "孤独摇滚第一集",
                  style: TextStyle(
                    color: Colors.white,
                  ),
                )
              ],
            )),
        Container(
            decoration: BoxDecoration(
                color: Colors.blue, borderRadius: BorderRadius.circular(5)),
            child: Column(
              children: [
                Image.network(
                  "http://124.223.18.34:5555/static/images/gdyg/gdyg1_2.jpg",
                  alignment: Alignment.topCenter,
                  fit: BoxFit.contain,
                  width: 530,
                  height: 110,
                ),
                const Text(
                  "孤独摇滚第二集",
                  style: TextStyle(
                    color: Colors.white,
                  ),
                )
              ],
            )),
        Container(
            decoration: BoxDecoration(
                color: Colors.blue, borderRadius: BorderRadius.circular(5)),
            child: Column(
              children: [
                Image.network(
                  "http://124.223.18.34:5555/static/images/gdyg/gdyg1_3.jpg",
                  alignment: Alignment.topCenter,
                  fit: BoxFit.contain,
                  width: 530,
                  height: 110,
                ),
                const Text(
                  "孤独摇滚第三集",
                  style: TextStyle(
                    color: Colors.white,
                  ),
                )
              ],
            ))
      ],
    );
  }
}

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

结城明日奈是我老婆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值