Skip to content

Commit acc5097

Browse files
committed
Migrate to PopScope from WillPopScope
1 parent 3b2cca1 commit acc5097

13 files changed

+88
-51
lines changed

example/lib/pages/alert_page.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ class AlertPage extends StatelessWidget {
3434
},
3535
),
3636
ListTile(
37-
title: const Text('OK Dialog (onWillPop: false)'),
37+
title: const Text('OK Dialog (canPop: false)'),
3838
onTap: () async {
3939
final result = await showOkAlertDialog(
4040
context: context,
4141
title: 'Title',
4242
message: 'This is message.',
43-
onWillPop: () => Future.value(false),
43+
canPop: false,
4444
);
4545
assert(result == OkCancelResult.ok);
4646
logger.info(result);

lib/src/alert_dialog/show_alert_dialog.dart

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ Future<T?> showAlertDialog<T>({
2828
bool useRootNavigator = true,
2929
VerticalDirection actionsOverflowDirection = VerticalDirection.up,
3030
bool fullyCapitalizedForMaterial = true,
31-
WillPopCallback? onWillPop,
31+
bool canPop = true,
32+
PopInvokedCallback? onPopInvoked,
3233
AdaptiveDialogBuilder? builder,
3334
Widget? macOSApplicationIcon,
3435
RouteSettings? routeSettings,
@@ -51,7 +52,8 @@ Future<T?> showAlertDialog<T>({
5152
actions: actions.convertToSheetActions(),
5253
style: style,
5354
useRootNavigator: useRootNavigator,
54-
onWillPop: onWillPop,
55+
canPop: canPop,
56+
onPopInvoked: onPopInvoked,
5557
builder: builder,
5658
routeSettings: routeSettings,
5759
);
@@ -70,8 +72,9 @@ Future<T?> showAlertDialog<T>({
7072
useRootNavigator: useRootNavigator,
7173
routeSettings: routeSettings,
7274
builder: (context) {
73-
final dialog = WillPopScope(
74-
onWillPop: onWillPop,
75+
final dialog = PopScope(
76+
canPop: canPop,
77+
onPopInvoked: onPopInvoked,
7578
child: CupertinoAlertDialog(
7679
title: titleText,
7780
content: messageText,
@@ -106,8 +109,9 @@ Future<T?> showAlertDialog<T>({
106109
routeSettings: routeSettings,
107110
builder: (context) {
108111
final Widget dialog = MacThemeWrapper(
109-
child: WillPopScope(
110-
onWillPop: onWillPop,
112+
child: PopScope(
113+
canPop: canPop,
114+
onPopInvoked: onPopInvoked,
111115
child: MacosAlertDialog(
112116
title: titleText ?? const SizedBox.shrink(),
113117
message: messageText ?? const SizedBox.shrink(),
@@ -134,8 +138,9 @@ Future<T?> showAlertDialog<T>({
134138
barrierDismissible: barrierDismissible,
135139
),
136140
builder: (context) {
137-
final dialog = WillPopScope(
138-
onWillPop: onWillPop,
141+
final dialog = PopScope(
142+
canPop: canPop,
143+
onPopInvoked: onPopInvoked,
139144
child: AlertDialog(
140145
title: titleText,
141146
content: messageText,

lib/src/alert_dialog/show_confirmation_dialog.dart

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ Future<T?> showConfirmationDialog<T>({
3232
bool useRootNavigator = true,
3333
bool shrinkWrap = true,
3434
bool fullyCapitalizedForMaterial = true,
35-
WillPopCallback? onWillPop,
35+
bool canPop = true,
36+
PopInvokedCallback? onPopInvoked,
3637
AdaptiveDialogBuilder? builder,
3738
RouteSettings? routeSettings,
3839
bool toggleable = true,
@@ -64,7 +65,8 @@ Future<T?> showConfirmationDialog<T>({
6465
contentMaxHeight: contentMaxHeight,
6566
shrinkWrap: shrinkWrap,
6667
fullyCapitalized: fullyCapitalizedForMaterial,
67-
onWillPop: onWillPop,
68+
canPop: canPop,
69+
onPopInvoked: onPopInvoked,
6870
toggleable: toggleable,
6971
);
7072
return builder == null ? dialog : builder(context, dialog);
@@ -78,7 +80,8 @@ Future<T?> showConfirmationDialog<T>({
7880
actions: actions.convertToSheetActions(),
7981
style: style,
8082
useRootNavigator: useRootNavigator,
81-
onWillPop: onWillPop,
83+
canPop: canPop,
84+
onPopInvoked: onPopInvoked,
8285
builder: builder,
8386
routeSettings: routeSettings,
8487
);
@@ -97,7 +100,8 @@ class _ConfirmationMaterialDialog<T> extends StatefulWidget {
97100
@required this.contentMaxHeight,
98101
required this.shrinkWrap,
99102
required this.fullyCapitalized,
100-
required this.onWillPop,
103+
required this.canPop,
104+
required this.onPopInvoked,
101105
required this.toggleable,
102106
});
103107

@@ -111,7 +115,8 @@ class _ConfirmationMaterialDialog<T> extends StatefulWidget {
111115
final double? contentMaxHeight;
112116
final bool shrinkWrap;
113117
final bool fullyCapitalized;
114-
final WillPopCallback? onWillPop;
118+
final bool canPop;
119+
final PopInvokedCallback? onPopInvoked;
115120
final bool toggleable;
116121

117122
@override
@@ -137,8 +142,8 @@ class _ConfirmationMaterialDialogState<T>
137142
final cancelLabel = widget.cancelLabel;
138143
final okLabel = widget.okLabel;
139144
final message = widget.message;
140-
return WillPopScope(
141-
onWillPop: widget.onWillPop,
145+
return PopScope(
146+
canPop: widget.canPop,
142147
child: Dialog(
143148
child: Column(
144149
mainAxisSize: MainAxisSize.min,

lib/src/alert_dialog/show_ok_alert_dialog.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ Future<OkCancelResult> showOkAlertDialog({
2121
bool useRootNavigator = true,
2222
VerticalDirection actionsOverflowDirection = VerticalDirection.up,
2323
bool fullyCapitalizedForMaterial = true,
24-
WillPopCallback? onWillPop,
24+
bool canPop = true,
25+
PopInvokedCallback? onPopInvoked,
2526
AdaptiveDialogBuilder? builder,
2627
RouteSettings? routeSettings,
2728
}) async {
@@ -39,7 +40,8 @@ Future<OkCancelResult> showOkAlertDialog({
3940
useRootNavigator: useRootNavigator,
4041
actionsOverflowDirection: actionsOverflowDirection,
4142
fullyCapitalizedForMaterial: fullyCapitalizedForMaterial,
42-
onWillPop: onWillPop,
43+
canPop: canPop,
44+
onPopInvoked: onPopInvoked,
4345
builder: builder,
4446
actions: [
4547
AlertDialogAction(

lib/src/alert_dialog/show_ok_cancel_alert_dialog.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ Future<OkCancelResult> showOkCancelAlertDialog({
3030
bool useRootNavigator = true,
3131
VerticalDirection actionsOverflowDirection = VerticalDirection.up,
3232
bool fullyCapitalizedForMaterial = true,
33-
WillPopCallback? onWillPop,
33+
bool canPop = true,
34+
PopInvokedCallback? onPopInvoked,
3435
AdaptiveDialogBuilder? builder,
3536
RouteSettings? routeSettings,
3637
}) async {
@@ -53,7 +54,8 @@ Future<OkCancelResult> showOkCancelAlertDialog({
5354
useRootNavigator: useRootNavigator,
5455
actionsOverflowDirection: actionsOverflowDirection,
5556
fullyCapitalizedForMaterial: fullyCapitalizedForMaterial,
56-
onWillPop: onWillPop,
57+
canPop: canPop,
58+
onPopInvoked: onPopInvoked,
5759
builder: builder,
5860
actions: [
5961
AlertDialogAction(

lib/src/modal_action_sheet/cupertino_modal_action_sheet.dart

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,32 @@ class CupertinoModalActionSheet<T> extends StatelessWidget {
1515
this.title,
1616
this.message,
1717
this.cancelLabel,
18-
this.onWillPop,
18+
required this.canPop,
19+
required this.onPopInvoked,
1920
});
2021

2122
final ActionCallback<T> onPressed;
2223
final List<SheetAction<T>> actions;
2324
final String? title;
2425
final String? message;
2526
final String? cancelLabel;
26-
final WillPopCallback? onWillPop;
27+
final bool canPop;
28+
final PopInvokedCallback? onPopInvoked;
2729

2830
@override
2931
Widget build(BuildContext context) {
3032
final mediaQuery = MediaQuery.of(context);
3133
final title = this.title;
3234
final message = this.message;
33-
return WillPopScope(
34-
onWillPop: onWillPop,
35+
return PopScope(
36+
canPop: canPop,
37+
onPopInvoked: onPopInvoked,
3538
child: MediaQuery(
3639
data: mediaQuery.copyWith(
3740
// `CupertinoAlertDialog` overrides textScaleFactor
3841
// to keep larger than 1, but `CupertinoActionSheet` doesn't.
3942
// https://twitter.com/_mono/status/1266997626693509126
40-
textScaleFactor: max(1, mediaQuery.textScaleFactor),
43+
textScaler: TextScaler.linear(max(1, mediaQuery.textScaleFactor)),
4144
),
4245
child: CupertinoActionSheet(
4346
title: title == null ? null : Text(title),

lib/src/modal_action_sheet/material_modal_action_sheet.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ class MaterialModalActionSheet<T> extends StatelessWidget {
1111
this.title,
1212
this.message,
1313
this.materialConfiguration,
14-
this.onWillPop,
14+
required this.canPop,
15+
required this.onPopInvoked,
1516
});
1617

1718
final ActionCallback<T> onPressed;
1819
final List<SheetAction<T>> actions;
1920
final String? title;
2021
final String? message;
2122
final MaterialModalActionSheetConfiguration? materialConfiguration;
22-
final WillPopCallback? onWillPop;
23+
final bool canPop;
24+
final PopInvokedCallback? onPopInvoked;
2325

2426
@override
2527
Widget build(BuildContext context) {
@@ -86,8 +88,9 @@ class MaterialModalActionSheet<T> extends StatelessWidget {
8688
children: children,
8789
),
8890
);
89-
return WillPopScope(
90-
onWillPop: onWillPop,
91+
return PopScope(
92+
canPop: canPop,
93+
onPopInvoked: onPopInvoked,
9194
child: body,
9295
);
9396
}

lib/src/modal_action_sheet/modal_action_sheet.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ Future<T?> showModalActionSheet<T>({
2323
bool isDismissible = true,
2424
bool useRootNavigator = true,
2525
MaterialModalActionSheetConfiguration? materialConfiguration,
26-
WillPopCallback? onWillPop,
26+
bool canPop = true,
27+
PopInvokedCallback? onPopInvoked,
2728
AdaptiveDialogBuilder? builder,
2829
RouteSettings? routeSettings,
2930
}) {
@@ -48,7 +49,8 @@ Future<T?> showModalActionSheet<T>({
4849
message: message,
4950
actions: actions,
5051
materialConfiguration: materialConfiguration,
51-
onWillPop: onWillPop,
52+
canPop: canPop,
53+
onPopInvoked: onPopInvoked,
5254
);
5355
return builder == null ? sheet : builder(context, sheet);
5456
},
@@ -64,7 +66,8 @@ Future<T?> showModalActionSheet<T>({
6466
message: message,
6567
actions: actions,
6668
cancelLabel: cancelLabel,
67-
onWillPop: onWillPop,
69+
canPop: canPop,
70+
onPopInvoked: onPopInvoked,
6871
);
6972
return builder == null ? sheet : builder(context, sheet);
7073
},

lib/src/text_input_dialog/ios_text_input_dialog.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class IOSTextInputDialog extends StatefulWidget {
1515
this.isDestructiveAction = false,
1616
this.style = AdaptiveStyle.adaptive,
1717
this.useRootNavigator = true,
18-
this.onWillPop,
18+
required this.canPop,
19+
required this.onPopInvoked,
1920
this.autoSubmit = false,
2021
});
2122
@override
@@ -29,7 +30,8 @@ class IOSTextInputDialog extends StatefulWidget {
2930
final bool isDestructiveAction;
3031
final AdaptiveStyle style;
3132
final bool useRootNavigator;
32-
final WillPopCallback? onWillPop;
33+
final bool canPop;
34+
final PopInvokedCallback? onPopInvoked;
3335
final bool autoSubmit;
3436
}
3537

@@ -110,8 +112,8 @@ class _IOSTextInputDialogState extends State<IOSTextInputDialog> {
110112
}
111113

112114
final validationMessage = _validationMessage;
113-
return WillPopScope(
114-
onWillPop: widget.onWillPop,
115+
return PopScope(
116+
canPop: widget.canPop,
115117
child: CupertinoAlertDialog(
116118
title: title == null ? null : Text(title),
117119
content: Column(

lib/src/text_input_dialog/macos_text_input_dialog.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class MacOSTextInputDialog extends StatefulWidget {
1818
this.isDestructiveAction = false,
1919
this.style = AdaptiveStyle.adaptive,
2020
this.useRootNavigator = true,
21-
this.onWillPop,
21+
required this.canPop,
22+
required this.onPopInvoked,
2223
this.autoSubmit = false,
2324
});
2425
@override
@@ -32,7 +33,8 @@ class MacOSTextInputDialog extends StatefulWidget {
3233
final bool isDestructiveAction;
3334
final AdaptiveStyle style;
3435
final bool useRootNavigator;
35-
final WillPopCallback? onWillPop;
36+
final bool canPop;
37+
final PopInvokedCallback? onPopInvoked;
3638
final bool autoSubmit;
3739
}
3840

0 commit comments

Comments
 (0)