📅  最后修改于: 2023-12-03 15:30:49.472000             🧑  作者: Mango
在Flutter中,GridView可以用于展示一个二维网格列表,其中每个单元格可以包含任意的widget,比如图片、文本等等。本文将对GridView的组成结构、属性和常用方法进行介绍。
GridView有两个重要的概念:行和列。
一行中包含多个列,每个列中可以放置一个widget。
GridView的常见属性有:
scrollDirection
:滚动方向,可选值有Axis.horizontal
和Axis.vertical
。crossAxisCount
:每行(或每列)的列数。crossAxisSpacing
:列之间的间隔。mainAxisSpacing
:行之间的间隔。padding
:GridView内容的内边距。children
:GridView中包含的子widget。以下是常见属性的详细描述:
GridView({
Key? key,
Axis scrollDirection = Axis.vertical,
required SliverGridDelegate gridDelegate,
bool reverse = false,
ScrollController? controller,
bool primary,
ScrollPhysics? physics,
bool shrinkWrap = false,
EdgeInsetsGeometry? padding,
@Deprecated('Use clipBehavior parameter instead. ' 'This feature was deprecated after v1.19.0.')
bool? addAutomaticKeepAlives,
bool addRepaintBoundaries = true,
bool addSemanticIndexes = true,
double cacheExtent = RenderViewport.defaultCacheExtent,
List<Widget> children = const <Widget>[],
int? semanticChildCount,
DragStartBehavior dragStartBehavior = DragStartBehavior.start,
ScrollViewKeyboardDismissBehavior keyboardDismissBehavior = ScrollViewKeyboardDismissBehavior.manual,
String? restorationId,
})
scrollDirection
Axis.vertical
。Axis
枚举类型,可选值有Axis.horizontal
和Axis.vertical
。crossAxisCount
gridDelegate
gridDelegate
的具体实现类如下:
SliverGridDelegateWithMaxCrossAxisExtent
GridView(
gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 120,
childAspectRatio: 4 / 3,
mainAxisSpacing: 10,
crossAxisSpacing: 10,
),
)
SliverGridDelegateWithFixedCrossAxisCount
GridView(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 2 / 1,
mainAxisSpacing: 10,
crossAxisSpacing: 10,
),
)
SliverGridDelegateWithFixedCrossAxisExtent
GridView(
gridDelegate: SliverGridDelegateWithFixedCrossAxisExtent(
crossAxisExtent: 120,
),
)
reverse
controller
ScrollController
,可选。primary
physics
ScrollPhysics
,可选。shrinkWrap
padding
EdgeInsetsGeometry
,可选。addRepaintBoundaries
addSemanticIndexes
children
List<Widget>
,可选。semanticChildCount
dragStartBehavior
DragStartBehavior
枚举类型,可选值有DragStartBehavior.start
和DragStartBehavior.down
。keyboardDismissBehavior
ScrollViewKeyboardDismissBehavior
枚举类型,可选值有ScrollViewKeyboardDismissBehavior.manual
、ScrollViewKeyboardDismissBehavior.onDrag
和ScrollViewKeyboardDismissBehavior.none
。restorationId
以下是GridView常用的方法:
scrollTo
index
:目标位置的索引值。scrollToCell
row
:目标单元格所在行的索引值。column
:目标单元格所在列的索引值。jumpTo
offset
:目标位置的偏移量。animateTo
offset
:目标位置的偏移量。duration
:动画时长。curve
:动画方式,比如匀速、减速等。以上就是关于GridView的介绍,希望对大家有所帮助。如果你也想深入学习Flutter,请移步Flutter官网。