Skip to content

Instantly share code, notes, and snippets.

@stevenosse
Created January 22, 2024 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevenosse/e4492fb662608669bf8df5080cc9fc3e to your computer and use it in GitHub Desktop.
Save stevenosse/e4492fb662608669bf8df5080cc9fc3e to your computer and use it in GitHub Desktop.
PopConfirm
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
/// Flutter widget to execute asynchronous operations before a page is popped
/// Uses Flutter 3.16.x's PopScope attribute
class PopConfirm extends StatefulWidget {
const PopConfirm({
super.key,
required this.onPop,
required this.child,
});
final Future<bool> Function() onPop;
final Widget child;
@override
State<PopConfirm> createState() => _PopConfirmState();
}
class _PopConfirmState extends State<PopConfirm> {
@override
Widget build(BuildContext context) {
return PopScope(
canPop: false,
onPopInvoked: (bool didPop) async {
if (didPop) { return; }
final shouldPop = await widget.onPop();
if (shouldPop) {
unawaited(SystemNavigator.pop());
}
},
child: widget.child,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment