【问题标题】:Flutter Gesture detector onTap problemsFlutter 手势检测器 onTap 问题
【发布时间】:2019-02-07 07:50:27
【问题描述】:

我是 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 个蓝色网格单元,当我按下其中任何一个时,没有任何变化!

提前致谢:)

【问题讨论】:

  • 问题或错误是什么?预期的行为是什么?实际行为是什么?
  • 非常感谢您的评论。预期的行为是出现 9 个 redgridcells,当我按下其中一个文本变为 X 并且其颜色变为浅蓝色时,第二次按下另一个单元格将显示文本 O,颜色为浅蓝色,第三个文本为 X,依此类推。实际行为是 9 个蓝色网格单元,当我按下其中任何一个时,我在终端中写入的 ui 中出现错误
  • 这没有发生?发生了什么?
  • 抱歉,我编辑了最后一条评论并添加了实际行为。你能检查一下吗?

标签: user-interface flutter gesturedetector


【解决方案1】:

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

【讨论】:

  • 我把它初始化为choice=' ';但是网格单元最初看起来仍然是蓝色的,不知道为什么,单击单元格后文本没有更新
【解决方案2】:

color: Colors.transparent, 添加到 GestureDetector 的 Container 子项

您的小部件将看起来:

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

【讨论】:

    猜你喜欢
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 2012-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    相关资源
    最近更新 更多