futter学习

Flutter的staggered GridView详细使用

2020-05-26  本文已影响0人  Mr_panmin

一、简介

flutter staggered gridview是一个支持多列网格大小不同的布局,且Android、iOS、Web都适用
在这种布局中每个单元格都可以称为一个Tile。
它有以下几种特性:

二、使用

2.1、pubspec.yaml添加依赖

dependencies:
   flutter_staggered_grid_view:

2.2、导包

import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart’;

2.3、6种创建方式

区别:

2.4、StaggeredTile的使用

三、应用场景

3.1、无法确定GridView中的item的高度,所以无法设置宽高比,这种情况可以使用StaggeredGridView来自动适配高度

StaggeredGridView.countBuilder(
  shrinkWrap: true,
    controller: _scrollController,
    crossAxisCount: 4,
    crossAxisSpacing: 4,
    mainAxisSpacing: 10,
    itemCount: _count,
    itemBuilder: (context, index) {
      return Container(
          color: Colors.green,
          child: new Center(
            child: new CircleAvatar(
              backgroundColor: Colors.white,
              child: new Text('$index'),
            ),
          ));
    },
    staggeredTileBuilder: (index) =>
        StaggeredTile.fit(2))

3.2、瀑布流样式

StaggeredGridView.countBuilder(
  shrinkWrap: true,
    controller: _scrollController,
    crossAxisCount: 4,
    crossAxisSpacing: 4,
    mainAxisSpacing: 10,
    itemCount: _count,
    itemBuilder: (context, index) {
      return Container(
          color: Colors.green,
          child: new Center(
            child: new CircleAvatar(
              backgroundColor: Colors.white,
              child: new Text('$index'),
            ),
          ));
    },
    staggeredTileBuilder: (index) =>
        StaggeredTile.count(2, index == 0 ? 2.5 : 3))

3.3、配合RefreshIndicator实现下拉刷新

CustomScrollView(
  controller: _scrollController,
  slivers: <Widget>[
    SliverToBoxAdapter(
      child: RefreshIndicator(
        onRefresh: () async {
          await Future.delayed(Duration(seconds: 5));
        },
        child: StaggeredGridView.countBuilder(
          shrinkWrap: true,
            controller: _scrollController,
            crossAxisCount: 4,
            crossAxisSpacing: 4,
            mainAxisSpacing: 10,
            itemCount: _count,
            itemBuilder: (context, index) {
              return Container(
                  color: Colors.green,
                  child: new Center(
                    child: new CircleAvatar(
                      backgroundColor: Colors.white,
                      child: new Text('$index'),
                    ),
                  ));
            },
            staggeredTileBuilder: (index) =>
                StaggeredTile.count(2, index == 0 ? 2.5 : 3)),
      ),
    ),
  ],
))

四、常见问题总结(持续更新)

4.1、嵌套CustomScrollView使用时无法滑动

1、升级StaggeredGridView的版本,据说0.3.0以上版本已经解决

2、StaggeredGridView设置shrinkWrap:true

上一篇下一篇

猜你喜欢

热点阅读