Ashta
Ashta

Reputation: 191

How to trigger radio button when text or another widget is tapped?

I create Row with 2 widgets like this

Row(
   children: [
      new Radio(
        value: 0,
        groupValue: rv0,
        onChanged: _handleRadioValueChange1,
      ),

      GestureDetector(
         onTap: (){ //trigger radio button },
         child: Text('Tap Me')
      )
   ],
)

So, when I tap on the text, the radio will be triggered, for example in javascript with jquery

$('#radio').click()

How to do it in dart flutter? thanks

Upvotes: 6

Views: 5845

Answers (2)

Code Runner
Code Runner

Reputation: 1028

    Row(
   children: [
      new Radio(
        value: 0,
        groupValue: rv0,
        onChanged: _handleRadioValueChange1,
      ),

      GestureDetector(
         onTap: (){ 
        //trigger radio button
//you can pass any value which is the value of respective radio button
_handleRadioValueChange1(0)
 },
         child: Text('Tap Me')
      )
   ],
)

or you can go for the RadioListTile widget.

Upvotes: 9

AskNilesh
AskNilesh

Reputation: 69681

if you want to select radio button value on click of your textview Then you need change groupValue of your radio button on click of your textview

SAMPLE CODE

class MyApp extends StatefulWidget {
  @override
  _State createState() => _State();
}

class _State extends State<MyApp> {
  int _radioValue1 = 0;

  void _handleRadioValueChange1(int value) {
    setState(() {
      _radioValue1 = value;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('RadioListTile Demo'),
      ),
      //hit Ctrl+space in intellij to know what are the options you can use in flutter widgets
      body: SingleChildScrollView(
        child: Container(
          padding: EdgeInsets.all(32.0),
          child: Column(
            children: <Widget>[
              Radio(
                value: 0,
                groupValue: _radioValue1,
                onChanged: _handleRadioValueChange1,
              ),
              Radio(
                value: 1,
                groupValue: _radioValue1,
                onChanged: _handleRadioValueChange1,
              ),
              Radio(
                value: 2,
                groupValue: _radioValue1,
                onChanged: _handleRadioValueChange1,
              ),
              GestureDetector(
                onTap: () {
                  setState(() {
                    _radioValue1 = 0;
                  });
                },
                child: Text("Select First Radio Button"),
              ),
              GestureDetector(
                onTap: () {
                  setState(() {
                    _radioValue1 = 1;
                  });
                },
                child: Text("Select Second Radio Button"),
              ),
              GestureDetector(
                onTap: () {
                  setState(() {
                    _radioValue1 = 2;
                  });
                },
                child: Text("Select Third Radio Button"),
              )
            ],
          ),
        ),
      ),
    );
  }
}

Upvotes: 6

Related Questions