Navigator.pop(context)在Flutter中完成堆栈中的最后一个Activity时显示黑屏

时间:2019-07-15 10:33:09

标签: flutter dart navigator onbackpressed

我对此进行了很多搜索,但尚未找到正确的解决方案。我想知道Flutter的 finish()(在Java中为Android)等效于什么

 @override
 Widget build(BuildContext context) {
 return WillPopScope(
  onWillPop: ()  {
    if (counter == 1) {
    // **What should I write here so that my current activity is destroyed with showing the Toast message.**
    }
    Fluttertoast.showToast(
      msg: "Press again for exit!",
      toastLength: Toast.LENGTH_SHORT,
      gravity: ToastGravity.BOTTOM,
    );
    counter++;
    Timer(Duration(seconds: 2), () {
      counter = 0;
    });
    print(counter);
  },
 child: new Scaffold(
      ....................
           );
   );
}

1 个答案:

答案 0 :(得分:0)

我已经找到解决方案。绝对可以!

@override
Widget build(BuildContext context) {
return new WillPopScope(
    onWillPop: () {
      if (counter == 1) {
        return Future.value(true);
      }
      Fluttertoast.showToast(
        msg: "Press again for exit!",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.BOTTOM,
      );
      counter++;
      Timer(Duration(seconds: 2), () {
        counter = 0;
      });
      return Future.value(false);
    },
    child: new Scaffold(
      ..................

    ));
 }