Flutter手势探测器onTap问题

问题描述 投票:1回答:1

我是新手,我正在尝试制作tic tac toe游戏;尽管在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个蓝色网格单元,当我按下其中任何一个没有任何变化!

提前致谢:)

user-interface flutter gesturedetector
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.