颤振中的切换按钮

问题描述

我很陌生,因此,如果这个问题感到微不足道或不需要,我深表歉意!

我有一个Toggle小部件,其中包含一个带有5个子级的Row小部件,但是我还添加了4个SizedBox来创建5个Container小部件之间的区别。

我希望5个容器在单击时显示不同的颜色,并存储与它们关联的int值。

我关注了官方文档和YouTube视频,但听不清。 这是我的代码:

ToggleButtons(
                            isSelected: <bool>[true,false,false],children: <Widget>[
                              GestureDetector(
                                child: Container(
                                    height: 85,width: 85,decoration: BoxDecoration(
                                        color: _colour,borderRadius: BorderRadius.circular(10)
                                    ),child: Padding(
                                      padding: EdgeInsets.all(8),child: Image(
                                        image: AssetImage('images/happy_icon.png'),width: 75,),)
                                ),onTap: () {
                                  setState(() {
                                    remind = 1;
                                  });
                                },SizedBox(
                                width: 8,// ... 3 more widgets like these

GestureDetector(
                                child: Container(
                                    height: 85,child: Image(
                                        image: AssetImage('images/sad.png'),onTap: () {
                                  setState(() {
                                    remind = 5;
                                  });
                                },)
selectedColor: _selectedColour,onPressed: (int index) {
                              setState(() {
                                for (int buttonIndex = 0; buttonIndex < isSelected.length; buttonIndex++) {
                                  if (buttonIndex == index) {
                                    isSelected[buttonIndex] = !isSelected[buttonIndex];
                                  } else {
                                    isSelected[buttonIndex] = false;
                                  }
                                }
                              });
                            },

enter image description here

解决方法

尝试仅使用带有子项的Column,这些子项是包裹在GestureDetector中的按钮,然后onTap()设置颜色的状态,然后保存整数。

示例:

          Column(
            children: [
              GestureDetector(
                onTap: () {
                  associatedInt = 1;
                  setState(() {
                    if(containerColor == Colors.white)
                      containerColor = Colors.blue;
                    else
                      containerColor = Colors.white;
                  });
                },child: Container(
                  width: 100,height: 100,color: containerColor,),],

我只用了一个孩子,但您可以再用一个。希望这会有所帮助。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...