【问题标题】:Radio Button active color in flutter doesn't work颤动中的单选按钮活动颜色不起作用
【发布时间】:2021-02-28 16:35:16
【问题描述】:

所以我试图制作两个具有男性和女性价值的单选按钮输入。当我按下单选按钮时,它确实打印了值,但是,单选按钮活动颜色不会改变或保持白色。当我按下单选按钮时,当我按下它时,activecolor 设置为黑色。

Scaffold(
          body:Container(
                            margin:EdgeInsets.fromLTRB(40, 100, 80, 0),
                            child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Container(
                                child:Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: [
                                   Radio(
                              groupValue:genderController.text,
                              value:"Male",
                              activeColor: Colors.black,
                              onChanged:(value){
                                setState(() {
                                  genderController.text=value;
                                  print(genderController.text);
                                  
                                });
                              }
                            ),
                              Text("Male"
                                ,style:TextStyle(fontSize: 10),textAlign: TextAlign.justify,
                              ),
                                ],
                                )
                              ),
                             
                             Container(
                                child:Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: [
                                Container(
                                child: Radio(
                                  groupValue:genderController.text,
                              value:"Female",
                              activeColor: Colors.black,
                               onChanged:(value){
                                setState(() {
                                  genderController.text=value;
                                  print(genderController.text);
                                });
                              }
                            ),
                          ),
                          Text("Female"
                            ,style:TextStyle(fontSize: 10,),textAlign: TextAlign.justify,
                          ),
                            ],
                            )
                          ),
                        ],
                     ),
     ));

https://i.stack.imgur.com/3299H.png https://i.stack.imgur.com/G8rWE.png

【问题讨论】:

    标签: flutter


    【解决方案1】:

    如链接中所述: https://api.flutter.dev/flutter/material/Radio-class.html

    //Variable of your state 
    String _yourGroupValue = "Male"; //This would show you the pre-selected
    
    Radio(
          groupValue:_yourGroupValue,
          value:"Male",
          activeColor: Colors.black,
          onChanged:(value){
          print(value);
          setState(() {
            _yourGroupValue = value;
          });
    }),
    

    为你另一个单选按钮做同样的事情。

    【讨论】:

      【解决方案2】:

      我认为您的错误与您的值未动态设置这一事实有关。

      这里有一个工作示例:https://api.flutter.dev/flutter/material/Radio-class.html

      【讨论】:

        猜你喜欢
        • 2018-05-16
        • 1970-01-01
        • 2022-01-13
        • 1970-01-01
        • 2019-09-04
        • 2021-09-08
        • 1970-01-01
        • 2021-06-18
        • 1970-01-01
        相关资源
        最近更新 更多