Flutter GridView网格组件

[复制链接]
发表于 2025-6-21 09:34:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
目录
常用属性
GridView利用配置       
        GridView.count利用
        GridView.extent利用
        GridView.count Container 实现列表
        GridView.extent Container 实现列表
        GridView.builder利用


GridView网格布局在实际项目中用的也是非常多的,当我们想让可以滚动的元素利用矩阵方式排列的时候。此时我们可以用网格列表组件GridView实现布局。
GridView创建网格列表重要有下面三种方式
1、可以通过GridView.count实现网格布局
2、可以通过GridView.extent 实现网格布局
3、通过GridView.builder实现动态网格布局
常用属性


GridView利用配置       

        GridView.count利用

  1. import 'package:flutter/material.dart';
  2. void main() {
  3.   runApp(MyTestApp());
  4. }
  5. class MyTestApp extends StatelessWidget {
  6.   @override
  7.   Widget build(BuildContext context) {
  8.     return MaterialApp(
  9.       theme: ThemeData(primarySwatch: Colors.yellow),
  10.       home: Scaffold(
  11.         appBar: AppBar(title: Text("App")),
  12.         body: MyTestHomePage(),
  13.       ),
  14.     );
  15.   }
  16. }
  17. //Material Design官方Icons图标
  18. class MyTestHomePage extends StatelessWidget {
  19.   @override
  20.   Widget build(BuildContext context) {
  21.     return GridView.count(crossAxisCount: 4, //一行显示的
  22.       children: [
  23.         Image.asset("assets/images/iv_account_selected.png"),
  24.         Icon(Icons.abc_rounded),
  25.         Icon(Icons.abc_sharp),
  26.         Icon(Icons.factory),
  27.     ]);
  28.   }
  29. }
复制代码

        GridView.extent利用

  1. import 'package:flutter/material.dart';
  2. void main() {
  3.   runApp(MyTestApp());
  4. }
  5. class MyTestApp extends StatelessWidget {
  6.   @override
  7.   Widget build(BuildContext context) {
  8.     return MaterialApp(
  9.       theme: ThemeData(primarySwatch: Colors.yellow),
  10.       home: Scaffold(
  11.         appBar: AppBar(title: Text("App")),
  12.         body: MyTestHomePage(),
  13.       ),
  14.     );
  15.   }
  16. }
  17. //Material Design官方Icons图标
  18. class MyTestHomePage extends StatelessWidget {
  19.   @override
  20.   Widget build(BuildContext context) {
  21.     return GridView.extent(
  22.       maxCrossAxisExtent: 180, //横轴子元素的最大长度
  23.       children: [
  24.         Icon(Icons.abc_rounded),
  25.         Icon(Icons.abc_sharp),
  26.         Icon(Icons.factory),
  27.       ],
  28.     );
  29.   }
  30. }
复制代码

        GridView.count Container 实现列表

  1. import 'package:flutter/material.dart';
  2. void main() {
  3.   runApp(MyTestApp());
  4. }
  5. class MyTestApp extends StatelessWidget {
  6.   @override
  7.   Widget build(BuildContext context) {
  8.     return MaterialApp(
  9.       theme: ThemeData(primarySwatch: Colors.yellow),
  10.       home: Scaffold(
  11.         appBar: AppBar(title: Text("App")),
  12.         body: MyTestHomePage(),
  13.       ),
  14.     );
  15.   }
  16. }
  17. //Material Design官方Icons图标
  18. class MyTestHomePage extends StatelessWidget {
  19.   List<Widget> initList() {
  20.     List<Widget> list = [];
  21.     for (var i = 0; i < 10; i++) {
  22.       list.add(
  23.         Container(
  24.           alignment: Alignment.center,
  25.           decoration: BoxDecoration(color: Colors.blue),
  26.           child: Text("第$i个元素", style: TextStyle(fontSize: 20)),
  27.         ),
  28.       );
  29.     }
  30.     return list;
  31.   }
  32.   @override
  33.   Widget build(BuildContext context) {
  34.     return GridView.count(
  35.       padding: EdgeInsets.all(10),
  36.       crossAxisSpacing: 10, //水平间距
  37.       mainAxisSpacing: 10, //垂直间距
  38.       crossAxisCount: 2, //一行显示多少个元素
  39.       childAspectRatio: 0.7,//宽高的比例
  40.       children: initList(),
  41.     );
  42.   }
  43. }
复制代码

        GridView.extent Container 实现列表

  1. import 'package:flutter/material.dart';
  2. void main() {
  3.   runApp(MyTestApp());
  4. }
  5. class MyTestApp extends StatelessWidget {
  6.   @override
  7.   Widget build(BuildContext context) {
  8.     return MaterialApp(
  9.       theme: ThemeData(primarySwatch: Colors.yellow),
  10.       home: Scaffold(
  11.         appBar: AppBar(title: Text("App")),
  12.         body: MyTestHomePage(),
  13.       ),
  14.     );
  15.   }
  16. }
  17. //Material Design官方Icons图标
  18. class MyTestHomePage extends StatelessWidget {
  19.   List<Widget> initList() {
  20.     List<Widget> list = [];
  21.     for (var i = 0; i < 10; i++) {
  22.       list.add(
  23.         Container(
  24.           alignment: Alignment.center,
  25.           decoration: BoxDecoration(color: Colors.blue),
  26.           child: Text("第$i个元素", style: TextStyle(fontSize: 20)),
  27.         ),
  28.       );
  29.     }
  30.     return list;
  31.   }
  32.   @override
  33.   Widget build(BuildContext context) {
  34.     return GridView.extent(
  35.       padding: EdgeInsets.all(10),
  36.       crossAxisSpacing: 10, //水平间距
  37.       mainAxisSpacing: 10, //垂直间距
  38.       maxCrossAxisExtent: 120,//横轴子元素的最大长度 动态计算
  39.       //childAspectRatio: 0.7,//宽高的比例
  40.       children: initList(),
  41.     );
  42.   }
  43. }
