I create a WillPopScope
Function for exit pop service. When user click on back button it's show a dialog for exit permission.
It worked on flutter version 1 perfectly. Now now for null issue, it's create a problem.
Here is my code :
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class ExitPopUp extends StatelessWidget {
final page;
ExitPopUp(this.page);
@override
Widget build(BuildContext context) {
Future<bool?> showExitPopUp() {
return showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20.0))),
backgroundColor: Color.fromRGBO(0, 68, 69, .5),
title: Text("Confirm", style: TextStyle(color: Colors.white)),
content: Text("Do you want to Exit ?",
style: TextStyle(color: Colors.white)),
actions: <Widget>[
ElevatedButton(
child: Text(
"No",
style: TextStyle(color: Colors.yellow[100]),
),
onPressed: () {
Navigator.pop(context, false);
}),
ElevatedButton(
child: Text("Yes",
style: TextStyle(color: Colors.yellow[100])),
onPressed: () {
SystemNavigator.pop();
})
],
);
});
}
return WillPopScope(child: page, onWillPop: showExitPopUp);
}
}
but here is a issue on onWillPop.
How can I fix this issue ?
CodePudding user response:
You can chnage your WillPopScope
to this:
return WillPopScope(child: page, onWillPop: () => showExitPopUp().then(b -> b ?? false));
Or you can change your showExitPopUp
from Future<bool?>
to Future<bool>
.
CodePudding user response:
I just convert it to async function. And it's worked here is updated code
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class ExitPopUp extends StatelessWidget {
final page;
ExitPopUp(this.page);
@override
Widget build(BuildContext context) {
Future<bool> showExitPopUp() async {
return await showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20.0))),
backgroundColor: Color.fromRGBO(0, 68, 69, .5),
title: Text("Confirm", style: TextStyle(color: Colors.white)),
content: Text("Do you want to Exit ?",
style: TextStyle(color: Colors.white)),
actions: <Widget>[
ElevatedButton(
child: Text(
"No",
style: TextStyle(color: Colors.yellow[100]),
),
onPressed: () {
Navigator.pop(context, false);
}),
ElevatedButton(
child: Text("Yes",
style: TextStyle(color: Colors.yellow[100])),
onPressed: () {
SystemNavigator.pop();
})
],
);
});
}
return WillPopScope(child: page, onWillPop: showExitPopUp);
}
}