Last active
May 28, 2024 11:16
-
-
Save stevenosse/e4492fb662608669bf8df5080cc9fc3e to your computer and use it in GitHub Desktop.
PopConfirm
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
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