【问题标题】:How to unselect a radio button in flutter?如何取消选择颤振中的单选按钮?
【发布时间】:2020-03-04 19:17:51
【问题描述】:

我使用单选地图、文本字段、显示结果的文本和清除所有按钮。

TextField 可以被删除:

RaisedButton(onPressed: () { _controllerTextField.clear();},
                  child: Text('Clear',style:Theme.of(context).textTheme.body2.merge(TextStyle(color: Colors.white)),),)

但我不明白如何删除我的文本并将我的收音机置于默认取消选择状态。

更多,当一个收音机被选中时,我给它一个自定义颜色,但我不能给收音机图标同样的颜色。

你知道要使用的方法吗?

谢谢

【问题讨论】:

  • 您能否为您要执行的所有操作添加代码?我在您的问题中找不到单选按钮或您要解决的地图。

标签: flutter dart radio


【解决方案1】:

使用 Radio 的 toggleable 属性。

【讨论】:

    【解决方案2】:

    感谢卡里姆的建议。

    抱歉,我正忙于其他事情。 最后,我做到了:

    onPressed: () {
        _controller.clear();
        setState(() {
        radioSelectionne = 3;
         nombreTextField = null;
         Resultat = null;});
         },
    

    对于收音机的颜色:在我的主文件中 -> 主题数据 -> unselectedWidgetColor

    一切如我所愿。

    感谢您的帮助。

    【讨论】:

      【解决方案3】:
      import 'package:flutter/cupertino.dart';
      import 'package:flutter/material.dart';
      
      class Test extends StatefulWidget {
        Test({Key key, this.title}) : super(key: key);
        final String title;
      
        @override
        _TestState createState() => _TestState();
      }
      
      class _TestState extends State<Test> {
      
        final TextEditingController _controllerTextField =  TextEditingController();
      
      
        double nombreTextField;
        int radioSelectionne;
        int resultat;
      
        Map coefMultipicateur = {
          0: 'x 2',
          1: 'x 5',
          2: 'x 10',
        };
      
      
        @override
        Widget build(BuildContext context) {
          return  GestureDetector(
            onTap: (() => FocusScope.of(context).requestFocus(FocusNode())),
            child: Scaffold(
              appBar: AppBar(
                title: Text('Test'),
              ),
              body: SingleChildScrollView(
                child: Padding(
                  padding: EdgeInsets.only(left:10.0, right: 10.0),
                  child: Column(
                    children: <Widget>[
                      Container(
                          margin: EdgeInsets.only(top: 10.0, bottom: 50.0),
                          child:
                          Row(
                            children: <Widget>[
                              Expanded(
                                  flex:6,
                                  child: Text('entrer nombre :',
                                    style:Theme.of(context).textTheme.body2.merge(
                                        TextStyle(
                                            fontSize: 16)),
                                  )
                              ),
                              Expanded(
                                flex: 4,
                                child: TextField(
                                  controller: _controllerTextField,
                                  keyboardType: TextInputType.number,
                                  onChanged: (String string) {
                                    setState(() {
                                      nombreTextField = double.tryParse(string);
                                    });
                                  },
                                  decoration: InputDecoration(
                                    labelText: 'entrer nombre',
                                  ),
                                ),
                              )
                            ],
                          )
                      ),
                      Container(
                        margin: EdgeInsets.only(top: 10.0, bottom: 20.0),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            Expanded(
                              flex: 3,
                              child: Container(
                                color: Colors.blue,
      
                                child:columnRadio(),
                              ),
                            ),
                            Expanded(
                                flex: 7,
                                child: Center(
                                    child: Column(
                                      children: <Widget>[
                                        Text('resultat\n= ',
                                          textAlign: TextAlign.center,
                                          style:Theme.of(context).textTheme.title.merge(TextStyle(color: Colors.blueGrey)),),
                                        Container(
                                          margin: EdgeInsets.only(bottom: 10.0),
                                          child: Text(nombreTextField == null || radioSelectionne == null ? '' : '$resultat',
                                            style:Theme.of(context).textTheme.title.merge(TextStyle(color: Colors.green)),),
                                        ),
                                      ],
                                    )
                                )
                            )
                          ],
                        ),
                      ),
                      Container(
                          margin: EdgeInsets.only(top: 50.0),
                          child: RaisedButton(
                            onPressed: () {
                              _controllerTextField.clear();
                            },
                            color: Colors.blue,
                            child: Text('Clear',
                              style:Theme.of(context).textTheme.body2.merge(TextStyle(color: Colors.white)),
                            ),
                          )
                      )
                    ],
                  ),
                ),
              ),
            ),
          );
        }
      
        Column columnRadio() {
          List<Widget> pourcentages = [];
          coefMultipicateur.forEach((key, value) {
            Row listePourcentages = Row(
              children: <Widget>[
                Radio(
                    activeColor: Colors.white,
                    value: key,
                    groupValue: radioSelectionne,
                    onChanged: (Object i) {
                      setState(() {
                        radioSelectionne = i;
                        if (nombreTextField != null) {
                          switch (radioSelectionne) {
                            case 0:
                              resultat = (nombreTextField * 2).toInt();
                              break;
                            case 1:
                              resultat = (nombreTextField * 5).toInt();
                              break;
                            case 2:
                              resultat = (nombreTextField *  10).toInt();
                              break;
                          }
                        }
                      });
                    }),
                Text(value,
                    style: TextStyle(
                        color: (radioSelectionne == key) ? Colors.white : Colors.blue[200],
                        fontWeight: FontWeight.bold)),
              ],
            );
            pourcentages.add(listePourcentages);
          });
          return Column(
            children: pourcentages,
          );
        }
      }
      

      【讨论】:

        【解决方案4】:

        如果你只能选择一个选项;为单选按钮指定groupValue

        例子:

        const TYPE_DOG = 1
        const TYPE_CAT = 2
        
                              Radio(
                                value: TYPE_DOG,
                                groupValue: type,
                                onChanged: (value) => setState(() => type = value),
                              ),
                              Radio(
                                value: TYPE_CAT,
                                groupValue: type,
                                onChanged: (value) => setState(() => type = value),
                              ),
        
        

        然后在onPressed中将此值设置为0;

        【讨论】:

        • 感谢卡里姆的建议。对不起,我正忙于其他事情。最后,我做到了: onPressed: () { _controller.clear(); setState(() { radioSelectionne = 3; nombreTextField = null; Resultat = null;}); },
        猜你喜欢
        • 1970-01-01
        • 2020-06-09
        • 1970-01-01
        • 2020-06-21
        • 2012-02-21
        • 2011-07-17
        • 2013-10-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多