如何取消选择RadioListTile Flutter

时间:2020-06-08 20:20:17

标签: flutter radio-button

我想通过onPress取消选择RadioListTile的另一个按钮。这是我的代码,有人可以帮忙吗?

groupvalue和value来自服务器动态!

            RadioListTile(
                    groupValue: _con.cart.tips.toString(),
                    title: Row(
                      children: <Widget>[
                        Expanded(
                          flex: 3,
                          child: Column(
                            crossAxisAlignment:
                                CrossAxisAlignment.start,
                            children: <Widget>[
                              Text(
                                _con.cart.defaultTipsOptions[index]
                                    .toString(),
                                style: TextStyle(
                                    fontSize: 16,
                                    color:
                                        _con.cart.defaultTipsOptions[
                                                    index] ==
                                                _con.cart.tips
                                            ? Theme.of(context)
                                                .accentColor
                                            : Colors.blueGrey),
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                    value: _con.cart.defaultTipsOptions[index]
                        .toString(),
                    onChanged: (val) {
                      state(() {
                        _selectedAmount =
                            double.parse(val).toString();
                        _con.cart.tips = double.parse(val);
                      });
                    },
                            activeColor: Theme.of(context).accentColor),

1 个答案:

答案 0 :(得分:2)

_value传递给您的groupValue。您可以将其初始化为返回的数据。 然后在按下按钮时清除该值,例如:

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  double _value = 1;
  double option1 = 1;
  double option2 = 2;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        RadioListTile(
          groupValue: _value.toString(),
          title: Row(
            children: <Widget>[
              Expanded(
                flex: 3,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(
                      option1.toString(),
                      style: TextStyle(
                        fontSize: 16,
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
          value: option1.toString(),
          onChanged: (val) {
            setState(() {
              _value = double.parse(val);
            });
          },
          activeColor: Theme.of(context).accentColor,
        ),
        RadioListTile(
          groupValue: _value.toString(),
          title: Row(
            children: <Widget>[
              Expanded(
                flex: 3,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(
                      option2.toString(),
                      style: TextStyle(
                        fontSize: 16,
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
          value: option2.toString(),
          onChanged: (val) {
            setState(() {
              _value = double.parse(val);
            });
          },
          activeColor: Theme.of(context).accentColor,
        ),
        RaisedButton(
          child: Text('Unselect'),
          onPressed: () {
            setState(() {
              _value = null;
            });
          },
        )
      ],
    );
  }
}