如何在Flutter Grid Builder中每10个网格后添加一个完全拉伸的黄色容器?

cnwbcb6i  于 2023-06-30  发布在  Flutter
关注(0)|答案(2)|浏览(112)

我有一个Flutter应用程序,我使用网格生成器来显示网格项目。每隔10个网格后,我想插入一个完全拉伸的黄色容器来分隔各部分。例如,第一行应包含网格1至5,第二行应包含网格6至10,然后在下一行显示网格11至15之前,应插入黄色容器。该图案应当继续直到网格的末端(例如,100个网格)。
我已经附上了当前的代码和图像供参考。你能指导我如何实现这个功能吗?先谢谢你。

GridView.builder(
        itemCount: 100,
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 5,
        ),
        itemBuilder: (BuildContext context, int index) {
          if ((index + 1) % 11 == 0) {
            // Add yellow color stretch container after every 10th container
            return Container(
              color: Colors.yellow,
              height: 50,
              width: double.infinity,
            );
          } else {
            // Regular square rounded shape container
            return Container(
              margin: const EdgeInsets.all(8),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10),
                color: Colors.white,
              ),
              child: Center(
                child: Text('${index + 1}'),
              ),
            );
          }
        },
      )

mpbci0fu

mpbci0fu1#

我的看法是...对子组使用.skip和.take:

final items = Iterable<int>.generate(47).toList();

class Body extends StatelessWidget {
  const Body({
    super.key,
  });

  @override
  Widget build(BuildContext context) => ListView.separated(
        itemBuilder: (context, index) {
          final group = items.skip(index * 10).take(10).toList();
          if (group.isEmpty) return null;
          return GridView.count(
            crossAxisCount: 5,
            shrinkWrap: true,
            children: [
              for (final item in group)
                Container(
                  margin: const EdgeInsets.all(8),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(10),
                    color: Colors.blue,
                  ),
                  child: Center(
                    child: Text('$item'),
                  ),
                )
            ],
          );
        },
        separatorBuilder: (context, index) => const Divider(
          thickness: 32,
          height: 40,
          color: Colors.yellow,
        ),
        itemCount: (items.length / 10).ceil(),
      );
}
9o685dep

9o685dep2#

这里有一个简单的例子。

class TestScreen extends StatelessWidget {
  const TestScreen({super.key});

  @override
  Widget build(BuildContext context) {
    // The number you want to sperate the data.
    const int splitCount = 10;

    // Items that you have. Change this to suit your conditions.
    final List<int> items = List.generate(
      100,
      (index) => index,
    );

    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: const Text('Staggered GridView'),
      ),
      body: SafeArea(
        child: SingleChildScrollView(
          child: TestStaggerdGridView(
            items: items,
            splitCount: splitCount,
            crossAxisCount: 5,
          ),
        ),
      ),
    );
  }
}

class TestStaggerdGridView extends StatelessWidget {
  const TestStaggerdGridView({
    super.key,
    required this.items,
    required this.splitCount,
    required this.crossAxisCount,
  });

  /// Items that you have. Change this to suit your conditions.
  final List<int> items;

  /// The number you want to seprate the items.
  final int splitCount;

  /// The number of cell count in an axis.
  final int crossAxisCount;

  @override
  Widget build(BuildContext context) {
    final int addedCount = items.length ~/ (splitCount + 1);

    return StaggeredGrid.count(
      axisDirection: AxisDirection.down,
      crossAxisCount: crossAxisCount,
      children: List.generate(
        items.length + addedCount,
        (index) {
          if ((index + 1) % (splitCount + 1) == 0) {
            return StaggeredGridTile.count(
              mainAxisCellCount: 1,
              crossAxisCellCount: crossAxisCount,
              child: _stretchedItem(),
            );
          } else {
            int itemIndex = (index - (index ~/ (splitCount + 1)));

            return StaggeredGridTile.count(
              mainAxisCellCount: 1,
              crossAxisCellCount: 1,
              child: _normalItem(itemIndex),
            );
          }
        },
      ),
    );
  }

  Widget _normalItem(int itemIndex) {
    return Container(
      margin: const EdgeInsets.all(8),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10),
        color: Colors.white,
      ),
      child: Center(child: Text('${itemIndex + 1}')),
    );
  }

  Widget _stretchedItem() {
    return Container(
      margin: const EdgeInsets.all(8),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10),
        color: Colors.yellow,
      ),
    );
  }
}

相关问题