Created
April 28, 2023 14:25
-
-
Save rei-codes/d192861b2225b0756fc0205b870823dc to your computer and use it in GitHub Desktop.
Tap again to exit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DoubleBack extends StatefulWidget { | |
const DoubleBack({super.key, required this.child}); | |
final Widget child; | |
@override | |
State<DoubleBack> createState() => _DoubleBackState(); | |
} | |
class _DoubleBackState extends State<DoubleBack> { | |
DateTime? pressedTime; | |
Future<bool> tapAgainToExitToast([String? text]) async { | |
final now = DateTime.now(); | |
// If pressed to back button before and haven't passed 2 seconds yet, go back. | |
// Otherwise do nothing | |
if (pressedTime == null || | |
now.difference(pressedTime!) > const Duration(seconds: 2)) { | |
// updates the pressed time | |
pressedTime = now; | |
// I didn't want to include any package into this project | |
// but you can add your toast message here :) | |
log('Tap again to exit'); | |
// do not pop the route | |
return false; | |
} | |
// pop the route | |
return true; | |
} | |
@override | |
Widget build(BuildContext context) { | |
return WillPopScope( | |
// on back pressed | |
onWillPop: tapAgainToExitToast, | |
child: widget.child, | |
); | |
} | |
} | |
// It is useful only for android devices | |
class TapAgainToExit extends StatelessWidget { | |
const TapAgainToExit({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
// Magic Happens here | |
// A WillPopScope wrapper widget to make the feature reusable | |
return DoubleBack( | |
child: Scaffold( | |
appBar: AppBar( | |
title: const Text('Tap again to exit'), | |
), | |
body: const Center( | |
child: Text('Press on the back button 2 times to exit the app'), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment