У меня есть виджет с сохранением состояния, который имеет простую сетку, и в каждой ячейке сетки есть контейнер.
Я хотел бы нажать на ячейку / контейнер и изменить его содержимое.
Проблема в том, что метод GestureDetector -> onTap запускается при обновлении приложения для всех клеток.
В приведенном ниже примере метод _changeCell
запускается сразу для всех клеток, а onTap
не работает.
Есть идеи?
import 'package:flutter/material.dart';
class GridWidget extends StatefulWidget {
@override
_GridWidgetState createState() => new _GridWidgetState();
}
class _GridWidgetState extends State<GridWidget> {
@override
Widget build(BuildContext context) {
Color cellColor = Colors.white;
Text cellText = new Text('');
// when a cell is tapped, change the color and text
_changeCell(index) {
setState(() {
cellColor = Colors.lightBlue;
cellText = new Text('clicked');
});
print("Container clicked " + index.toString());
}
// create a 5 by 5 grid
return new GridView.count(
crossAxisCount: 5,
children: new List.generate(5, (index) {
return new GestureDetector(
onTap: _changeCell(index),
child: new Container(
width: double.infinity,
height: double.infinity,
decoration: new BoxDecoration(
color: cellColor,
),
child: new Center(
child: cellText,
),
),
);
}),
);
}
}