checkDoubleBackExit static method

Future<bool> checkDoubleBackExit(
  1. BuildContext context,
  2. Duration timeout,
  3. String? message
)

Double back exit logic

Implementation

static Future<bool> checkDoubleBackExit(
  BuildContext context,
  Duration timeout,
  String? message,
) async {
  final now = DateTime.now();
  if (_lastBackPress == null || now.difference(_lastBackPress!) > timeout) {
    _lastBackPress = now;

    if (message != null) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text(message), duration: timeout),
      );
    }
    return false; // prevent exit
  }
  return true; // allow exit
}