Flutter GridView 控件

一个可滚动的2D排列的小部件。

控件网格的主轴方向是它滚动的方向( scrollDirection)。

最常用的网格布局是 GridView.count,它创建了一个在横轴上具有固定数量 网格块 的平铺的布局,和 GridView.extent ,它使用具有最大横轴范围的 网格块 创建布局。自定义 SliverGridDelegate 可以生成任意2D排列的子代,包括未对齐或重叠的排列。

要创建具有大量(或无限)个子节点的网格,请将 GridView.builder 构造函数与gridDelegateSliverGridDelegateWithFixedCrossAxisCountSliverGridDelegateWithMaxCrossAxisExtent 一起使用。

要使用自定义SliverChildDelegate ,请使用 GridView.custom

要创建线性数组的子代,请使用 ListView

若要控制滚动视图的初始滚动偏移,请为控制器 controller 提供其ScrollController.initialScrollOffset 属性集。

转换到CustomScrollView

GridView基本上是一个 CustomScrollView ,其 CustomScrollView.slivers 中包含一个SliverGrid

如果GridView不再足够,例如因为滚动视图既有网格又有列表,或者因为网格要与 SliverAppBar 等组合在一起,直接将代码从使用 GridView 移植到 CustomScrollView 直接使用。

CustomScrollView.slivers 属性只能使用包含 SliverGrid 的列表。

GridView上 的 childrenDelegate 属性对应于 SliverGrid.delegate 属性,GridView上 的 gridDelegate 属性对应于 SliverGrid.gridDelegate 属性。

GridView,GridView.count和GridView.extent构造函数的子参数对应于 GridView 中 childrenDelegate 是具有相同参数的 SliverChildListDelegate

GridView.builder 构造函数的 itemBuilder 和 childCount 参数对应于 GridView 中的 childrenDelegate,它们是具有匹配参数的 SliverChildBuilderDelegate

GridView.count 和 GridView.extent 构造函数创建自定义网格委托,并在 SliverGrid上 具有等效命名的构造函数以简化转换:分别为 SliverGrid.count 和 SliverGrid.extent。

padding 属性对应于在 CustomScrollView.slivers 属性中使用 SliverPadding 而不是网格本身,并且让 SliverGrid 成为 SliverPadding 的子级。

默认情况下,ListView将自动填充列表的可滚动范围,以避免MediaQuery的padding 造成的部分障碍。要避免此行为,请使用padding属性为0进行覆盖。

将代码移植到使用CustomScrollView后,可以将其他slivers(例如 SliverListSliverAppBar)放入CustomScrollView.slivers 中。

以下是使用 CustomScrollView 显示 GridView 等效的代码片段:

GridView.count(
  primary: false,
  padding: const EdgeInsets.all(20.0),
  crossAxisSpacing: 10.0,
  crossAxisCount: 2,
  children: <Widget>[
    const Text('He\'d have you all unravel at the'),
    const Text('Heed not the rabble'),
    const Text('Sound of screams but the'),
    const Text('Who scream'),
    const Text('Revolution is coming...'),
    const Text('Revolution, they...'),
  ],
)
CustomScrollView(
  primary: false,
  slivers: <Widget>[
    SliverPadding(
      padding: const EdgeInsets.all(20.0),
      sliver: SliverGrid.count(
        crossAxisSpacing: 10.0,
        crossAxisCount: 2,
        children: <Widget>[
          const Text('He\'d have you all unravel at the'),
          const Text('Heed not the rabble'),
          const Text('Sound of screams but the'),
          const Text('Who scream'),
          const Text('Revolution is coming...'),
          const Text('Revolution, they...'),
        ],
      ),
    ),
  ],
)

也可以看看:

构造函数

使用自定义SliverGridDelegate创建可滚动的2D小部件数组
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 <widget>[]</widget>, int semanticChildCount })

创建 按需创建的可滚动的2D小部件数组
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 })

创建一个可滚动的2D小部件数组,在横轴上具有固定数量的网格块
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 <widget>[]</widget>, int semanticChildCount })

使用自定义SliverGridDelegate和自定义SliverChildDelegate创建可滚动的2D小部件数组
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 })

使用每个都具有最大横轴范围的 网格块 创建可滚动的2D小部件数组。
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 <widget>[]</widget>, int semanticChildCount })

属性

名称 类型 描述
childrenDelegateSliverChildDelegate 为GridView提供子代的委托
gridDelegateSliverGridDelegate 一个控制 GridView 中子项布局的委托。
cacheExtentdouble 视口在可见区域之前和之后有一个区域,用于缓存在用户滚动时即将变为可见的项目
controllerScrollController 一个对象,可用于控制滚动滚动视图的位置
hashCodeint 此对象的哈希码
keyKey 控制一个小部件如何替换树中的另一个小部件
paddingEdgeInsetsGeometry 插入子代的内边距
physicsScrollPhysics 滚动视图应如何响应用户输入,物理滚动方式
primarybool 这是否是与父 PrimaryScrollController 关联的主滚动视图。
reversebool 滚动视图是否在读取方向上滚动
runtimeTypeType 表示对象的运行时类型
scrollDirectionAxis 滚动视图的滚动轴
semanticChildCountint 将提供语义信息的子代数量
shrinkWrapbool 是否应该由正在查看的内容确定scrollDirection中滚动视图的范围。

方法

名称 类型 描述
buildChildLayout(BuildContext context) → Widget 子类应重写此方法以构建布局模型
build(BuildContext context) → Widget 描述此窗口小部件表示的用户界面部分
buildSlivers(BuildContext context) → List<Widget> 构建要放置在视口内的窗口小部件列表
buildViewport(BuildContext context, ViewportOffset offset, AxisDirection axisDirection, List<Widget> slivers) → Widget 构建视口
createElement() → StatelessElement 创建StatelessElement以管理此窗口小部件在树中的位置。
debugDescribeChildren() → List<DiagnosticsNode> 返回描述此节点的子节点的DiagnosticsNode对象列表。
debugFillProperties(DiagnosticPropertiesBuilder properties) → void 添加与节点关联的其他属性
getDirection(BuildContext context) → AxisDirection 返回滚动视图滚动的AxisDirection
noSuchMethod(Invocation invocation) → dynamic 访问不存在的方法或属性时调用
toDiagnosticsNode({String name, DiagnosticsTreeStyle style }) → DiagnosticsNode 返回调试工具和toStringDeep使用的对象的调试表示形式
toString({DiagnosticLevel minLevel: DiagnosticLevel.debug }) → String 返回此对象的字符串表示形式
toStringDeep({String prefixLineOne: '', String prefixOtherLines, DiagnosticLevel minLevel: DiagnosticLevel.debug }) → String 返回此节点及其后代的字符串表示形式。
toStringShallow({String joiner: ', ', DiagnosticLevel minLevel: DiagnosticLevel.debug }) → String 返回对象的单行详细描述
toStringShort() → String 这个小部件的简短文字描述
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 229,362评论 6 537
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 99,013评论 3 423
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 177,346评论 0 382
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 63,421评论 1 316
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 72,146评论 6 410
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 55,534评论 1 325
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 43,585评论 3 444
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 42,767评论 0 289
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 49,318评论 1 335
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 41,074评论 3 356
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 43,258评论 1 371
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 38,828评论 5 362
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 44,486评论 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 34,916评论 0 28
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 36,156评论 1 290
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 51,993评论 3 395
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 48,234评论 2 375