颤振关闭条件内的对话框

时间:2018-10-15 07:53:35

标签: dart flutter

我正在尝试动态关闭对话框。 我实际上想做的就是根据我目前的信息来更改对话框的内容。

从加载信息开始,没有按钮,几秒钟后,单击确定按钮关闭对话框可能会出错。

class Dialogs{
  loginLoading(BuildContext context, String type, String description){
    var descriptionBody;

    if(type == "error"){
      descriptionBody = CircleAvatar(
        radius: 100.0,
        maxRadius: 100.0,
        child: new Icon(Icons.warning),
        backgroundColor: Colors.redAccent,
      );
    } else {
      descriptionBody = new Center(
        child: new CircularProgressIndicator(),
      );
    }

    return showDialog(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context){
        return AlertDialog(
          title: descriptionBody,
          content: SingleChildScrollView(
            child: ListBody(
              children: <Widget>[
                Center(child: Text(description))
              ],
            ),
          ),
        );
      }
    );
  }
}

因此,在创建实例后,打开对话框并打开对话框

Dialogs _dialog = new Dialogs();
_dialog.loginLoading(context, "loading", "loading...");

// Close the dialog code here
don't know how to do it

// Call again the AlertDialog with different content.

2 个答案:

答案 0 :(得分:2)

https://docs.flutter.io/flutter/material/showDialog.html

  

通过此方法创建的对话框路由被推送到根导航器。如果应用程序具有多个Navigator对象,则可能有必要调用Navigator.of(context,rootNavigator:true).pop(result)关闭对话框,而不仅仅是Navigator.pop(context,result)。

因此以下任何一项都可以为您工作

  • Navigator.of(上下文,rootNavigator:true).pop(结果)
  • Navigator.pop(上下文,结果)

答案 1 :(得分:0)

您无需关闭并重新打开对话框。而是让flutter处理对话框更新。框架为此进行了优化。

这是一个可以运行的示例应用程序,您可以将其用作起点(只需添加自己的Dialogs类):

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'MyApp',
      home: Login(
        child: Home(),
      ),
    );
  }
}

class Home extends StatefulWidget {
  final Dialogs dialog = Dialogs();
  @override
  State<StatefulWidget> createState() => HomeState();
}

class HomeState extends State<Home> {
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    Future.delayed(Duration(milliseconds: 50)).then((_) {
      widget.dialog.loginLoading(
        context,
        LoginStateProvider.of(context).type,
        LoginStateProvider.of(context).description,
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Updating Dialog'),
      ),
      body: Container(),
    );
  }
}

class Login extends StatefulWidget {
  final Widget child;
  Login({@required this.child});

  @override
  State<StatefulWidget> createState() => LoginState();
}

class LoginState extends State<Login> {
  String type = 'wait';
  String description = 'foo';

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    Future.delayed(Duration(milliseconds: 2000)).then((_) {
      setState(() {
        type = 'error';
        description = 'bar';
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return LoginStateProvider(widget.child, type, description);
  }
}

class LoginStateProvider extends InheritedWidget {
  final String type;
  final String description;
  LoginStateProvider(Widget child, this.type, this.description)
      : super(child: child);
  @override
  bool updateShouldNotify(LoginStateProvider old) {
    return type != old.type || description != old.description;
  }

  static LoginStateProvider of(BuildContext context) =>
      context.inheritFromWidgetOfExactType(LoginStateProvider);
}