Skip to content

Commit c42ad96

Browse files
justinmcparlough
andauthored
Predictive back migration guide confirmation dialog example (#9244)
A user requested this example be added to the migration guide [on Discord](https://discord.com/channels/608014603317936148/969301283825659914/1140422179511603290). I think it's a reasonable request. Follow up on: #8952 Framework PR: flutter/flutter#132249 --------- Co-authored-by: Parker Lougheed <[email protected]>
1 parent 34bebf8 commit c42ad96

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

src/release/breaking-changes/android-predictive-back.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,60 @@ if (_route.popDisposition == RoutePopDisposition.doNotPop) {
270270
}
271271
```
272272

273+
### Migrating a back confirmation dialog
274+
275+
`WillPopScope` was sometimes used to show a confirmation dialog when
276+
a back gesture was received.
277+
This can still be done with `PopScope` in a similar pattern.
278+
279+
Code before migration:
280+
281+
```dart
282+
WillPopScope(
283+
onWillPop: () async {
284+
final bool? shouldPop = await _showBackDialog();
285+
return shouldPop ?? false;
286+
},
287+
child: child,
288+
)
289+
```
290+
291+
Code after migration:
292+
293+
```dart
294+
return PopScope(
295+
canPop: false,
296+
onPopInvoked: (bool didPop) async {
297+
if (didPop) {
298+
return;
299+
}
300+
final NavigatorState navigator = Navigator.of(context);
301+
final bool? shouldPop = await _showBackDialog();
302+
if (shouldPop ?? false) {
303+
navigator.pop();
304+
}
305+
},
306+
child: child,
307+
)
308+
```
309+
310+
### Supporting predictive back
311+
312+
1. Run Android 33 or above.
313+
1. Enable the feature flag for predictive back on
314+
the device under "Developer options".
315+
This will be unnecessary on future versions of Android.
316+
1. Set `android:enableOnBackInvokedCallback="true"` in
317+
`android/app/src/main/AndroidManifest.xml`. If needed, refer to
318+
[Android's full guide](https://developer.android.com/guide/navigation/custom-back/predictive-back-gesture)
319+
for migrating Android apps to support predictive back.
320+
1. Make sure you're using version `3.14.0-7.0.pre` of Flutter or greater.
321+
1. Run the app and perform a back gesture (swipe from the left side of
322+
the screen).
323+
273324
## Timeline
274325

275-
Landed in version: 3.14.0-0.0.pre<br>
326+
Landed in version: 3.14.0-7.0.pre<br>
276327
In stable release: not yet
277328

278329
## References

0 commit comments

Comments
 (0)