当前位置:网站首页>(shuttle) navigation return interception: willpopscope

(shuttle) navigation return interception: willpopscope

2022-07-06 22:56:00 Xiaobai is more than that

1. Calculate the time difference between two dates

DateTime.now().difference(_lastPressedAt!)
// Use  difference  Method , You can get the difference of all time units 

among ,DateTime Of difference Method Back to a Duration object .Duration It's used to show time span ( Difference value ) Class .

Reference article :
Flutter Calculate the time difference between two dates

Xiaobai is more than that :(Dart) Usage supplement

2. Navigation returns to intercept : WillPopScope

  DateTime? _lastPressedAt; // Last click time 

  @override
  Widget build(BuildContext context) {
    
    return WillPopScope(
      child: Scaffold(
        body: Container(
          alignment: Alignment.center,
          child: Text("2 Press the return key twice in a row within seconds to exit "),
        ),
      ),
      onWillPop: () async {
    
        if (_lastPressedAt == null || DateTime.now().difference(_lastPressedAt!) > Duration(seconds: 2)) {
    
          // The interval between two clicks is more than 2 Second is the second 
          _lastPressedAt = DateTime.now();
          Fluttertoast.showToast(
            msg: ' Press again to exit !',
            toastLength: Toast.LENGTH_SHORT,
            gravity: ToastGravity.CENTER,
            timeInSecForIosWeb: 2,
            backgroundColor: Colors.black87,
            textColor: Colors.white,
          );
          return false;// Current route does not stack ( Will not return )
        }
        return true;// The current route exits the stack 
      },
    );
  }

 Navigation returns the interception effect diagram
Reference article :
Navigation returns to intercept (WillPopScope)

原网站

版权声明
本文为[Xiaobai is more than that]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131042125583.html