Flutter圈子Flutter中文社区

Flutter-GridView

2019-07-27  本文已影响5人  哎呀啊噢
简介:
一个可以滚动的2D数组组件;类似Android的GridView,都是列表容器,用法跟ListView类似,网格布局,一行可以有多个项,整个Widget可以滚动,常见效果照片瀑布流.
基本用法

创建一个列表

继承关系

Object > Diagnosticable > DiagnosticableTree > Widget > StatelessWidget > ScrollView > BoxScrollView > GridView

构造方法
GridView({Key key, Axis scrollDirection: Axis.vertical, bool reverse: false, ScrollController controller, bool primary, ScrollPhysics physics, bool shrinkWrap: false, EdgeInsetsGeometry padding, @required SliverGridDelegate gridDelegate, bool addAutomaticKeepAlives: true, bool addRepaintBoundaries: true, bool addSemanticIndexes: true, double cacheExtent, List<Widget> children: const [], int semanticChildCount })
GridView.builder({Key key, Axis scrollDirection: Axis.vertical, bool reverse: false, ScrollController controller, bool primary, ScrollPhysics physics, bool shrinkWrap: false, EdgeInsetsGeometry padding, @required SliverGridDelegate gridDelegate, @required IndexedWidgetBuilder itemBuilder, int itemCount, bool addAutomaticKeepAlives: true, bool addRepaintBoundaries: true, bool addSemanticIndexes: true, double cacheExtent, int semanticChildCount })
GridView.count({Key key, Axis scrollDirection: Axis.vertical, bool reverse: false, ScrollController controller, bool primary, ScrollPhysics physics, bool shrinkWrap: false, EdgeInsetsGeometry padding, @required int crossAxisCount, double mainAxisSpacing: 0.0, double crossAxisSpacing: 0.0, double childAspectRatio: 1.0, bool addAutomaticKeepAlives: true, bool addRepaintBoundaries: true, bool addSemanticIndexes: true, double cacheExtent, List<Widget> children: const [], int semanticChildCount, DragStartBehavior dragStartBehavior: DragStartBehavior.start })
GridView.custom({Key key, Axis scrollDirection: Axis.vertical, bool reverse: false, ScrollController controller, bool primary, ScrollPhysics physics, bool shrinkWrap: false, EdgeInsetsGeometry padding, @required SliverGridDelegate gridDelegate, @required SliverChildDelegate childrenDelegate, double cacheExtent, int semanticChildCount, DragStartBehavior dragStartBehavior: DragStartBehavior.start })
GridView.extent({Key key, Axis scrollDirection: Axis.vertical, bool reverse: false, ScrollController controller, bool primary, ScrollPhysics physics, bool shrinkWrap: false, EdgeInsetsGeometry padding, @required double maxCrossAxisExtent, double mainAxisSpacing: 0.0, double crossAxisSpacing: 0.0, double childAspectRatio: 1.0, bool addAutomaticKeepAlives: true, bool addRepaintBoundaries: true, bool addSemanticIndexes: true, List<Widget> children: const [], int semanticChildCount, DragStartBehavior dragStartBehavior: DragStartBehavior.start })
属性介绍
示例代码:
class MyGridView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('GridView'),
        backgroundColor: Colors.black,
      ),
      body: _getWidgetCustom(context),
    );
  }

  //生成数据源
  List<String> getDataList() {
    List<String> mLists = [];
    for (int i = 0; i < 50; i++) {
      mLists.add(i.toString());
    }
    return mLists;
  }

  //生成gridView widget
  List<Widget> getWidgetList() {
    return getDataList().map((item) => getChildWidget(item)).toList();
  }

  Widget getChildWidget(String item) {
    return Container(
      alignment: Alignment.center,
      child: Text(
        item,
        style: TextStyle(color: Colors.white, fontSize: 20),
      ),
      color: Colors.blue,
    );
  }

  //count方式构造
  Widget _getWidgetCount(BuildContext context) {
    return GridView.count(
      crossAxisCount: 3,
      crossAxisSpacing: 20.0,
      mainAxisSpacing: 10.0,
      childAspectRatio: 1.0,
      padding: EdgeInsets.all(5.0),
      children: getWidgetList(),
    );
  }

  //builder--SliverGridDelegateWithFixedCrossAxisCount方法
  Widget _getWidgetBuilder(BuildContext context) {
    return GridView.builder(
        itemCount: getDataList().length,
        padding: EdgeInsets.all(5.0),
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 3,
          mainAxisSpacing: 20.0,
          crossAxisSpacing: 10.0,
          childAspectRatio: 1.0,
        ),
        itemBuilder: (BuildContext context, int index) {
          return getChildWidget(getDataList()[index]);
        });
  }

  //builder--SliverGridDelegateWithMaxCrossAxisExtent方式
  Widget _getWidgetBuilder2(BuildContext context) {
    return GridView.builder(
        itemCount: getDataList().length,
        padding: EdgeInsets.all(5.0),
        gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
          maxCrossAxisExtent: 100.0, //单个子widget的水平最大宽度
          mainAxisSpacing: 20.0, //水平子widget的间距
          crossAxisSpacing: 10.0, //垂直间距
          childAspectRatio: 1.0,
        ),
        itemBuilder: (BuildContext context, int index) {
          return getChildWidget(getDataList()[index]);
        });
  }

  //custom方式构造
  Widget _getWidgetCustom(BuildContext context) {
    return GridView.custom(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 4,
            mainAxisSpacing: 20.0,
            crossAxisSpacing: 20.0,
            childAspectRatio: 1.0),
        childrenDelegate:
            SliverChildBuilderDelegate((BuildContext context, int position) {
          return getChildWidget(getDataList()[position]);
        }, childCount: getDataList().length));
  }
}

效果:
image.png
上一篇下一篇

猜你喜欢

热点阅读