Created
January 22, 2024 10:29
-
-
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.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