如何从 Flutter 中的网格视图计数访问生成列表中的特定单元格?
How to access specific cell in generated List from grid view count in Flutter?
这里,我有一个列表。我想在我的 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),
),
),
),
],
);
},
),
),
这是一个解决方案,通过传入您要查找的内容的索引并将其与单元格的索引匹配(按照下面的代码):
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())),
);
}
}
这里,我有一个列表。我想在我的 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),
),
),
),
],
);
},
),
),
这是一个解决方案,通过传入您要查找的内容的索引并将其与单元格的索引匹配(按照下面的代码):
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())),
);
}
}