Skip to content

Instantly share code, notes, and snippets.

@stevenosse
Last active May 28, 2024 11:16
Show Gist options
  • 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.handler,
required this.child,
});
final Future<bool> Function() handler;
final Widget child;
@override
State<PopConfirm> createState() => _PopConfirmState();
}
class _PopConfirmState extends State<PopConfirm> {
@override
Widget build(BuildContext context) {
return PopScope(
canPop: false,
onPopInvoked: (didPop) async {
if (!didPop && await widget.handler() && context.mounted) {
Navigator.pop(context);
}
},
child: widget.child,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment