使用子女的ShowDialog已被弃用,如何以另一种方式使用它?

时间:2018-05-02 22:17:56

标签: dart flutter

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

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

以上工作正常,但是它声明{@ 1}}参数已被弃用,另一种方法是:

  

'不使用“child”参数,而是从a返回一个孩子   关闭'       '提供给“建设者”的论点。这将确保BuildContext'       '适用于在对话框中构建的小部件。'

我不确定这意味着什么。这里有任何简单的例子。

4 个答案:

答案 0 :(得分:14)

改变它

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

答案 1 :(得分:1)

孩子已弃用。如果您查看此属性,则可以发出此警告。

  

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

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

我的加载器函数中的示例用法

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);

答案 2 :(得分:1)

我们可以将文本小部件分配给 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'))
    ],
  );

并使用与上述相同的内容。

答案 3 :(得分:0)

这对我有用。

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