我正在尝试按以下方式使用 showDialog
showDialog(context: context,child:new Text("Hello Dialgo"));
以上工作正常,但它声明 child
参数已被弃用,替代方法是:
'Instead of using the "child" argument, return the child from a closure ' 'provided to the "builder" argument. This will ensure that the BuildContext ' 'is appropriate for widgets built in the dialog.'
我不确定这意味着什么。任何简单的例子都将不胜感激。
最佳答案
改一下
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
关于dart - 不推荐使用使用 child 的 ShowDialog 如何以其他方式使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50144356/