gpt4 book ai didi

flutter - 我可以使用showDialog在Flutter中向后传播数据吗?

转载 作者:行者123 更新时间:2023-12-03 04:46:33 25 4
gpt4 key购买 nike

Future<bool> show(BuildContext context) async {
return Platform.isIOS
? await showCupertinoDialog<bool>
(context: context, builder: (context)=>this)
:await showDialog<bool>(
context: context,
builder: (context) => this,
);
}

任何人都可以帮助我理解术语“this”,“this”指的是什么以及showDialog如何工作以返回Future。我试图阅读文档但仍然无法理解?它与AlertDialog小部件一样吗?

最佳答案

好吧,这几乎是文档所说的,它在您应用的当前内容上方显示一个实质性对话框,对于this,它将当前小部件作为该对话框的子级传递,至于返回值就像普通页面导航一样您调用pop(context, {value})方法时,您还可以返回一个值,以便从对话框中返回pop内部的值。

这是下面的示例:

class DialogTest extends StatefulWidget {
@override
_DialogTestState createState() => _DialogTestState();
}

class _DialogTestState extends State<DialogTest> {
// the value that will be typed to the dialog
String dialogText;
// the value that will be returned from the dialog
String returnedFromDialog;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Sample Code'),
),
body: Center(
child:
Text('You got this value from the dialog => $returnedFromDialog'),
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
returnedFromDialog = await showDialog<String>(
context: context,
builder: (context) {
return AlertDialog(
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
TextField(
onChanged: (value) => dialogText = value,
),
FlatButton(
onPressed: () {
setState(() => Navigator.pop(context, dialogText));
},
child: Text(
'Close dialog',
style: TextStyle(color: Colors.red),
),
)
],
),
);
});
},
child: Icon(Icons.open_in_browser),
),
);
}
}

the results

关于flutter - 我可以使用showDialog在Flutter中向后传播数据吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62173297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com