复制代码


map循环遍历数据,通过Container渲染
  1. import 'package:flutter/material.dart';
  2. void main() {
  3.   runApp(MyTestApp());
  4. }
  5. class MyTestApp extends StatelessWidget {
  6.   @override
  7.   Widget build(BuildContext context) {
  8.     return MaterialApp(
  9.       theme: ThemeData(primarySwatch: Colors.yellow),
  10.       home: Scaffold(
  11.         appBar: AppBar(title: Text("App")),
  12.         body: MyTestHomePage(),
  13.       ),
  14.     );
  15.   }
  16. }
  17. //Material Design官方Icons图标
  18. class MyTestHomePage extends StatelessWidget {
  19.   List<Widget> initList() {
  20.     List<Widget> list = [];
  21.     for (var i = 0; i < 10; i++) {
  22.       list.add(
  23.         Container(
  24.           alignment: Alignment.center,
  25.           decoration: BoxDecoration(color: Colors.blue),
  26.           child: Column(children: [
  27.             Image.network("https://p4.img.cctvpic.com/photoAlbum/photo/2025/05/27/PHOT1cFfxozsH6VncGwLpViz250527_1000x2000.jpg"),
  28.             Text("第$i元素"),
  29.           ]),
  30.         ),
  31.       );
  32.     }
  33.     return list;
  34.   }
  35.   @override
  36.   Widget build(BuildContext context) {
  37.   
  38.     return GridView.extent(
  39.       padding: EdgeInsets.all(10),
  40.       crossAxisSpacing: 10,
  41.       //水平间距
  42.       mainAxisSpacing: 10,
  43.       //垂直间距
  44.       maxCrossAxisExtent: 120,
  45.       //横轴子元素的最大长度
  46.       childAspectRatio: 1,
  47.       //宽高的比例
  48.       children: initList(),
  49.     );
  50.   }
  51. }
复制代码

        GridView.builder利用

  1. import 'package:flutter/material.dart';
  2. void main() {
  3.   runApp(MyTestApp());
  4. }
  5. class MyTestApp extends StatelessWidget {
  6.   @override
  7.   Widget build(BuildContext context) {
  8.     return MaterialApp(
  9.       theme: ThemeData(primarySwatch: Colors.yellow),
  10.       home: Scaffold(
  11.         appBar: AppBar(title: Text("App")),
  12.         body: MyTestHomePage(),
  13.       ),
  14.     );
  15.   }
  16. }
  17. //Material Design官方Icons图标
  18. class MyTestHomePage extends StatelessWidget {
  19.   List list = [
  20.     {
  21.       "title": "Candy Shop",
  22.       "imageUrl":
  23.           "https://p4.img.cctvpic.com/photoAlbum/photo/2025/05/27/PHOT1cFfxozsH6VncGwLpViz250527_1000x2000.jpg",
  24.       "author": "Mohamed Chahin",
  25.     },
  26.     {
  27.       "title": "Zhao Shop",
  28.       "imageUrl":
  29.           "https://p4.img.cctvpic.com/photoAlbum/photo/2025/05/27/PHOT1cFfxozsH6VncGwLpViz250527_1000x2000.jpg",
  30.       "author": "Mohamed Chahin",
  31.     },
  32.     {
  33.       "title": "Fei Shop",
  34.       "imageUrl":
  35.       "https://p4.img.cctvpic.com/photoAlbum/photo/2025/05/27/PHOT1cFfxozsH6VncGwLpViz250527_1000x2000.jpg",
  36.       "author": "Mohamed Chahin",
  37.     },
  38.   ];
  39.   Widget initList(context,index) {
  40.    return Container(
  41.       alignment: Alignment.center,
  42.       decoration: BoxDecoration(color: Colors.blue),
  43.       child: Column(
  44.         children: [
  45.           Image.network(list[index]["imageUrl"]),
  46.           Text(list[index]["title"]),
  47.         ],
  48.       ),
  49.     );
  50.   }
  51.   @override
  52.   Widget build(BuildContext context) {
  53.     return GridView.builder(
  54.       padding: EdgeInsets.all(10),
  55.       itemCount: list.length,
  56.       gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  57.         crossAxisCount: 2,
  58.         crossAxisSpacing: 10,
  59.         childAspectRatio: 1,
  60.       ),
  61.       itemBuilder: initList
  62.     );
  63.   }
  64.   // return GridView.builder(
  65.     //   padding: EdgeInsets.all(10),
  66.     //   itemCount: list.length,
  67.     //   gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
  68.     //     maxCrossAxisExtent: 200,
  69.     //     crossAxisSpacing: 10,
  70.     //     childAspectRatio: 1,
  71.     //   ),
  72.     //   itemBuilder: initList,
  73.     // );
  74. }
复制代码


回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表