用户第一次单击时已更改,但第二次未更改

时间:2019-07-31 16:27:35

标签: flutter dart

我使用容器小部件设计了一个圆圈。我需要在用户单击时更改BoxDecoration。它仅更改第一次单击。然后用户再次单击,我需要再次更改

   GestureDetector(
           onTap: () {
              setState(() {
                radiocheck = true;//problem is here, if user click second time always true, is that way to reverse dicision
              });
          },
          child: Container(
                    height: 14.0,
                    width: 14.0,
                    decoration:radiocheck?
                                   BoxDecoration(
                                     color: Color(0xFF4A90E2),
                                     shape: BoxShape.circle,          
                                   ):                     
                                   BoxDecoration(
                                     border: Border.all(color:Color(
                                             0xFF4A90E2), width: 1),
                                     shape: BoxShape.circle,
                                     ),
                                   ),
                 ),

1 个答案:

答案 0 :(得分:1)

将容器分隔为新功能效果最佳。然后使用bool或int检查以前是否按下了按钮。

类似:

Widget _someFunction(){
   return GestureDetector(
           onTap: () {
              setState(() {}); // this should already get the correct radiocheck
          // otherwise, you can remove it from the container below and make if/else statement here
          },
          child: _buildContainer();
}

Widget _buildContainer(){
  BoxDecoration boxDecoration;

  if(radiocheck){
    boxDecoration = BoxDecoration(
      color: Color(0xFF4A90E2),
      shape: BoxShape.circle,
    );
    radiocheck = false;
  } else {
    boxDecoration= BoxDecoration(
      border: Border.all(color:Color(
          0xFF4A90E2), width: 1),
      shape: BoxShape.circle,
    );
    radiocheck = true;
  }

  return Container(
    height: 14.0,
    width: 14.0,
    decoration: boxDecoration,
  );
}

编辑:在您编辑的消息中,我看到您想要/需要更改状态。