[英]How to close the SimpleDialog in flutter after choosing an option
I added a SimpleDialog
with 2 options: lost and found.我添加了一个带有 2 个选项的SimpleDialog
:丢失和找到。 Whenever I make my selection and get redirected to where I want, the SimpleDialog doesn't close and stays on my screen.每当我做出选择并被重定向到我想要的位置时, SimpleDialog 不会关闭并停留在我的屏幕上。
The switch:开关:
switch (
await showDialog(
context: context,
child: new SimpleDialog(
title: new Text("Which category?"),
children: <Widget>[
new SimpleDialogOption(child: new Text("Found"),
onPressed: () {
goToCreate();
},
),
new SimpleDialogOption(child: new Text("Lost"),
onPressed: () {
//Whatever
},
),
],
)
)
)
And the cases:和案例:
{
case "Found":
goToCreate();
break;
case "Lost":
//Whatever
break;
}
You can do this from the dialog when you press Accept (or whatever):当您按“接受”(或其他任何方式)时,您可以从对话框中执行此操作:
Navigator.pop(context, true); // You could return any Dart type, like an enum
From the caller:来自来电者:
bool dialogReturnValue = await showDialog(...);
if (dialogReturnValue == true){
// do something
}
From official docs: https://docs.flutter.io/flutter/material/SimpleDialog-class.html来自官方文档: https : //docs.flutter.io/flutter/material/SimpleDialog-class.html
You need to execute inside options' onPressed method this:您需要在选项的 onPressed 方法中执行以下操作:
Navigator.pop(context, ===arguments===);
Full example:完整示例:
SimpleDialog(
title: const Text('Select assignment'),
children: <Widget>[
SimpleDialogOption(
onPressed: () { Navigator.pop(context, Department.treasury); },
child: const Text('Treasury department'),
),
SimpleDialogOption(
onPressed: () { Navigator.pop(context, Department.state); },
child: const Text('State department'),
),
],
);
EDIT:编辑:
switch (
await showDialog(
context: context,
child: new SimpleDialog(
title: new Text("Which category?"),
children: <Widget>[
new SimpleDialogOption(child: new Text("Found"),
onPressed: () {
Navigator.pop(context, 'Found'); //Close the SimpleDialog then=>
goToCreate();
},
),
new SimpleDialogOption(child: new Text("Lost"),
onPressed: () {
Navigator.pop(context, 'Lost'); //For closing the SimpleDialog
//After that do whatever you want
},
),
],
)
)
)
)
EDIT 2 (Demo Application):编辑 2(演示应用程序):
import 'package:flutter/material.dart';
void main() {
runApp(App());
}
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Test(),
);
}
}
class Test extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(onPressed: () {
_askedToLead(context);
}),
),
);
}
Future<void> _askedToLead(BuildContext context) async {
switch (await showDialog<String>(
context: context,
builder: (BuildContext context) {
return SimpleDialog(
title: const Text('Select assignment'),
children: <Widget>[
SimpleDialogOption(
onPressed: () {
Navigator.pop(context, 'Found');
},
child: const Text('FOUND'),
),
SimpleDialogOption(
onPressed: () {
Navigator.pop(context, 'Lost');
},
child: const Text('LOST'),
),
],
);
})) {
case 'Found':
print('FOUND!');
break;
case 'Lost':
print('LOST!');
break;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.