2

在这里,我有一个清单。我想在我的 GridView.count 中访问该列表中的特定位置。

就像我有一个列表一样,a = [133, 118, 117, 116, 115, 114];

我想弄清楚在我生成了 225 个单元格的 List.generate 中,如何访问存储在此列表中的单元格,a = [133, 118, 117, 116, 115, 114]; 就像我想从 225 个单元格中访问该列表的第 4 项(116)。

                     a = [133, 118, 117, 116, 115, 114];

                          child: GridView.count(
                            crossAxisCount: 15,
                            childAspectRatio: 1,
                            children: List<Widget>.generate(
                              225,
                              (index) {
                                return Stack(
                                  children: [
                                    GridTile(
                                      child: Container(
                                        decoration: BoxDecoration(
                                          borderRadius:
                                              BorderRadius.circular(5),
                                          border:
                                              Border.all(color: Colors.black),
                                        ),
                                      ),
                                    ),
                                  ],
                                );
                              },
                            ),
                          ),
4

1 回答 1

1

这是一个解决方案,通过传入您想要查找的索引并将其与单元格的索引匹配(如下面的代码):

在此处输入图像描述

import 'package:flutter/material.dart';

class GridExample extends StatefulWidget {
  const GridExample({Key? key}) : super(key: key);

  @override
  _GridExampleState createState() => _GridExampleState();
}

class _GridExampleState extends State<GridExample> {
  final a = [133, 118, 117, 116, 115, 114];

  @override
  Widget build(BuildContext context) {
    return Container(
      child: GridView.count(
        crossAxisCount: 15,
        childAspectRatio: 1,
        children: List<Widget>.generate(
          225,
          (index) {
            return Stack(
              children: [
                GridTile(
                  child: Tile(
                    index: index,
                    accessedCell: a[3],
                  ),
                ),
              ],
            );
          },
        ),
      ),
    );
  }
}

class Tile extends StatefulWidget {
  Tile({
    required this.index,
    required this.accessedCell,
    Key? key,
  }) : super(key: key);

  final int index;
  final int accessedCell;

  @override
  State<Tile> createState() => _TileState();
}

class _TileState extends State<Tile> {
  bool _isAccessed = false;

  @override
  Widget build(BuildContext context) {
    print(widget.accessedCell);
    print(widget.index);

    if (widget.accessedCell == widget.index) {
      _isAccessed = true;
    }

    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(5),
        border: Border.all(color: _isAccessed ? Colors.blue : Colors.red),
      ),
      child:
          FittedBox(fit: BoxFit.contain, child: Text(widget.index.toString())),
    );
  }
}

于 2021-12-06T07:25:20.327 回答