Reland root predictive back (#132249)
Root predictive back (https://github.com/flutter/flutter/pull/120385) was reverted in https://github.com/flutter/flutter/pull/132167. This PR is an attempt to reland it. The reversion happened due to failed Google tests (b/295073110).
This commit is contained in:
committed by
GitHub
parent
ced3e76626
commit
f68d03f1cd
@@ -48,9 +48,9 @@ class CupertinoNavigationDemo extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return WillPopScope(
|
||||
return PopScope(
|
||||
// Prevent swipe popping of this page. Use explicit exit buttons only.
|
||||
onWillPop: () => Future<bool>.value(true),
|
||||
canPop: false,
|
||||
child: DefaultTextStyle(
|
||||
style: CupertinoTheme.of(context).textTheme.textStyle,
|
||||
child: CupertinoTabScaffold(
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
// This demo is based on
|
||||
@@ -109,16 +110,15 @@ class FullScreenDialogDemoState extends State<FullScreenDialogDemo> {
|
||||
bool _hasName = false;
|
||||
late String _eventName;
|
||||
|
||||
Future<bool> _onWillPop() async {
|
||||
_saveNeeded = _hasLocation || _hasName || _saveNeeded;
|
||||
if (!_saveNeeded) {
|
||||
return true;
|
||||
Future<void> _handlePopInvoked(bool didPop) async {
|
||||
if (didPop) {
|
||||
return;
|
||||
}
|
||||
|
||||
final ThemeData theme = Theme.of(context);
|
||||
final TextStyle dialogTextStyle = theme.textTheme.titleMedium!.copyWith(color: theme.textTheme.bodySmall!.color);
|
||||
|
||||
return showDialog<bool>(
|
||||
final bool? shouldDiscard = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return AlertDialog(
|
||||
@@ -130,19 +130,31 @@ class FullScreenDialogDemoState extends State<FullScreenDialogDemo> {
|
||||
TextButton(
|
||||
child: const Text('CANCEL'),
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop(false); // Pops the confirmation dialog but not the page.
|
||||
// Pop the confirmation dialog and indicate that the page should
|
||||
// not be popped.
|
||||
Navigator.of(context).pop(false);
|
||||
},
|
||||
),
|
||||
TextButton(
|
||||
child: const Text('DISCARD'),
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop(true); // Returning true to _onWillPop will pop again.
|
||||
// Pop the confirmation dialog and indicate that the page should
|
||||
// be popped, too.
|
||||
Navigator.of(context).pop(true);
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
) as Future<bool>;
|
||||
);
|
||||
|
||||
if (shouldDiscard ?? false) {
|
||||
// Since this is the root route, quit the app where possible by invoking
|
||||
// the SystemNavigator. If this wasn't the root route, then
|
||||
// Navigator.maybePop could be used instead.
|
||||
// See https://github.com/flutter/flutter/issues/11490
|
||||
SystemNavigator.pop();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -162,7 +174,8 @@ class FullScreenDialogDemoState extends State<FullScreenDialogDemo> {
|
||||
],
|
||||
),
|
||||
body: Form(
|
||||
onWillPop: _onWillPop,
|
||||
canPop: !_saveNeeded && !_hasLocation && !_hasName,
|
||||
onPopInvoked: _handlePopInvoked,
|
||||
child: Scrollbar(
|
||||
child: ListView(
|
||||
primary: true,
|
||||
|
||||
@@ -143,10 +143,9 @@ class TextFormFieldDemoState extends State<TextFormFieldDemo> {
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<bool> _warnUserAboutInvalidData() async {
|
||||
final FormState? form = _formKey.currentState;
|
||||
if (form == null || !_formWasEdited || form.validate()) {
|
||||
return true;
|
||||
Future<void> _handlePopInvoked(bool didPop) async {
|
||||
if (didPop) {
|
||||
return;
|
||||
}
|
||||
|
||||
final bool? result = await showDialog<bool>(
|
||||
@@ -168,7 +167,14 @@ class TextFormFieldDemoState extends State<TextFormFieldDemo> {
|
||||
);
|
||||
},
|
||||
);
|
||||
return result!;
|
||||
|
||||
if (result ?? false) {
|
||||
// Since this is the root route, quit the app where possible by invoking
|
||||
// the SystemNavigator. If this wasn't the root route, then
|
||||
// Navigator.maybePop could be used instead.
|
||||
// See https://github.com/flutter/flutter/issues/11490
|
||||
SystemNavigator.pop();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -185,7 +191,8 @@ class TextFormFieldDemoState extends State<TextFormFieldDemo> {
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
autovalidateMode: _autovalidateMode,
|
||||
onWillPop: _warnUserAboutInvalidData,
|
||||
canPop: _formKey.currentState == null || !_formWasEdited || _formKey.currentState!.validate(),
|
||||
onPopInvoked: _handlePopInvoked,
|
||||
child: Scrollbar(
|
||||
child: SingleChildScrollView(
|
||||
primary: true,
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:scoped_model/scoped_model.dart';
|
||||
|
||||
import 'colors.dart';
|
||||
@@ -361,14 +360,12 @@ class ExpandingBottomSheetState extends State<ExpandingBottomSheet> with TickerP
|
||||
|
||||
// Closes the cart if the cart is open, otherwise exits the app (this should
|
||||
// only be relevant for Android).
|
||||
Future<bool> _onWillPop() async {
|
||||
if (!_isOpen) {
|
||||
await SystemNavigator.pop();
|
||||
return true;
|
||||
void _handlePopInvoked(bool didPop) {
|
||||
if (didPop) {
|
||||
return;
|
||||
}
|
||||
|
||||
close();
|
||||
return true;
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -378,8 +375,9 @@ class ExpandingBottomSheetState extends State<ExpandingBottomSheet> with TickerP
|
||||
duration: const Duration(milliseconds: 225),
|
||||
curve: Curves.easeInOut,
|
||||
alignment: FractionalOffset.topLeft,
|
||||
child: WillPopScope(
|
||||
onWillPop: _onWillPop,
|
||||
child: PopScope(
|
||||
canPop: !_isOpen,
|
||||
onPopInvoked: _handlePopInvoked,
|
||||
child: AnimatedBuilder(
|
||||
animation: widget.hideController,
|
||||
builder: _buildSlideAnimation,
|
||||
|
||||
@@ -325,14 +325,14 @@ class _GalleryHomeState extends State<GalleryHome> with SingleTickerProviderStat
|
||||
backgroundColor: isDark ? _kFlutterBlue : theme.primaryColor,
|
||||
body: SafeArea(
|
||||
bottom: false,
|
||||
child: WillPopScope(
|
||||
onWillPop: () {
|
||||
// Pop the category page if Android back button is pressed.
|
||||
if (_category != null) {
|
||||
setState(() => _category = null);
|
||||
return Future<bool>.value(false);
|
||||
child: PopScope(
|
||||
canPop: _category == null,
|
||||
onPopInvoked: (bool didPop) {
|
||||
if (didPop) {
|
||||
return;
|
||||
}
|
||||
return Future<bool>.value(true);
|
||||
// Pop the category page if Android back button is pressed.
|
||||
setState(() => _category = null);
|
||||
},
|
||||
child: Backdrop(
|
||||
backTitle: const Text('Options'),
|
||||
|
||||
Reference in New Issue
Block a user