【问题标题】:ShowDialog using children is deprecated how to use it the other way?不推荐使用使用儿童的 ShowDialog 如何以其他方式使用它?
【发布时间】:2018-05-02 22:17:55
【问题描述】:

我正在尝试按以下方式使用 showDialog

showDialog(context: context,child:new Text("Hello Dialgo"));

以上工作正常,但它指出child 参数已被弃用,替代方法是:

'不使用“child”参数,而是从 闭包' '提供给“builder”参数。这将确保 BuildContext ' '适用于对话框中内置的小部件。'

我不确定这意味着什么。任何简单的例子都将不胜感激。

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    改一下

    showDialog(
       context: context,
       builder: (_) => new Text("Hello Dialgo")
    );
    

    如果您需要对话框中的上下文,请将 builder: (_) => 更改为 builder: (BuildContext context) =>

    由于 Builder 是一个函数处理程序,我们需要创建一个接受单个参数 (BuildContext) 并返回一个 Widget 的函数。

    语法可以是:

    (BuildContext context) => new Text('...');
    

    (BuildContext context) {
        return new Text('...')
    }
    

    它们是等价的,虽然第二个可以有不止一行

    在此处查看示例:https://github.com/aqwert/flutter_auth_starter/blob/master/lib/core/dialogs/showError_dialog.dart

    【讨论】:

    • 你能告诉我这里发生了什么吗?
    • 你刚刚添加了一个 lambda 吗?
    • 是的,Lambda 只是函数的简写,所以使用 { return } 也是正确的
    • 啊,好吧,我和(_) 混淆了。你能告诉我这是做什么的吗
    • 好的,抱歉,这只是一种方法,但我不会使用它(或关心它)。您可以随意称呼它,但这似乎是一个不成文的约定
    【解决方案2】:

    孩子已弃用。如果你查看 this 属性,你可以看到这个警告。

    不使用“child”参数,而是从提供给“builder”参数的闭包中返回子节点。这将确保 BuildContext 适用于对话框中构建的小部件。

    如果您想使用 builder,只需编写一个返回您的小部件的函数。

    在我的加载器函数中的使用示例

    void showLoader(BuildContext context) {
      showDialog(context: context, builder: (BuildContext context) => new ProgressHUD(
        color: Colors.white,
        containerColor: Theme.of(context).primaryColor,
      ));
    }
    

    用法

    // Start to show loader
    showLoader(context);
    // Do a async job and wait it
    await do();
    // Hide the loader
    Navigator.pop(context);
    

    【讨论】:

      【解决方案3】:

      我们可以将文本小部件分配给 alert 变量,如下所示:

      var alert = new Text("Hello dialog");

      由于 child 已被弃用:

      showDialog(context: context, child: alert);

      我们可以这样写:

      showDialog(context: context, builder: (_) => alert);

      如果你想创建更复杂的对话框,你可以像这样重新定义alert

      var alert = new AlertDialog(
          title: new Text('App'),
          content: new Text(message),
          actions: <Widget>[
            new FlatButton(onPressed: () {Navigator.pop(context);},
                child: new Text('OK'))
          ],
        );
      

      并像上面一样使用它。

      【讨论】:

      • 我这是不推荐使用的解决方案,但在 showDialog 出现后,即使使用 builder (_)=>alert,问题在 showDialog 处同样变暗,对此有任何想法吗?谢谢
      【解决方案4】:

      这对我有用。

      showDialog(
              context: context,
              builder: (BuildContext context) => new AlertDialog(
                title: new Text('Warning'),
                content: new Text('Hi this is Flutter Alert Dialog'),
                actions: <Widget>[
                  new IconButton(
                      icon: new Icon(Icons.close),
                      onPressed: () {
                        Navigator.pop(context);
                      })
                ],
              ));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-12-22
        • 2012-05-04
        • 1970-01-01
        • 2020-03-10
        • 2016-02-08
        • 1970-01-01
        • 2020-10-21
        • 1970-01-01
        相关资源
        最近更新 更多