[英]How to increment index in a GridView in Flutter?
I have a GridView
that is as shown below.我有一个如下图所示的
GridView
。 I am trying to section it out in smaller GridView
s and have a separator in between.我正在尝试将其划分为较小的
GridView
,并在两者之间有一个分隔符。
However instead of showing me a continuous list, the elements from 0-5 keep repeating.然而,0-5 的元素并没有向我展示一个连续的列表,而是不断重复。 I am trying to continue the elements in batches of 6 where the first batch is 0-5 and the next is from 6-11 and so on..
我正在尝试以 6 个批次继续元素,其中第一批是 0-5,下一批是 6-11,依此类推..
This is my code:这是我的代码:
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.separated(
separatorBuilder: (context, int) {
return Divider(color: Colors.black,);
},
// shrinkWrap: true,
itemBuilder: (BuildContext context, int index) {
return GridView.count(
shrinkWrap: true,
crossAxisCount: 3,
childAspectRatio: 2.0,
children: List.generate(6, (index) {
return Center(
child: RaisedButton(
onPressed: (){},
color: Colors.greenAccent,
child: Text(
'$index AM',
),
),
);
}),
);
},
itemCount: 4,
));
}
This is what the GridView
looks like:这是
GridView
的样子:
You can use the index from the itembuilder to know which group of 6 you're generating.您可以使用 itembuilder 中的索引来了解您正在生成哪一组 6。
Change the name index
in itemBuilder
to itemBuilderIndex
like so:将 itemBuilder 中的名称
index
更改为itemBuilder
, itemBuilderIndex
所示:
itemBuilder: (BuildContext context, int itemBuilderIndex) {
And do this to get the calculated number you want:并这样做以获得您想要的计算数字:
'${(itemBuilderIndex * 6) + index} AM'
(6 being the number of items you have per segment) (6 是您每个段拥有的项目数)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.