dart - Flutter - 广播动画未显示在 showDialog 上

标签 dart flutter

我正在尝试在 showDialog 中创建 Radio,但是 Radio 上发生的动画并未出现在 中显示对话框.

例如:在 foo2 中点击时没有任何反应,当您在 showDialog 中退出并返回时,foo2 被选中。

下面是代码和显示正在发生的事情的 gif:

enter image description here

import "package:flutter/material.dart";

void main() {
  runApp(new ControlleApp());
}

class ControlleApp extends StatelessWidget {  
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: "My App",
      home: new HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  HomePageState createState() => new HomePageState();
}

enum _RadioGroup {
  foo1,
  foo2
}

class HomePageState extends State<HomePage> {
  _RadioGroup _itemType = _RadioGroup.foo1;

  void changeItemType(_RadioGroup type) {
    setState(() {
      _itemType = type;
    });
  }

  void showDemoDialog<T>({ BuildContext context, Widget child }) {
    showDialog<T>(
      context: context,
      child: child,
    );
  }

  @override
  Widget build(BuildContext context){
    return new Scaffold( 
      appBar: new AppBar(backgroundColor: new Color(0xFF26C6DA)),
      body: new Container(
        child: new Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new InkWell(
              onTap: (){
                showDemoDialog<String>(
                  context: context,
                  child: new SimpleDialog(
                    title: const Text("show"),
                    children: <Widget>[
                      new Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          new Radio<_RadioGroup>(
                            groupValue: _itemType,
                            value: _RadioGroup.foo1,
                            onChanged: changeItemType
                          ),
                          const Text("foo1"),

                          new Radio<_RadioGroup>(
                            groupValue: _itemType,
                            value: _RadioGroup.foo2,
                            onChanged: changeItemType
                          ),
                          const Text("foo2"),
                        ],
                      )
                    ],
                  )
                );
              },
              child: new Container(
                margin: new EdgeInsets.only(top: 16.0, bottom: 8.0),
                child: new Text("Show"),
              ),
            )
          ],
        ),
      )
    );
  }
}

最佳答案

请记住,组件是不可变的。 当您调用 showDialog 时,即使 HomePage 改变,该对话框的内容也不会改变。

解决方法很简单。您需要将代码重构为以下内容:

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

而不是更改 HomePage 的状态,而是更改 MyForm 的状态。

示例:

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

关于dart - Flutter - 广播动画未显示在 showDialog 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46690765/

相关文章:

dart - 如何使用 http 库捕获 SocketException?

ios - 如何在 android-studio 中生成 .ipa 文件?

xml - 收到的XML响应是乱码

android - Flutter 无法准备运行隔离

dart - 有没有关于 build.dart 的文档?

android - 任务 ':app:processDebugResources' 的 Flutter 执行失败。无法解析配置 ':app:debugCompileClasspath' 的所有依赖项

dart - 在 Dart 中按类映射

android - Flutter- "extendBody: true"完全没有任何作用。我想扩展导航栏后面的主体

flutter - 在 flutter 中 mockito 上缺少 stub 错误。尝试在模拟的 SharedPreferences 上使用 setString

dart - 加载应用程序时,如何禁止 RegistrationPage 短暂出现/闪烁?