принудительно выйти из приложения, используя WillPopScope во Flutter - PullRequest
1 голос
/ 04 мая 2020

Моя проблема в том, что у меня есть 3 экрана, которые я хочу выйти из приложения на 3-м экране, используя WillPopScope. Когда я использую WillPopScope, он возвращается на второй экран ...

 Future<bool> onWillPop() async {
  DateTime now = DateTime.now();
  if (currentBackPressTime == null ||
      now.difference(currentBackPressTime) > Duration(seconds: 2)) {
    currentBackPressTime = now;
    Fluttertoast.showToast(msg: 'Press back again to exit');
    return Future.value(false);
  }

  return Future.value(true);
}



WillPopScope(
  onWillPop: onWillPop,
  child: Scaffold();

Ответы [ 2 ]

1 голос
/ 04 мая 2020

Неожиданный выход из приложения противоречит политикам Apple, поэтому SystemNavigator.pop () работает только с android. Сделайте следующее.

Future<bool> onWillPop() async {
  DateTime now = DateTime.now();
  if (currentBackPressTime == null ||
      now.difference(currentBackPressTime) > Duration(seconds: 2)) {
    currentBackPressTime = now;
    Fluttertoast.showToast(msg: 'Press back again to exit');
    return Future.value(false);
  }
  SystemNavigator.pop(); // add this.

  return Future.value(true);
}



WillPopScope(
  onWillPop: onWillPop,
  child: Scaffold();
0 голосов
/ 04 мая 2020

Вы должны указать навигатору всплывающее окно, как я сделал здесь для свойства onWillPop объекта WillPopScope.

 Future<bool> onWillPop() async {
  DateTime now = DateTime.now();
  if (currentBackPressTime == null ||
      now.difference(currentBackPressTime) > Duration(seconds: 2)) {
    currentBackPressTime = now;
    Fluttertoast.showToast(msg: 'Press back again to exit');
    return Future.value(false);
  }

  return Future.value(true);
}



WillPopScope(
  onWillPop: if(onWillPop == true) {Navigator.pop(context)},
  child: Scaffold();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...