Flutter-GridView

GridView 构建二维网格列表。
构造参数
    Key key,
    Axis scrollDirection = Axis.vertical,
    bool reverse = false,
    ScrollController controller,
    bool primary,
    ScrollPhysics physics,
    bool shrinkWrap = false,
    EdgeInsetsGeometry padding,
    @required this.gridDelegate,
    bool addAutomaticKeepAlives = true,
    bool addRepaintBoundaries = true,
    bool addSemanticIndexes = true,
    double cacheExtent,
    List<Widget> children = const <Widget>[],
    int semanticChildCount,
    DragStartBehavior dragStartBehavior = DragStartBehavior.start,
    Clip clipBehavior = Clip.hardEdge,
    ScrollViewKeyboardDismissBehavior keyboardDismissBehavior = ScrollViewKeyboardDismissBehavior.manual,
    String restorationId,
  })

大部分与ListView相同,重点关注gridDelegate参数。其类型为SliverGridDelegate它的作用是控制GridView子组件如何排列(layout)。
SliverGridDelegate是一个抽象类,其两个SliverGridDelegate的子类为SliverGridDelegateWithFixedCrossAxisCount和SliverGridDelegateWithMaxCrossAxisExtent。

SliverGridDelegateWithFixedCrossAxisCount:提供了一种横轴为固定数量的子元素的layout算法。
SliverGridDelegateWithMaxCrossAxisExtent:提供了一种横轴子元素为固定最大长度的layout算法。

Samples

1.使用SliverGridDelegateWithFixedCrossAxisCount算法构造

    required this.crossAxisCount,//代表横轴子元素个数
    this.mainAxisSpacing = 0.0,主轴方向上的间距
    this.crossAxisSpacing = 0.0,//横轴子元素间距
    this.childAspectRatio = 1.0,//子元素在横轴和主轴上的长度比例
  })
import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _HomePageState();
  }
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("GridView"),
        ),
        body: Container(
          child: GridView(
            //横轴固定子元素个数
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              //横轴子元素个数
              crossAxisCount: 3,
              //横轴和纵轴比例
              childAspectRatio: 1.0,
            ),
            children: <Widget>[
              Icon(Icons.phone),
              Icon(Icons.add),
              Icon(Icons.map),
              Icon(Icons.lock),
              Icon(Icons.ad_units),
              Icon(Icons.add_a_photo),
            ],
          ),
        ));
  }
}

快捷方式:
  child: GridView.count(
            //横轴子元素个数
            crossAxisCount: 3,
            //横轴和纵轴比例
            childAspectRatio: 1.0,
            children: <Widget>[
              Icon(Icons.phone),
              Icon(Icons.add),
              Icon(Icons.map),
              Icon(Icons.lock),
              Icon(Icons.ad_units),
              Icon(Icons.add_a_photo),
            ],
          ),

2.使用SliverGridDelegateWithMaxCrossAxisExtent算法构造

    required this.maxCrossAxisExtent,//代表子元素在横轴上的最大长度
    this.mainAxisSpacing = 0.0,
    this.crossAxisSpacing = 0.0,
    this.childAspectRatio = 1.0,
  })
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _HomePageState();
  }
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("GridView"),
        ),
        body: Container(
          child: GridView(
            gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
                maxCrossAxisExtent: 110.0, childAspectRatio: 1.0),
            children: <Widget>[
              Icon(Icons.phone),
              Icon(Icons.add),
              Icon(Icons.map),
              Icon(Icons.lock),
              Icon(Icons.ad_units),
              Icon(Icons.add_a_photo),
            ],
          ),
        ));
  }
}
快捷方式:
 child: GridView.extent(
            maxCrossAxisExtent: 110.0,
            childAspectRatio: 1.0,
            children: <Widget>[
              Icon(Icons.phone),
              Icon(Icons.add),
              Icon(Icons.map),
              Icon(Icons.lock),
              Icon(Icons.ad_units),
              Icon(Icons.add_a_photo),
            ],
          ),

3.懒加载方式GridView.Builder。
上面两种方式只适用于子元素较少的情况下,因为在其是在构建的时候一次性把所有子Widget都构建好的。当数量较多时就要用到GridView.Builder()懒加载模型了。

import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _HomePageState();
  }
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("GridView"),
        ),
        body: Container(
          child: GridView.builder(
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 3,
                  childAspectRatio: 1.0
              ), itemCount: 20,
              itemBuilder: (context, index) {
                return Icon(Icons.add_a_photo);
              }),
        ));
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值