0

I am new to flutter and dart. I have tried to insert few radio buttons and a raised button within a ModalBottomSheet. The expected output is that, the button should be disabled until any option is selected from the radio buttons, and once any option is selected the button should be enabled. In the code I have tried, there is an issue that the radio buttons are not getting selected as soon I click on them, instead they get selected once I close and reopen the bottom popup screen. Also, I'm unable to write code for disabling and enabling the button as I am unaware of it. Any suggestions would be very much helpful, thanks in advance!

class ReturnReason extends StatefulWidget {
  @override
  _ReturnReasonState createState() => _ReturnReasonState();
}

class _ReturnReasonState extends State<ReturnReason> {
  int selectedRadio;
  @override
 void initSate() {
    super.initState();
    selectedRadio = 0;
  }
  setSelectedRadio(int val) {
    setState(() {
      selectedRadio = val;
    });
  }
@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          'Flutter Project',
          style: TextStyle(
            fontSize: 18.0,
            color: Colors.blue,
            fontWeight: FontWeight.bold,
          ),
        ),
        backgroundColor: Colors.black12,
        iconTheme: IconThemeData(color: Colors.black),
      ),
      body: Center(
        child: RaisedButton(
          onPressed: () {
            _bottomSheet(context);
          },
          color: Colors.deepPurple,
          padding: EdgeInsets.all(10.0),
          child: Text(
            'Click me',
            style: TextStyle(color: Colors.white, fontSize: 18.0),
          ),
        ),
      ),
    );
  }

  _bottomSheet(context) {
    showModalBottomSheet(
        context: context,
        builder: (BuildContext bc) {
          return SingleChildScrollView(
              child: Container(
            height: MediaQuery.of(context).size.height * .80,
            decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(30),
                  topRight: Radius.circular(30),
                )),
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: Column(
                children: <Widget>[
                  Container(
                    width: 0.8 * MediaQuery.of(context).size.height,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text('Select Return Reason'),
                      ],
                    ),
                  ),
                  Container(
                    width: 0.8 * MediaQuery.of(context).size.width,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text('Improper description'),
                        Radio(
                          value: 1,
                          groupValue: selectedRadio,
                          onChanged: (val) {
                            setSelectedRadio(val);
                          },
                        ),
                      ],
                    ),
                  ),
                  Divider(),
                  Container(
                    width: 0.8 * MediaQuery.of(context).size.width,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text('Size issue'),
                        Radio(
                         value: 2,
                          groupValue: selectedRadio,
                          onChanged: (val) {
                          setSelectedRadio(val);
                          },
                        ),
                      ],
                    ),
                  ),
                  Divider(),
                  Container(
                    width: 0.8 * MediaQuery.of(context).size.width,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text('Other'),
                        Radio(
                          value: 3,
                          groupValue: selectedRadio,
                          onChanged: (val) {
                           setSelectedRadio(val);
                          },
                        ),
                      ],
                    ),
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Container(
                        width: 0.9 * MediaQuery.of(context).size.width,
                        height: 0.075 * MediaQuery.of(context).size.height,
                        child: RaisedButton(
                          onPressed: () => {
                            _bottomSheet2(context)
                          },
                          shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(10.0)),
                          padding: EdgeInsets.all(0.0),
                          child: Ink(
                            decoration: BoxDecoration(
                                gradient: LinearGradient(
                                  begin: Alignment.bottomCenter,
                                  end: Alignment.topCenter,
                                ),
                                borderRadius: BorderRadius.circular(10.0)),
                            child: GestureDetector(
                              onTap: () {
                                _bottomSheet2(context);
                              },
                              child: Container(
                                constraints: BoxConstraints(
                                    maxWidth: MediaQuery.of(context).size.width,
                                    minHeight: 0.075 *
                                        MediaQuery.of(context).size.height),
                                alignment: Alignment.center,
                                child: Text('Continue'),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ));
        });
  }
yamini r
  • 153
  • 1
  • 15
  • Does this answer your question? [How to refresh an AlertDialog in Flutter?](https://stackoverflow.com/questions/51962272/how-to-refresh-an-alertdialog-in-flutter) – mkobuolys May 20 '21 at 11:30
  • Sorry, that didn't work for me. Actually the solution there was to use a setState() function, but I've used that function in my code already. – yamini r May 20 '21 at 13:07
  • Seems like you skipped the most important part: StatefulBuilder is used inside the builder where you are creating the content for your bottom sheet, did you try that? – mkobuolys May 20 '21 at 16:46
  • I'm sorry, I'm not sure of what you said, is that the StatefulWidget? But that is not inside any other builder, that's the beginning of my code. – yamini r May 21 '21 at 03:30
  • https://stackoverflow.com/a/57240941/15427566 You can find the StatefulBuilder example in this answer. – mkobuolys May 21 '21 at 06:27
  • Thank you very much, that method of using StatefulBuilder within the builder of bottom sheet worked! Can you also please suggest me on that button enabling and disabling process? – yamini r May 21 '21 at 09:02
  • What you can do is make a boolean and set it to false. If the boolean is false then the button is disabled, then once you click on one of the options, using setState(), change the boolean to true. This can be accomplished easily by using a ternary operator, just put the boolean in front of the `onPressed` function. https://www.w3adda.com/dart-tutorial/dart-conditional-operators – Unbreachable May 22 '21 at 00:02
  • I tried a lot of ways, but the doubt I have is that, can I set two variables for Value, onChanged and setState under Radio buttons to call this function of enabling the button as well as selection of the radio button? – yamini r May 25 '21 at 03:40

0 Answers0