gpt4 book ai didi

user-interface - Flutter Gesture detector onTap 问题

转载 作者:IT王子 更新时间:2023-10-29 07:06:17 25 4
gpt4 key购买 nike

我是 Flutter 的新手,我正在尝试制作井字游戏;尽管在 Flutter GestureDetector, onTap gets triggered automatically, how to? 中遵循相同的概念,但我还是有一些 ontap

我的代码返回最初带有红色和空白文本的网格单元

return Scaffold(
appBar: AppBar(title: Text('Tic Tac Toe')),
body: GridView.count(
crossAxisCount: 3,
crossAxisSpacing: 2.0,
mainAxisSpacing: 2.0,
children: List<Widget>.generate(
9,
(int index){
return new GridCell(
index:index,
color: Colors.red,
text:Text(' '),
);
})));

那么gridcell的类是:

class GridCell extends StatefulWidget {
final Color color;
final Text text;
final int index;


GridCell({Key key, this.color,this.text,this.index}) :
super(key: key);

@override
GridCellState createState() {
return new GridCellState();
}
}

class GridCellState extends State<GridCell> {
Color cellColor=Colors.white;
Text cellText=new Text(' ');
String choice=' ';

@override
void initState() {
super.initState();
choice;
cellColor=widget.color;
cellText=widget.text;
}
//get text function to switch the x and o between the players
String _getText(){

if (choice=='X'){
choice='O';
}
else{
choice='X';
}
return choice;
}
_changeCell(index) {
setState(() {
switch (index) {
case 0:
cellColor = Colors.lightBlue;
cellText = new Text(choice);
print(_getText());
break;
case 1:
cellColor = Colors.lightBlue;
cellText = new Text(_getText());
print(_getText());
print(_getText());

break;
case 2:
cellColor = Colors.lightBlue;
cellText = new Text(_getText());
print(_getText());

break;

case 3:
cellColor = Colors.lightBlue;
cellText = new Text(_getText());
print(_getText());

break;
case 4:
cellColor = Colors.lightBlue;
cellText = new Text(_getText());
print(_getText());

break;
case 5:
cellColor = Colors.lightBlue;
cellText = new Text(_getText());
print(_getText());

break;
case 6:
cellColor = Colors.lightBlue;
cellText = new Text(_getText());
print(_getText());

break;
case 7:
cellColor = Colors.lightBlue;
cellText = new Text(_getText());
print(_getText());

break;
case 8:
cellColor = Colors.lightBlue;
cellText = new Text(_getText());
print(_getText());

break;


}
});
}

@override
Widget build(BuildContext context) {
return new GestureDetector(
onTap:()=>_changeCell(widget.index),
child:Container(
height:20.0,
color:Theme.of(context).primaryColor,
),
);
}
}

预期的行为是出现 9 个 redgridcells,当我按下其中一个时,它的文本变成 X 并且它的颜色变成浅蓝色,第二次按下另一个单元格将有文本 O 和浅蓝色,第三个的文本是 X 等等上。实际行为是 9 个蓝色网格单元,当我按下其中任何一个时,没有任何变化!

提前致谢:)

最佳答案

您收到错误是因为 choice 被初始化为 null,并且在将它与 Text(choice) 或条件语句一起使用之前从未真正具有值。

关于user-interface - Flutter Gesture detector onTap 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52129690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com