Reputation: 103
I'm trying to return a bool value from the dialogbox but I do not understand why the value does not return as need. I have tried returning as a future value and returning the values together with context after popping the dialogbox.
final bool delete = await _showDialog();
print(delete);
Future<bool> _showDialog() {
bool result;
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Delete Appointment'),
content: Text(
'Are you sure? \nThis action cannot undo.',
style: TextStyle(
color: Colors.red,
fontSize: 20
),
),
actions: <Widget>[
FlatButton(
color: Colors.blue,
child: Text(
'CANCEL',
style: TextStyle(
color: Colors.white,
fontSize: 20
),
),
onPressed: () {
setState(() => result = false);
//print(result);
Navigator.pop(context, result);
return Future.value(result);
},
),
SizedBox(
width: 50,
),
FlatButton(
color: Colors.red,
child: Text(
'CONFIRM',
style: TextStyle(
color: Colors.white,
fontSize: 20
),
),
onPressed: () {
setState(() => result = true);
//print(result);
Navigator.pop(context, result);
return Future.value(result);
},
)
],
);
}
);
}
Upvotes: 7
Views: 6854
Reputation: 1447
For Return True
Navigator.of(context).pop(true);
For Return False
Navigator.of(context).pop(false);
Upvotes: 8
Reputation: 3488
onPressed:() {
result = false;
//print(result);
setState(() {});
Navigator.pop(context, result);
}
or
onPressed:() {
setState(() {
result = false;
//print(result);
Navigator.pop(context, result);
});
}
Upvotes: 1
Reputation: 27177
check out this full woking code. you have to pass value which you want to return.
import 'package:flutter/material.dart';
class Delete extends StatefulWidget {
Delete({Key key}) : super(key: key);
@override
_DeleteState createState() => _DeleteState();
}
class _DeleteState extends State<Delete> with SingleTickerProviderStateMixin {
int index = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
onPressed: () {
_showDialog().then((value) {
print(value.toString());
});
},
child: Text("press ME!"),
),
),
);
}
_showDialog() {
return showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Delete Appointment'),
content: Text(
'Are you sure? \nThis action cannot undo.',
style: TextStyle(color: Colors.red, fontSize: 20),
),
actions: <Widget>[
FlatButton(
color: Colors.blue,
child: Text(
'CANCEL',
style: TextStyle(color: Colors.white, fontSize: 20),
),
onPressed: () {
Navigator.pop(context, false);
},
),
SizedBox(
width: 50,
),
FlatButton(
color: Colors.red,
child: Text(
'CONFIRM',
style: TextStyle(color: Colors.white, fontSize: 20),
),
onPressed: () {
Navigator.pop(context, true);
},
)
],
);
});
}
}
Upvotes: 1
Reputation: 9635
Here's a working example based on your code:
@override
Widget build(BuildContext context) {
return Container(
child: RaisedButton(onPressed: () async {
bool delete = await _showDialog(context);
print(delete);
})
);
}
Future<bool> _showDialog(context) {
return showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Delete Appointment'),
content: Text(
'Are you sure? \nThis action cannot undo.',
style: TextStyle(
color: Colors.red,
fontSize: 20
),
),
actions: <Widget>[
FlatButton(
color: Colors.blue,
child: Text(
'CANCEL',
style: TextStyle(
color: Colors.white,
fontSize: 20
),
),
onPressed: () {
Navigator.pop(context, false);
},
),
SizedBox(
width: 50,
),
FlatButton(
color: Colors.red,
child: Text(
'CONFIRM',
style: TextStyle(
color: Colors.white,
fontSize: 20
),
),
onPressed: () {
Navigator.pop(context, true);
},
)
],
);
}
);
}
Upvotes: 7
Reputation: 24736
You pass the value you want to return to Navigator.pop
and it will be returned by showDialog
:
final result = await showDialog(
...
// At some point, Navigator.pop is called
Navigator.pop(context, true);
);
print(result); // Prints: true
Upvotes: 2