Flutter - Radio Animation is not showing up on showDialog

后端 未结 1 1083
予麋鹿
予麋鹿 2021-01-03 09:38

I\'m trying to create a Radio in a showDialog, however the animation that occurs on Radio does not appear in showDialog.<

相关标签:
1条回答
  • 2021-01-03 09:52

    Remember that components are immutable. When you call showDialog, the content of that dialog won't change even if HomePage does.

    The solution is easy. You need to refactor a bit your code to something like :

        showDialog(
            context: context,
            builder: (context) => MyForm()
        )
    

    and instead of changing the state of HomePage, you instead change the state of MyForm.

    example :

    class Test extends StatelessWidget {
      void onSubmit(String result) {
        print(result);
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: RaisedButton(
              onPressed: () => showDialog(context: context, builder: (context) => MyForm(onSubmit: onSubmit)),
              child: Text("dialog"),
            ),
          ),
        );
      }
    }
    
    typedef void MyFormCallback(String result);
    
    class MyForm extends StatefulWidget {
      final MyFormCallback onSubmit;
    
      MyForm({this.onSubmit});
    
      @override
      _MyFormState createState() => _MyFormState();
    }
    
    class _MyFormState extends State<MyForm> {
      String value = "foo";
    
      @override
      Widget build(BuildContext context) {
        return SimpleDialog(
          title: Text("My form"),
          children: <Widget>[
            Radio(
              groupValue: value,
              onChanged: (value) => setState(() => this.value = value),
              value: "foo",
            ),
            Radio(
              groupValue: value,
              onChanged: (value) => setState(() => this.value = value),
              value: "bar",
            ),
            FlatButton(
              onPressed: () {
                Navigator.pop(context);
                widget.onSubmit(value);
              },
              child: new Text("submit"),
            )
          ],
        );
      }
    }
    
    0 讨论(0)
提交回复
热议问题