我有以下代码,只需单击FlatButton即可调用
:_performOrderCheck(BuildContext context) async {
bool _checksCompleted = await _performBundleCheck(context);
print("Sepp");
print(_checksCompleted);
if (_checksCompleted) {
_addArticleToOrder(_innerQty, _article);
Navigator.pop(context);
}
}
Future<bool> _performBundleCheck(BuildContext context) async {
//check bundles
if (!_article.checkBundeledArticles()) {
showDialog(
context: context,
builder: (_) => AlertDialog(
title: Text('Menü unvollständig'),
content: Text(
'Sie haben nicht alle möglichen Artikel gewählt. Wollen sie dennoch fortfahren?'),
actions: <Widget>[
FlatButton(
onPressed: () {
Navigator.pop(_);
return false;
},
child: Text('Nein')),
FlatButton(
onPressed: () {
//_addArticleToOrder(_innerQty, _article);
Navigator.pop(_);
return true;
//Navigator.pop(context);
},
child: Text('Ja')),
],
elevation: 24,
),
barrierDismissible: false);
} else {
return true;
}
}
我想要的是可以等待用户决定,然后调用“ _addArticleToOrder”。有可能吗?
感谢您的帮助。
答案 0 :(得分:0)
您可以在showdialog的前面添加await关键字,并在show对话框的末尾返回值。
添加了等待状态。
await showDialog(
添加返回值
barrierDismissible: false);
return true; // added line
答案 1 :(得分:0)
当接受的答案有效时,结果始终返回 true 。
如果您想获得对话框的结果,可以通过单击'Nein'来 false ,并通过单击'Ja'来 true ,这是代码:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
_performOrderCheck(context);
},
// onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
_performOrderCheck(BuildContext context) async {
_incrementCounter();
print("_performOrderCheck called");
bool _checksCompleted = await _performBundleCheck(context);
print("_checksCompleted result: $_checksCompleted");
if (_checksCompleted) {
print("_checksCompleted");
}
}
Future<bool> _performBundleCheck(BuildContext context) async {
//check bundles
if (true) {
return await showDialog(
context: context,
builder: (_) => AlertDialog(
title: Text('Menü unvollständig'),
content: Text(
'Sie haben nicht alle möglichen Artikel gewählt. Wollen sie dennoch fortfahren?'),
actions: <Widget>[
FlatButton(
onPressed: () {
Navigator.pop(context, false);
},
child: Text('Nein')),
FlatButton(
onPressed: () {
Navigator.pop(context, true);
},
child: Text('Ja')),
],
elevation: 24,
),
barrierDismissible: false,
);
} else {
return true;
}
}
}
使用Navigator.pop(context, false);
和Navigator.pop(context, true);
将对话框的结果返回到showDialog
。
然后,使用return await
将其从_performBundleCheck
函数返回到_performOrderCheck
。