我有一个简单的网格,它比屏幕实际空间更大,可以向上和向下滚动。
每个单元格都有一个
问题在于,一旦我将更改后的单元格滚出视图,状态就不会保持。
有什么想法吗?
CellWidget
单元格窗口小部件
每个单元格都有一个
onTap()
方法来更改单元格颜色。问题在于,一旦我将更改后的单元格滚出视图,状态就不会保持。
有什么想法吗?
class GridWidget extends StatefulWidget {
@override
_GridWidgetState createState() => new _GridWidgetState();
}
class _GridWidgetState extends State<GridWidget> {
@override
Widget build(BuildContext context) {
Color cellColor = Colors.white;
return new GridView.count(
crossAxisCount: 10,
children: new List.generate(100, (index) {
return new CellWidget(
index: index,
color: cellColor,
text: new Text(index.toString()),
);
}),
);
}
}
CellWidget
单元格窗口小部件
...
class _CellWidgetState extends State<CellWidget> {
Color cellColor = Colors.white;
Text cellText = new Text('white');
@override
void initState() {
super.initState();
cellColor = widget.color;
cellText = widget.text;
}
_changeCell(index) {
setState(() {
cellColor = Colors.lightBlue;
});
}
@override
Widget build(BuildContext context) {
return new GestureDetector(
onTap: () => _changeCell(widget.index),
child: new Container(
width: double.infinity,
height: double.infinity,
child: new Center(child: cellText),
),
);
}
}
PageState
(抱歉只能在手机上) - Günter Zöchbauercolor: cellColor
应该依赖于该点击。 - Rene