i have the following code which is called by a click of a 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;
}
}
What i would like is that the could waits for the user decision and then it calls "_addArticleToOrder". Is that possible?
Thanks for any help.
You can add await keyword in-front of showdialog and return value at the end of show dialog.
added await.
await showDialog(
add return value
barrierDismissible: false);
return true; // added line
While the accepted answer is working the result is always returning true .
If you want to get the result of the dialog, which could be false by clicking 'Nein' and true by clicking 'Ja', here´s the code:
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;
}
}
}
Using Navigator.pop(context, false);
and Navigator.pop(context, true);
returns the result of the dialog to showDialog
. Using return await
returns it then from the _performBundleCheck
function to _performOrderCheck
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.