简体   繁体   English

如何在 Flutter 中增加 GridView 中的索引?

[英]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更改为itemBuilderitemBuilderIndex所示:

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.

相关问题 如何在颤动的列表视图中增加每张卡片的索引? - How to increment an index for each card in a listview in flutter? FLUTTER - 如何获取GridView横向索引? - FLUTTER - How to obtain the GridView horizontal index? 如何在Flutter中访问GridView中特定项目的索引? - How to access the index of a particular item in GridView in flutter? 如何获得 Flutter 中的 gridview 索引? - How can I get the gridview index in Flutter? 使用索引生成 GridView 和 flutter - Generate GridView with flutter using index 如何在 Flutter 上使用 gridview 将硬编码的 1 个项目添加到列表中的最后一个索引中 - How to add 1 item hardcoded into last index in list using gridview on Flutter 如何增加 Flutter 中的特定值? - How to increment for a specific in Flutter? 如何在 Flutter 中重新订购 GridView? - How to reorder a GridView in Flutter? Flutter:如何使用 Flutter 中的 Navigator.push 在 GridView.builder 的每个索引中设置可点击系统 - Flutter: how to set clickable systems in each index at GridView.builder with Navigator.push in Flutter Flutter:如何在 Swiper 中完成特定逻辑后显示下一个索引,其中 GridView 也在 Swiper 中设置? - Flutter : how to show next index after complete a specific logic in Swiper, where GridView also set in Swiper?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM