Rock
Rock

Reputation: 551

How to unselect RadioListTile Flutter

I want to unselect RadioListTile with onPress by another button. Here is my code anybody can help ?

The groupvalue and value are coming dynamically from server !

            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),

Upvotes: 0

Views: 873

Answers (1)

Sami Haddad
Sami Haddad

Reputation: 1426

Pass a _value to your groupValue. You can initialize it to be your returned data. And then clear the value when the button is pressed, for example:

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;
            });
          },
        )
      ],
    );
  }
}

Upvotes: 2

Related Questions