Unverified Commit 2728ba0f authored by Jesús S Guerrero's avatar Jesús S Guerrero Committed by GitHub

Revert of #120385 (#132167)

Breaking google testing 
revert of: https://github.com/flutter/flutter/pull/120385
b/295065534
parent b77b149d
......@@ -48,9 +48,9 @@ class CupertinoNavigationDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return PopScope(
return WillPopScope(
// Prevent swipe popping of this page. Use explicit exit buttons only.
canPop: false,
onWillPop: () => Future<bool>.value(true),
child: DefaultTextStyle(
style: CupertinoTheme.of(context).textTheme.textStyle,
child: CupertinoTabScaffold(
......
......@@ -3,7 +3,6 @@
// 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
......@@ -110,15 +109,16 @@ class FullScreenDialogDemoState extends State<FullScreenDialogDemo> {
bool _hasName = false;
late String _eventName;
Future<void> _handlePopInvoked(bool didPop) async {
if (didPop) {
return;
Future<bool> _onWillPop() async {
_saveNeeded = _hasLocation || _hasName || _saveNeeded;
if (!_saveNeeded) {
return true;
}
final ThemeData theme = Theme.of(context);
final TextStyle dialogTextStyle = theme.textTheme.titleMedium!.copyWith(color: theme.textTheme.bodySmall!.color);
final bool? shouldDiscard = await showDialog<bool>(
return showDialog<bool>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
......@@ -130,31 +130,19 @@ class FullScreenDialogDemoState extends State<FullScreenDialogDemo> {
TextButton(
child: const Text('CANCEL'),
onPressed: () {
// Pop the confirmation dialog and indicate that the page should
// not be popped.
Navigator.of(context).pop(false);
Navigator.of(context).pop(false); // Pops the confirmation dialog but not the page.
},
),
TextButton(
child: const Text('DISCARD'),
onPressed: () {
// Pop the confirmation dialog and indicate that the page should
// be popped, too.
Navigator.of(context).pop(true);
Navigator.of(context).pop(true); // Returning true to _onWillPop will pop again.
},
),
],
);
},
);
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();
}
) as Future<bool>;
}
@override
......@@ -174,8 +162,7 @@ class FullScreenDialogDemoState extends State<FullScreenDialogDemo> {
],
),
body: Form(
canPop: !_saveNeeded && !_hasLocation && !_hasName,
onPopInvoked: _handlePopInvoked,
onWillPop: _onWillPop,
child: Scrollbar(
child: ListView(
primary: true,
......
......@@ -143,9 +143,10 @@ class TextFormFieldDemoState extends State<TextFormFieldDemo> {
return null;
}
Future<void> _handlePopInvoked(bool didPop) async {
if (didPop) {
return;
Future<bool> _warnUserAboutInvalidData() async {
final FormState? form = _formKey.currentState;
if (form == null || !_formWasEdited || form.validate()) {
return true;
}
final bool? result = await showDialog<bool>(
......@@ -167,14 +168,7 @@ class TextFormFieldDemoState extends State<TextFormFieldDemo> {
);
},
);
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();
}
return result!;
}
@override
......@@ -191,8 +185,7 @@ class TextFormFieldDemoState extends State<TextFormFieldDemo> {
child: Form(
key: _formKey,
autovalidateMode: _autovalidateMode,
canPop: _formKey.currentState == null || !_formWasEdited || _formKey.currentState!.validate(),
onPopInvoked: _handlePopInvoked,
onWillPop: _warnUserAboutInvalidData,
child: Scrollbar(
child: SingleChildScrollView(
primary: true,
......
......@@ -3,6 +3,7 @@
// 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';
......@@ -360,12 +361,14 @@ 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).
void _handlePopInvoked(bool didPop) {
if (didPop) {
return;
Future<bool> _onWillPop() async {
if (!_isOpen) {
await SystemNavigator.pop();
return true;
}
close();
return true;
}
@override
......@@ -375,9 +378,8 @@ class ExpandingBottomSheetState extends State<ExpandingBottomSheet> with TickerP
duration: const Duration(milliseconds: 225),
curve: Curves.easeInOut,
alignment: FractionalOffset.topLeft,
child: PopScope(
canPop: !_isOpen,
onPopInvoked: _handlePopInvoked,
child: WillPopScope(
onWillPop: _onWillPop,
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: PopScope(
canPop: _category == null,
onPopInvoked: (bool didPop) {
if (didPop) {
return;
}
child: WillPopScope(
onWillPop: () {
// Pop the category page if Android back button is pressed.
setState(() => _category = null);
if (_category != null) {
setState(() => _category = null);
return Future<bool>.value(false);
}
return Future<bool>.value(true);
},
child: Backdrop(
backTitle: const Text('Options'),
......
......@@ -71,10 +71,14 @@ class _HomeState extends State<Home> with TickerProviderStateMixin<Home> {
@override
Widget build(BuildContext context) {
return NavigatorPopHandler(
onPop: () {
return WillPopScope(
onWillPop: () async {
final NavigatorState navigator = navigatorKeys[selectedIndex].currentState!;
if (!navigator.canPop()) {
return true;
}
navigator.pop();
return false;
},
child: Scaffold(
body: SafeArea(
......
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
/// This sample demonstrates showing a confirmation dialog when the user
/// attempts to navigate away from a page with unsaved [Form] data.
void main() => runApp(const FormApp());
class FormApp extends StatelessWidget {
const FormApp({
super.key,
});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Confirmation Dialog Example'),
),
body: Center(
child: _SaveableForm(),
),
),
);
}
}
class _SaveableForm extends StatefulWidget {
@override
State<_SaveableForm> createState() => _SaveableFormState();
}
class _SaveableFormState extends State<_SaveableForm> {
final TextEditingController _controller = TextEditingController();
String _savedValue = '';
bool _isDirty = false;
@override
void initState() {
super.initState();
_controller.addListener(_onChanged);
}
@override
void dispose() {
_controller.removeListener(_onChanged);
super.dispose();
}
void _onChanged() {
final bool nextIsDirty = _savedValue != _controller.text;
if (nextIsDirty == _isDirty) {
return;
}
setState(() {
_isDirty = nextIsDirty;
});
}
Future<void> _showDialog() async {
final bool? shouldDiscard = await showDialog<bool>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Are you sure?'),
content: const Text('Any unsaved changes will be lost!'),
actions: <Widget>[
TextButton(
child: const Text('Yes, discard my changes'),
onPressed: () {
Navigator.pop(context, true);
},
),
TextButton(
child: const Text('No, continue editing'),
onPressed: () {
Navigator.pop(context, false);
},
),
],
);
},
);
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();
}
}
void _save(String? value) {
setState(() {
_savedValue = value ?? '';
});
}
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text('If the field below is unsaved, a confirmation dialog will be shown on back.'),
const SizedBox(height: 20.0),
Form(
canPop: !_isDirty,
onPopInvoked: (bool didPop) {
if (didPop) {
return;
}
_showDialog();
},
autovalidateMode: AutovalidateMode.always,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextFormField(
controller: _controller,
onFieldSubmitted: (String? value) {
_save(value);
},
),
TextButton(
onPressed: () {
_save(_controller.text);
},
child: Row(
children: <Widget>[
const Text('Save'),
if (_controller.text.isNotEmpty)
Icon(
_isDirty ? Icons.warning : Icons.check,
),
],
),
),
],
),
),
TextButton(
onPressed: () {
if (_isDirty) {
_showDialog();
return;
}
// 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();
},
child: const Text('Go back'),
),
],
),
);
}
}
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
/// This sample demonstrates using [NavigatorPopHandler] to handle system back
/// gestures when there are nested [Navigator] widgets by delegating to the
/// current [Navigator].
void main() => runApp(const NavigatorPopHandlerApp());
class NavigatorPopHandlerApp extends StatelessWidget {
const NavigatorPopHandlerApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: '/',
routes: <String, WidgetBuilder>{
'/': (BuildContext context) => _HomePage(),
'/nested_navigators': (BuildContext context) => const NestedNavigatorsPage(),
},
);
}
}
class _HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Nested Navigators Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text('Home Page'),
const Text('A system back gesture here will exit the app.'),
const SizedBox(height: 20.0),
ListTile(
title: const Text('Nested Navigator route'),
subtitle: const Text('This route has another Navigator widget in addition to the one inside MaterialApp above.'),
onTap: () {
Navigator.of(context).pushNamed('/nested_navigators');
},
),
],
),
),
);
}
}
class NestedNavigatorsPage extends StatefulWidget {
const NestedNavigatorsPage({super.key});
@override
State<NestedNavigatorsPage> createState() => _NestedNavigatorsPageState();
}
class _NestedNavigatorsPageState extends State<NestedNavigatorsPage> {
final GlobalKey<NavigatorState> _nestedNavigatorKey = GlobalKey<NavigatorState>();
@override
Widget build(BuildContext context) {
return NavigatorPopHandler(
onPop: () {
_nestedNavigatorKey.currentState!.maybePop();
},
child: Navigator(
key: _nestedNavigatorKey,
initialRoute: 'nested_navigators/one',
onGenerateRoute: (RouteSettings settings) {
switch (settings.name) {
case 'nested_navigators/one':
final BuildContext rootContext = context;
return MaterialPageRoute<void>(
builder: (BuildContext context) => NestedNavigatorsPageOne(
onBack: () {
Navigator.of(rootContext).pop();
},
),
);
case 'nested_navigators/one/another_one':
return MaterialPageRoute<void>(
builder: (BuildContext context) => const NestedNavigatorsPageTwo(
),
);
default:
throw Exception('Invalid route: ${settings.name}');
}
},
),
);
}
}
class NestedNavigatorsPageOne extends StatelessWidget {
const NestedNavigatorsPageOne({
required this.onBack,
super.key,
});
final VoidCallback onBack;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey,
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text('Nested Navigators Page One'),
const Text('A system back here returns to the home page.'),
TextButton(
onPressed: () {
Navigator.of(context).pushNamed('nested_navigators/one/another_one');
},
child: const Text('Go to another route in this nested Navigator'),
),
TextButton(
// Can't use Navigator.of(context).pop() because this is the root
// route, so it can't be popped. The Navigator above this needs to
// be popped.
onPressed: onBack,
child: const Text('Go back'),
),
],
),
),
);
}
}
class NestedNavigatorsPageTwo extends StatelessWidget {
const NestedNavigatorsPageTwo({
super.key,
});
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey.withBlue(180),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text('Nested Navigators Page Two'),
const Text('A system back here will go back to Nested Navigators Page One'),
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('Go back'),
),
],
),
),
);
}
}
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This sample demonstrates nested navigation in a bottom navigation bar.
import 'package:flutter/material.dart';
// There are three possible tabs.
enum _Tab {
home,
one,
two,
}
// Each tab has two possible pages.
enum _TabPage {
home,
one,
}
typedef _TabPageCallback = void Function(List<_TabPage> pages);
void main() => runApp(const NavigatorPopHandlerApp());
class NavigatorPopHandlerApp extends StatelessWidget {
const NavigatorPopHandlerApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: '/home',
routes: <String, WidgetBuilder>{
'/home': (BuildContext context) => const _BottomNavPage(
),
},
);
}
}
class _BottomNavPage extends StatefulWidget {
const _BottomNavPage();
@override
State<_BottomNavPage> createState() => _BottomNavPageState();
}
class _BottomNavPageState extends State<_BottomNavPage> {
_Tab _tab = _Tab.home;
final GlobalKey _tabHomeKey = GlobalKey();
final GlobalKey _tabOneKey = GlobalKey();
final GlobalKey _tabTwoKey = GlobalKey();
List<_TabPage> _tabHomePages = <_TabPage>[_TabPage.home];
List<_TabPage> _tabOnePages = <_TabPage>[_TabPage.home];
List<_TabPage> _tabTwoPages = <_TabPage>[_TabPage.home];
BottomNavigationBarItem _itemForPage(_Tab page) {
switch (page) {
case _Tab.home:
return const BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Go to Home',
);
case _Tab.one:
return const BottomNavigationBarItem(
icon: Icon(Icons.one_k),
label: 'Go to One',
);
case _Tab.two:
return const BottomNavigationBarItem(
icon: Icon(Icons.two_k),
label: 'Go to Two',
);
}
}
Widget _getPage(_Tab page) {
switch (page) {
case _Tab.home:
return _BottomNavTab(
key: _tabHomeKey,
title: 'Home Tab',
color: Colors.grey,
pages: _tabHomePages,
onChangedPages: (List<_TabPage> pages) {
setState(() {
_tabHomePages = pages;
});
},
);
case _Tab.one:
return _BottomNavTab(
key: _tabOneKey,
title: 'Tab One',
color: Colors.amber,
pages: _tabOnePages,
onChangedPages: (List<_TabPage> pages) {
setState(() {
_tabOnePages = pages;
});
},
);
case _Tab.two:
return _BottomNavTab(
key: _tabTwoKey,
title: 'Tab Two',
color: Colors.blueGrey,
pages: _tabTwoPages,
onChangedPages: (List<_TabPage> pages) {
setState(() {
_tabTwoPages = pages;
});
},
);
}
}
void _onItemTapped(int index) {
setState(() {
_tab = _Tab.values.elementAt(index);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: _getPage(_tab),
),
bottomNavigationBar: BottomNavigationBar(
items: _Tab.values.map(_itemForPage).toList(),
currentIndex: _Tab.values.indexOf(_tab),
selectedItemColor: Colors.amber[800],
onTap: _onItemTapped,
),
);
}
}
class _BottomNavTab extends StatefulWidget {
const _BottomNavTab({
super.key,
required this.color,
required this.onChangedPages,
required this.pages,
required this.title,
});
final Color color;
final _TabPageCallback onChangedPages;
final List<_TabPage> pages;
final String title;
@override
State<_BottomNavTab> createState() => _BottomNavTabState();
}
class _BottomNavTabState extends State<_BottomNavTab> {
final GlobalKey<NavigatorState> _navigatorKey = GlobalKey<NavigatorState>();
@override
Widget build(BuildContext context) {
return NavigatorPopHandler(
onPop: () {
_navigatorKey.currentState?.maybePop();
},
child: Navigator(
key: _navigatorKey,
onPopPage: (Route<void> route, void result) {
if (!route.didPop(null)) {
return false;
}
widget.onChangedPages(<_TabPage>[
...widget.pages,
]..removeLast());
return true;
},
pages: widget.pages.map((_TabPage page) {
switch (page) {
case _TabPage.home:
return MaterialPage<void>(
child: _LinksPage(
title: 'Bottom nav - tab ${widget.title} - route $page',
backgroundColor: widget.color,
buttons: <Widget>[
TextButton(
onPressed: () {
widget.onChangedPages(<_TabPage>[
...widget.pages,
_TabPage.one,
]);
},
child: const Text('Go to another route in this nested Navigator'),
),
],
),
);
case _TabPage.one:
return MaterialPage<void>(
child: _LinksPage(
backgroundColor: widget.color,
title: 'Bottom nav - tab ${widget.title} - route $page',
buttons: <Widget>[
TextButton(
onPressed: () {
widget.onChangedPages(<_TabPage>[
...widget.pages,
]..removeLast());
},
child: const Text('Go back'),
),
],
),
);
}
}).toList(),
),
);
}
}
class _LinksPage extends StatelessWidget {
const _LinksPage ({
required this.backgroundColor,
this.buttons = const <Widget>[],
required this.title,
});
final Color backgroundColor;
final List<Widget> buttons;
final String title;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: backgroundColor,
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(title),
...buttons,
],
),
),
);
}
}
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This sample demonstrates showing a confirmation dialog before navigating
// away from a page.
import 'package:flutter/material.dart';
void main() => runApp(const NavigatorPopHandlerApp());
class NavigatorPopHandlerApp extends StatelessWidget {
const NavigatorPopHandlerApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: '/home',
routes: <String, WidgetBuilder>{
'/home': (BuildContext context) => const _HomePage(),
'/two': (BuildContext context) => const _PageTwo(),
},
);
}
}
class _HomePage extends StatefulWidget {
const _HomePage();
@override
State<_HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<_HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text('Page One'),
TextButton(
onPressed: () {
Navigator.of(context).pushNamed('/two');
},
child: const Text('Next page'),
),
],
),
),
);
}
}
class _PageTwo extends StatefulWidget {
const _PageTwo();
@override
State<_PageTwo> createState() => _PageTwoState();
}
class _PageTwoState extends State<_PageTwo> {
void _showBackDialog() {
showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Are you sure?'),
content: const Text(
'Are you sure you want to leave this page?',
),
actions: <Widget>[
TextButton(
style: TextButton.styleFrom(
textStyle: Theme.of(context).textTheme.labelLarge,
),
child: const Text('Nevermind'),
onPressed: () {
Navigator.pop(context);
},
),
TextButton(
style: TextButton.styleFrom(
textStyle: Theme.of(context).textTheme.labelLarge,
),
child: const Text('Leave'),
onPressed: () {
Navigator.pop(context);
Navigator.pop(context);
},
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text('Page Two'),
PopScope(
canPop: false,
onPopInvoked: (bool didPop) {
if (didPop) {
return;
}
_showBackDialog();
},
child: TextButton(
onPressed: () {
_showBackDialog();
},
child: const Text('Go back'),
),
),
],
),
),
);
}
}
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
/// Flutter code sample for [WillPopScope].
void main() => runApp(const WillPopScopeExampleApp());
class WillPopScopeExampleApp extends StatelessWidget {
const WillPopScopeExampleApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: WillPopScopeExample(),
);
}
}
class WillPopScopeExample extends StatefulWidget {
const WillPopScopeExample({super.key});
@override
State<WillPopScopeExample> createState() => _WillPopScopeExampleState();
}
class _WillPopScopeExampleState extends State<WillPopScopeExample> {
bool shouldPop = true;
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
return shouldPop;
},
child: Scaffold(
appBar: AppBar(
title: const Text('Flutter WillPopScope demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
OutlinedButton(
child: const Text('Push'),
onPressed: () {
Navigator.of(context).push<void>(
MaterialPageRoute<void>(
builder: (BuildContext context) {
return const WillPopScopeExample();
},
),
);
},
),
OutlinedButton(
child: Text('shouldPop: $shouldPop'),
onPressed: () {
setState(
() {
shouldPop = !shouldPop;
},
);
},
),
const Text('Push to a new screen, then tap on shouldPop '
'button to toggle its value. Press the back '
'button in the appBar to check its behavior '
'for different values of shouldPop'),
],
),
),
),
);
}
}
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter_api_samples/widgets/navigator_pop_handler/navigator_pop_handler.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';
import '../navigator_utils.dart';
void main() {
testWidgets('Can go back with system back gesture', (WidgetTester tester) async {
await tester.pumpWidget(
const example.NavigatorPopHandlerApp(),
);
expect(find.text('Nested Navigators Example'), findsOneWidget);
expect(find.text('Nested Navigators Page One'), findsNothing);
expect(find.text('Nested Navigators Page Two'), findsNothing);
await tester.tap(find.text('Nested Navigator route'));
await tester.pumpAndSettle();
expect(find.text('Nested Navigators Example'), findsNothing);
expect(find.text('Nested Navigators Page One'), findsOneWidget);
expect(find.text('Nested Navigators Page Two'), findsNothing);
await tester.tap(find.text('Go to another route in this nested Navigator'));
await tester.pumpAndSettle();
expect(find.text('Nested Navigators Example'), findsNothing);
expect(find.text('Nested Navigators Page One'), findsNothing);
expect(find.text('Nested Navigators Page Two'), findsOneWidget);
await simulateSystemBack();
await tester.pumpAndSettle();
expect(find.text('Nested Navigators Example'), findsNothing);
expect(find.text('Nested Navigators Page One'), findsOneWidget);
expect(find.text('Nested Navigators Page Two'), findsNothing);
await simulateSystemBack();
await tester.pumpAndSettle();
expect(find.text('Nested Navigators Example'), findsOneWidget);
expect(find.text('Nested Navigators Page One'), findsNothing);
expect(find.text('Nested Navigators Page Two'), findsNothing);
});
}
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter_api_samples/widgets/navigator_pop_handler/navigator_pop_handler.1.dart' as example;
import 'package:flutter_test/flutter_test.dart';
import '../navigator_utils.dart';
void main() {
testWidgets("System back gesture operates on current tab's nested Navigator", (WidgetTester tester) async {
await tester.pumpWidget(
const example.NavigatorPopHandlerApp(),
);
expect(find.text('Bottom nav - tab Home Tab - route _TabPage.home'), findsOneWidget);
// Go to the next route in this tab.
await tester.tap(find.text('Go to another route in this nested Navigator'));
await tester.pumpAndSettle();
expect(find.text('Bottom nav - tab Home Tab - route _TabPage.one'), findsOneWidget);
// Go to another tab.
await tester.tap(find.text('Go to One'));
await tester.pumpAndSettle();
expect(find.text('Bottom nav - tab Tab One - route _TabPage.home'), findsOneWidget);
// Return to the home tab. The navigation state is preserved.
await tester.tap(find.text('Go to Home'));
await tester.pumpAndSettle();
expect(find.text('Bottom nav - tab Home Tab - route _TabPage.one'), findsOneWidget);
// A back pops the navigation stack of the current tab's nested Navigator.
await simulateSystemBack();
await tester.pumpAndSettle();
expect(find.text('Bottom nav - tab Home Tab - route _TabPage.home'), findsOneWidget);
});
}
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
/// Simulates a system back, like a back gesture on Android.
///
/// Sends the same platform channel message that the engine sends when it
/// receives a system back.
Future<void> simulateSystemBack() {
return TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.handlePlatformMessage(
'flutter/navigation',
const JSONMessageCodec().encodeMessage(<String, dynamic>{
'method': 'popRoute',
}),
(ByteData? _) {},
);
}
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter_api_samples/widgets/pop_scope/pop_scope.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';
import '../navigator_utils.dart';
void main() {
testWidgets('Can choose to stay on page', (WidgetTester tester) async {
await tester.pumpWidget(
const example.NavigatorPopHandlerApp(),
);
expect(find.text('Page One'), findsOneWidget);
expect(find.text('Page Two'), findsNothing);
expect(find.text('Are you sure?'), findsNothing);
await tester.tap(find.text('Next page'));
await tester.pumpAndSettle();
expect(find.text('Page One'), findsNothing);
expect(find.text('Page Two'), findsOneWidget);
expect(find.text('Are you sure?'), findsNothing);
await simulateSystemBack();
await tester.pumpAndSettle();
expect(find.text('Page One'), findsNothing);
expect(find.text('Page Two'), findsOneWidget);
expect(find.text('Are you sure?'), findsOneWidget);
await tester.tap(find.text('Nevermind'));
await tester.pumpAndSettle();
expect(find.text('Page One'), findsNothing);
expect(find.text('Page Two'), findsOneWidget);
expect(find.text('Are you sure?'), findsNothing);
});
testWidgets('Can choose to go back', (WidgetTester tester) async {
await tester.pumpWidget(
const example.NavigatorPopHandlerApp(),
);
expect(find.text('Page One'), findsOneWidget);
expect(find.text('Page Two'), findsNothing);
expect(find.text('Are you sure?'), findsNothing);
await tester.tap(find.text('Next page'));
await tester.pumpAndSettle();
expect(find.text('Page One'), findsNothing);
expect(find.text('Page Two'), findsOneWidget);
expect(find.text('Are you sure?'), findsNothing);
await simulateSystemBack();
await tester.pumpAndSettle();
expect(find.text('Page One'), findsNothing);
expect(find.text('Page Two'), findsOneWidget);
expect(find.text('Are you sure?'), findsOneWidget);
await tester.tap(find.text('Leave'));
await tester.pumpAndSettle();
expect(find.text('Page One'), findsOneWidget);
expect(find.text('Page Two'), findsNothing);
expect(find.text('Are you sure?'), findsNothing);
});
}
......@@ -2,36 +2,31 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:flutter_api_samples/widgets/form/form.1.dart' as example;
import 'package:flutter_api_samples/widgets/will_pop_scope/will_pop_scope.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Can go back when form is clean', (WidgetTester tester) async {
testWidgets('pressing shouldPop button changes shouldPop', (WidgetTester tester) async {
await tester.pumpWidget(
const example.FormApp(),
const example.WillPopScopeExampleApp(),
);
expect(find.text('Are you sure?'), findsNothing);
await tester.tap(find.text('Go back'));
await tester.pumpAndSettle();
expect(find.text('Are you sure?'), findsNothing);
final Finder buttonFinder = find.text('shouldPop: true');
expect(buttonFinder, findsOneWidget);
await tester.tap(buttonFinder);
await tester.pump();
expect(find.text('shouldPop: false'), findsOneWidget);
});
testWidgets('Cannot go back when form is dirty', (WidgetTester tester) async {
testWidgets('pressing Push button pushes route', (WidgetTester tester) async {
await tester.pumpWidget(
const example.FormApp(),
const example.WillPopScopeExampleApp(),
);
expect(find.text('Are you sure?'), findsNothing);
await tester.enterText(find.byType(TextFormField), 'some new text');
await tester.tap(find.text('Go back'));
final Finder buttonFinder = find.text('Push');
expect(buttonFinder, findsOneWidget);
expect(find.byType(example.WillPopScopeExample), findsOneWidget);
await tester.tap(buttonFinder);
await tester.pumpAndSettle();
expect(find.text('Are you sure?'), findsOneWidget);
expect(find.byType(example.WillPopScopeExample, skipOffstage: false), findsNWidgets(2));
});
}
......@@ -157,7 +157,6 @@ class CupertinoApp extends StatefulWidget {
this.onGenerateRoute,
this.onGenerateInitialRoutes,
this.onUnknownRoute,
this.onNavigationNotification = WidgetsApp.defaultOnNavigationNotification,
List<NavigatorObserver> this.navigatorObservers = const <NavigatorObserver>[],
this.builder,
this.title = '',
......@@ -203,7 +202,6 @@ class CupertinoApp extends StatefulWidget {
this.builder,
this.title = '',
this.onGenerateTitle,
this.onNavigationNotification = WidgetsApp.defaultOnNavigationNotification,
this.color,
this.locale,
this.localizationsDelegates,
......@@ -270,9 +268,6 @@ class CupertinoApp extends StatefulWidget {
/// {@macro flutter.widgets.widgetsApp.onUnknownRoute}
final RouteFactory? onUnknownRoute;
/// {@macro flutter.widgets.widgetsApp.onNavigationNotification}
final NotificationListenerCallback<NavigationNotification>? onNavigationNotification;
/// {@macro flutter.widgets.widgetsApp.navigatorObservers}
final List<NavigatorObserver>? navigatorObservers;
......@@ -578,7 +573,6 @@ class _CupertinoAppState extends State<CupertinoApp> {
onGenerateRoute: widget.onGenerateRoute,
onGenerateInitialRoutes: widget.onGenerateInitialRoutes,
onUnknownRoute: widget.onUnknownRoute,
onNavigationNotification: widget.onNavigationNotification,
builder: widget.builder,
title: widget.title,
onGenerateTitle: widget.onGenerateTitle,
......
......@@ -196,8 +196,7 @@ mixin CupertinoRouteTransitionMixin<T> on PageRoute<T> {
}
// If attempts to dismiss this route might be vetoed such as in a page
// with forms, then do not allow the user to dismiss the route with a swipe.
if (route.hasScopedWillPopCallback
|| route.popDisposition == RoutePopDisposition.doNotPop) {
if (route.hasScopedWillPopCallback) {
return false;
}
// Fullscreen dialogs aren't dismissible by back swipe.
......
......@@ -162,39 +162,15 @@ class _CupertinoTabViewState extends State<CupertinoTabView> {
..add(_heroController);
}
GlobalKey<NavigatorState>? _ownedNavigatorKey;
GlobalKey<NavigatorState> get _navigatorKey {
if (widget.navigatorKey != null) {
return widget.navigatorKey!;
}
_ownedNavigatorKey ??= GlobalKey<NavigatorState>();
return _ownedNavigatorKey!;
}
// Whether this tab is currently the active tab.
bool get _isActive => TickerMode.of(context);
@override
Widget build(BuildContext context) {
final Widget child = Navigator(
key: _navigatorKey,
return Navigator(
key: widget.navigatorKey,
onGenerateRoute: _onGenerateRoute,
onUnknownRoute: _onUnknownRoute,
observers: _navigatorObservers,
restorationScopeId: widget.restorationScopeId,
);
// Handle system back gestures only if the tab is currently active.
return NavigatorPopHandler(
enabled: _isActive,
onPop: () {
if (!_isActive) {
return;
}
_navigatorKey.currentState!.pop();
},
child: child,
);
}
Route<dynamic>? _onGenerateRoute(RouteSettings settings) {
......
......@@ -1179,10 +1179,9 @@ class _MasterDetailFlowState extends State<_MasterDetailFlow> implements _PageOp
_builtLayout = _LayoutMode.nested;
final MaterialPageRoute<void> masterPageRoute = _masterPageRoute(context);
return NavigatorPopHandler(
onPop: () {
_navigatorKey.currentState!.maybePop();
},
return WillPopScope(
// Push pop check into nested navigator.
onWillPop: () async => !(await _navigatorKey.currentState!.maybePop()),
child: Navigator(
key: _navigatorKey,
initialRoute: 'initial',
......@@ -1235,10 +1234,12 @@ class _MasterDetailFlowState extends State<_MasterDetailFlow> implements _PageOp
MaterialPageRoute<void> _detailPageRoute(Object? arguments) {
return MaterialPageRoute<dynamic>(builder: (BuildContext context) {
return PopScope(
onPopInvoked: (bool didPop) {
return WillPopScope(
onWillPop: () async {
// No need for setState() as rebuild happens on navigation pop.
focus = _Focus.master;
Navigator.of(context).pop();
return false;
},
child: BlockSemantics(child: widget.detailPageBuilder(context, arguments, null)),
);
......
......@@ -214,7 +214,6 @@ class MaterialApp extends StatefulWidget {
this.onGenerateRoute,
this.onGenerateInitialRoutes,
this.onUnknownRoute,
this.onNavigationNotification = WidgetsApp.defaultOnNavigationNotification,
List<NavigatorObserver> this.navigatorObservers = const <NavigatorObserver>[],
this.builder,
this.title = '',
......@@ -268,7 +267,6 @@ class MaterialApp extends StatefulWidget {
this.builder,
this.title = '',
this.onGenerateTitle,
this.onNavigationNotification = WidgetsApp.defaultOnNavigationNotification,
this.color,
this.theme,
this.darkTheme,
......@@ -345,9 +343,6 @@ class MaterialApp extends StatefulWidget {
/// {@macro flutter.widgets.widgetsApp.onUnknownRoute}
final RouteFactory? onUnknownRoute;
/// {@macro flutter.widgets.widgetsApp.onNavigationNotification}
final NotificationListenerCallback<NavigationNotification>? onNavigationNotification;
/// {@macro flutter.widgets.widgetsApp.navigatorObservers}
final List<NavigatorObserver>? navigatorObservers;
......@@ -1024,7 +1019,6 @@ class _MaterialAppState extends State<MaterialApp> {
onGenerateRoute: widget.onGenerateRoute,
onGenerateInitialRoutes: widget.onGenerateInitialRoutes,
onUnknownRoute: widget.onUnknownRoute,
onNavigationNotification: widget.onNavigationNotification,
builder: _materialBuilder,
title: widget.title,
onGenerateTitle: widget.onGenerateTitle,
......
......@@ -2,44 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/foundation.dart';
import 'system_channels.dart';
/// Controls specific aspects of the system navigation stack.
abstract final class SystemNavigator {
/// Informs the platform of whether or not the Flutter framework will handle
/// back events.
///
/// Currently, this is used only on Android to inform its use of the
/// predictive back gesture when exiting the app. When true, predictive back
/// is disabled.
///
/// See also:
///
/// * The
/// [migration guide](https://developer.android.com/guide/navigation/predictive-back-gesture)
/// for predictive back in native Android apps.
static Future<void> setFrameworkHandlesBack(bool frameworkHandlesBack) async {
// Currently, this method call is only relevant on Android.
if (kIsWeb) {
return;
}
switch (defaultTargetPlatform) {
case TargetPlatform.iOS:
case TargetPlatform.macOS:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
return;
case TargetPlatform.android:
return SystemChannels.platform.invokeMethod<void>(
'SystemNavigator.setFrameworkHandlesBack',
frameworkHandlesBack,
);
}
}
/// Removes the topmost Flutter instance, presenting what was before
/// it.
///
......
......@@ -19,7 +19,6 @@ import 'framework.dart';
import 'localizations.dart';
import 'media_query.dart';
import 'navigator.dart';
import 'notification_listener.dart';
import 'pages.dart';
import 'performance_overlay.dart';
import 'restoration.dart';
......@@ -314,7 +313,6 @@ class WidgetsApp extends StatefulWidget {
this.onGenerateRoute,
this.onGenerateInitialRoutes,
this.onUnknownRoute,
this.onNavigationNotification = defaultOnNavigationNotification,
List<NavigatorObserver> this.navigatorObservers = const <NavigatorObserver>[],
this.initialRoute,
this.pageRouteBuilder,
......@@ -422,7 +420,6 @@ class WidgetsApp extends StatefulWidget {
this.builder,
this.title = '',
this.onGenerateTitle,
this.onNavigationNotification = defaultOnNavigationNotification,
this.textStyle,
required this.color,
this.locale,
......@@ -704,17 +701,6 @@ class WidgetsApp extends StatefulWidget {
/// {@endtemplate}
final RouteFactory? onUnknownRoute;
/// {@template flutter.widgets.widgetsApp.onNavigationNotification}
/// The callback to use when receiving a [NavigationNotification].
///
/// By default set to [WidgetsApp.defaultOnNavigationNotification], which
/// updates the engine with the navigation status.
///
/// If null, [NavigationNotification] is not listened for at all, and so will
/// continue to propagate.
/// {@endtemplate}
final NotificationListenerCallback<NavigationNotification>? onNavigationNotification;
/// {@template flutter.widgets.widgetsApp.initialRoute}
/// The name of the first route to show, if a [Navigator] is built.
///
......@@ -1328,15 +1314,6 @@ class WidgetsApp extends StatefulWidget {
VoidCallbackIntent: VoidCallbackAction(),
};
/// The default value for [onNavigationNotification].
///
/// Updates the platform with [NavigationNotification.canHandlePop] and stops
/// bubbling.
static bool defaultOnNavigationNotification(NavigationNotification notification) {
SystemNavigator.setFrameworkHandlesBack(notification.canHandlePop);
return true;
}
@override
State<WidgetsApp> createState() => _WidgetsAppState();
}
......@@ -1771,25 +1748,30 @@ class _WidgetsAppState extends State<WidgetsApp> with WidgetsBindingObserver {
assert(_debugCheckLocalizations(appLocale));
Widget child = Shortcuts(
debugLabel: '<Default WidgetsApp Shortcuts>',
shortcuts: widget.shortcuts ?? WidgetsApp.defaultShortcuts,
// DefaultTextEditingShortcuts is nested inside Shortcuts so that it can
// fall through to the defaultShortcuts.
child: DefaultTextEditingShortcuts(
child: Actions(
actions: widget.actions ?? <Type, Action<Intent>>{
...WidgetsApp.defaultActions,
ScrollIntent: Action<ScrollIntent>.overridable(context: context, defaultAction: ScrollAction()),
},
child: FocusTraversalGroup(
policy: ReadingOrderTraversalPolicy(),
child: TapRegionSurface(
child: ShortcutRegistrar(
child: Localizations(
locale: appLocale,
delegates: _localizationsDelegates.toList(),
child: title,
return RootRestorationScope(
restorationId: widget.restorationScopeId,
child: SharedAppData(
child: Shortcuts(
debugLabel: '<Default WidgetsApp Shortcuts>',
shortcuts: widget.shortcuts ?? WidgetsApp.defaultShortcuts,
// DefaultTextEditingShortcuts is nested inside Shortcuts so that it can
// fall through to the defaultShortcuts.
child: DefaultTextEditingShortcuts(
child: Actions(
actions: widget.actions ?? <Type, Action<Intent>>{
...WidgetsApp.defaultActions,
ScrollIntent: Action<ScrollIntent>.overridable(context: context, defaultAction: ScrollAction()),
},
child: FocusTraversalGroup(
policy: ReadingOrderTraversalPolicy(),
child: TapRegionSurface(
child: ShortcutRegistrar(
child: Localizations(
locale: appLocale,
delegates: _localizationsDelegates.toList(),
child: title,
),
),
),
),
),
......@@ -1797,19 +1779,5 @@ class _WidgetsAppState extends State<WidgetsApp> with WidgetsBindingObserver {
),
),
);
if (widget.onNavigationNotification != null) {
child = NotificationListener<NavigationNotification>(
onNotification: widget.onNavigationNotification,
child: child,
);
}
return RootRestorationScope(
restorationId: widget.restorationScopeId,
child: SharedAppData(
child: child,
),
);
}
}
......@@ -54,8 +54,9 @@ export 'dart:ui' show AppLifecycleState, Locale;
/// ** See code in examples/api/lib/widgets/binding/widget_binding_observer.0.dart **
/// {@end-tool}
abstract mixin class WidgetsBindingObserver {
/// Called when the system tells the app to pop the current route, such as
/// after a system back button press or back gesture.
/// Called when the system tells the app to pop the current route.
/// For example, on Android, this is called when the user presses
/// the back button.
///
/// Observers are notified in registration order until one returns
/// true. If none return true, the application quits.
......@@ -68,8 +69,6 @@ abstract mixin class WidgetsBindingObserver {
///
/// This method exposes the `popRoute` notification from
/// [SystemChannels.navigation].
///
/// {@macro flutter.widgets.AndroidPredictiveBack}
Future<bool> didPopRoute() => Future<bool>.value(false);
/// Called when the host tells the application to push a new route onto the
......@@ -721,27 +720,6 @@ mixin WidgetsBinding on BindingBase, ServicesBinding, SchedulerBinding, GestureB
///
/// This method exposes the `popRoute` notification from
/// [SystemChannels.navigation].
///
/// {@template flutter.widgets.AndroidPredictiveBack}
/// ## Handling backs ahead of time
///
/// Not all system backs will result in a call to this method. Some are
/// handled entirely by the system without informing the Flutter framework.
///
/// Android API 33+ introduced a feature called predictive back, which allows
/// the user to peek behind the current app or route during a back gesture and
/// then decide to cancel or commit the back. Flutter enables or disables this
/// feature ahead of time, before a back gesture occurs, and back gestures
/// that trigger predictive back are handled entirely by the system and do not
/// trigger this method here in the framework.
///
/// By default, the framework communicates when it would like to handle system
/// back gestures using [SystemNavigator.setFrameworkHandlesBack] in
/// [WidgetsApp.defaultOnNavigationNotification]. This is done automatically
/// based on the status of the [Navigator] stack and the state of any
/// [PopScope] widgets present. Developers can manually set this by calling
/// the method directly or by using [NavigationNotification].
/// {@endtemplate}
@protected
@visibleForTesting
Future<void> handlePopRoute() async {
......
......@@ -10,10 +10,8 @@ import 'package:flutter/rendering.dart';
import 'basic.dart';
import 'framework.dart';
import 'navigator.dart';
import 'pop_scope.dart';
import 'restoration.dart';
import 'restoration_properties.dart';
import 'routes.dart';
import 'will_pop_scope.dart';
// Duration for delay before announcement in IOS so that the announcement won't be interrupted.
......@@ -54,17 +52,10 @@ class Form extends StatefulWidget {
const Form({
super.key,
required this.child,
this.canPop,
this.onPopInvoked,
@Deprecated(
'Use canPop and/or onPopInvoked instead. '
'This feature was deprecated after v3.12.0-1.0.pre.',
)
this.onWillPop,
this.onChanged,
AutovalidateMode? autovalidateMode,
}) : autovalidateMode = autovalidateMode ?? AutovalidateMode.disabled,
assert((onPopInvoked == null && canPop == null) || onWillPop == null, 'onWillPop is deprecated; use canPop and/or onPopInvoked.');
}) : autovalidateMode = autovalidateMode ?? AutovalidateMode.disabled;
/// Returns the [FormState] of the closest [Form] widget which encloses the
/// given context, or null if none is found.
......@@ -143,44 +134,8 @@ class Form extends StatefulWidget {
///
/// * [WillPopScope], another widget that provides a way to intercept the
/// back button.
@Deprecated(
'Use canPop and/or onPopInvoked instead. '
'This feature was deprecated after v3.12.0-1.0.pre.',
)
final WillPopCallback? onWillPop;
/// {@macro flutter.widgets.PopScope.canPop}
///
/// {@tool dartpad}
/// This sample demonstrates how to use this parameter to show a confirmation
/// dialog when a navigation pop would cause form data to be lost.
///
/// ** See code in examples/api/lib/widgets/form/form.1.dart **
/// {@end-tool}
///
/// See also:
///
/// * [onPopInvoked], which also comes from [PopScope] and is often used in
/// conjunction with this parameter.
/// * [PopScope.canPop], which is what [Form] delegates to internally.
final bool? canPop;
/// {@macro flutter.widgets.navigator.onPopInvoked}
///
/// {@tool dartpad}
/// This sample demonstrates how to use this parameter to show a confirmation
/// dialog when a navigation pop would cause form data to be lost.
///
/// ** See code in examples/api/lib/widgets/form/form.1.dart **
/// {@end-tool}
///
/// See also:
///
/// * [canPop], which also comes from [PopScope] and is often used in
/// conjunction with this parameter.
/// * [PopScope.onPopInvoked], which is what [Form] delegates to internally.
final PopInvokedCallback? onPopInvoked;
/// Called when one of the form fields changes.
///
/// In addition to this callback being invoked, all the form fields themselves
......@@ -245,18 +200,6 @@ class FormState extends State<Form> {
break;
}
if (widget.canPop != null || widget.onPopInvoked != null) {
return PopScope(
canPop: widget.canPop ?? true,
onPopInvoked: widget.onPopInvoked,
child: _FormScope(
formState: this,
generation: _generation,
child: widget.child,
),
);
}
return WillPopScope(
onWillPop: widget.onWillPop,
child: _FormScope(
......
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'framework.dart';
import 'navigator.dart';
import 'notification_listener.dart';
import 'pop_scope.dart';
/// Enables the handling of system back gestures.
///
/// Typically wraps a nested [Navigator] widget and allows it to handle system
/// back gestures in the [onPop] callback.
///
/// {@tool dartpad}
/// This sample demonstrates how to use this widget to properly handle system
/// back gestures when using nested [Navigator]s.
///
/// ** See code in examples/api/lib/widgets/navigator_pop_handler/navigator_pop_handler.0.dart **
/// {@end-tool}
///
/// {@tool dartpad}
/// This sample demonstrates how to use this widget to properly handle system
/// back gestures with a bottom navigation bar whose tabs each have their own
/// nested [Navigator]s.
///
/// ** See code in examples/api/lib/widgets/navigator_pop_handler/navigator_pop_handler.1.dart **
/// {@end-tool}
///
/// See also:
///
/// * [PopScope], which allows toggling the ability of a [Navigator] to
/// handle pops.
/// * [NavigationNotification], which indicates whether a [Navigator] in a
/// subtree can handle pops.
class NavigatorPopHandler extends StatefulWidget {
/// Creates an instance of [NavigatorPopHandler].
const NavigatorPopHandler({
super.key,
this.onPop,
this.enabled = true,
required this.child,
});
/// The widget to place below this in the widget tree.
///
/// Typically this is a [Navigator] that will handle the pop when [onPop] is
/// called.
final Widget child;
/// Whether this widget's ability to handle system back gestures is enabled or
/// disabled.
///
/// When false, there will be no effect on system back gestures. If provided,
/// [onPop] will still be called.
///
/// This can be used, for example, when the nested [Navigator] is no longer
/// active but remains in the widget tree, such as in an inactive tab.
///
/// Defaults to true.
final bool enabled;
/// Called when a handleable pop event happens.
///
/// For example, a pop is handleable when a [Navigator] in [child] has
/// multiple routes on its stack. It's not handleable when it has only a
/// single route, and so [onPop] will not be called.
///
/// Typically this is used to pop the [Navigator] in [child]. See the sample
/// code on [NavigatorPopHandler] for a full example of this.
final VoidCallback? onPop;
@override
State<NavigatorPopHandler> createState() => _NavigatorPopHandlerState();
}
class _NavigatorPopHandlerState extends State<NavigatorPopHandler> {
bool _canPop = true;
@override
Widget build(BuildContext context) {
// When the widget subtree indicates it can handle a pop, disable popping
// here, so that it can be manually handled in canPop.
return PopScope(
canPop: !widget.enabled || _canPop,
onPopInvoked: (bool didPop) {
if (didPop) {
return;
}
widget.onPop?.call();
},
// Listen to changes in the navigation stack in the widget subtree.
child: NotificationListener<NavigationNotification>(
onNotification: (NavigationNotification notification) {
// If this subtree cannot handle pop, then set canPop to true so
// that our PopScope will allow the Navigator higher in the tree to
// handle the pop instead.
final bool nextCanPop = !notification.canHandlePop;
if (nextCanPop != _canPop) {
setState(() {
_canPop = nextCanPop;
});
}
return false;
},
child: widget.child,
),
);
}
}
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/foundation.dart';
import 'framework.dart';
import 'navigator.dart';
import 'routes.dart';
/// Manages system back gestures.
///
/// The [canPop] parameter can be used to disable system back gestures. Defaults
/// to true, meaning that back gestures happen as usual.
///
/// The [onPopInvoked] parameter reports when system back gestures occur,
/// regardless of whether or not they were successful.
///
/// If [canPop] is false, then a system back gesture will not pop the route off
/// of the enclosing [Navigator]. [onPopInvoked] will still be called, and
/// `didPop` will be `false`.
///
/// If [canPop] is true, then a system back gesture will cause the enclosing
/// [Navigator] to receive a pop as usual. [onPopInvoked] will be called with
/// `didPop` as `true`, unless the pop failed for reasons unrelated to
/// [PopScope], in which case it will be `false`.
///
/// {@tool dartpad}
/// This sample demonstrates how to use this widget to handle nested navigation
/// in a bottom navigation bar.
///
/// ** See code in examples/api/lib/widgets/pop_scope/pop_scope.0.dart **
/// {@end-tool}
///
/// See also:
///
/// * [NavigatorPopHandler], which is a less verbose way to handle system back
/// gestures in simple cases of nested [Navigator]s.
/// * [Form.canPop] and [Form.onPopInvoked], which can be used to handle system
/// back gestures in the case of a form with unsaved data.
/// * [ModalRoute.registerPopEntry] and [ModalRoute.unregisterPopEntry],
/// which this widget uses to integrate with Flutter's navigation system.
class PopScope extends StatefulWidget {
/// Creates a widget that registers a callback to veto attempts by the user to
/// dismiss the enclosing [ModalRoute].
const PopScope({
super.key,
required this.child,
this.canPop = true,
this.onPopInvoked,
});
/// The widget below this widget in the tree.
///
/// {@macro flutter.widgets.ProxyWidget.child}
final Widget child;
/// {@template flutter.widgets.PopScope.onPopInvoked}
/// Called after a route pop was handled.
/// {@endtemplate}
///
/// It's not possible to prevent the pop from happening at the time that this
/// method is called; the pop has already happened. Use [canPop] to
/// disable pops in advance.
///
/// This will still be called even when the pop is canceled. A pop is canceled
/// when the relevant [Route.popDisposition] returns false, such as when
/// [canPop] is set to false on a [PopScope]. The `didPop` parameter
/// indicates whether or not the back navigation actually happened
/// successfully.
///
/// See also:
///
/// * [Route.onPopInvoked], which is similar.
final PopInvokedCallback? onPopInvoked;
/// {@template flutter.widgets.PopScope.canPop}
/// When false, blocks the current route from being popped.
///
/// This includes the root route, where upon popping, the Flutter app would
/// exit.
///
/// If multiple [PopScope] widgets appear in a route's widget subtree, then
/// each and every `canPop` must be `true` in order for the route to be
/// able to pop.
///
/// [Android's predictive back](https://developer.android.com/guide/navigation/predictive-back-gesture)
/// feature will not animate when this boolean is false.
/// {@endtemplate}
final bool canPop;
@override
State<PopScope> createState() => _PopScopeState();
}
class _PopScopeState extends State<PopScope> implements PopEntry {
ModalRoute<dynamic>? _route;
@override
PopInvokedCallback? get onPopInvoked => widget.onPopInvoked;
@override
late final ValueNotifier<bool> canPopNotifier;
@override
void initState() {
super.initState();
canPopNotifier = ValueNotifier<bool>(widget.canPop);
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
final ModalRoute<dynamic>? nextRoute = ModalRoute.of(context);
if (nextRoute != _route) {
_route?.unregisterPopEntry(this);
_route = nextRoute;
_route?.registerPopEntry(this);
}
}
@override
void didUpdateWidget(PopScope oldWidget) {
super.didUpdateWidget(oldWidget);
canPopNotifier.value = widget.canPop;
}
@override
void dispose() {
_route?.unregisterPopEntry(this);
canPopNotifier.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) => widget.child;
}
......@@ -9,25 +9,26 @@ import 'routes.dart';
/// Registers a callback to veto attempts by the user to dismiss the enclosing
/// [ModalRoute].
///
/// {@tool dartpad}
/// Whenever the back button is pressed, you will get a callback at [onWillPop],
/// which returns a [Future]. If the [Future] returns true, the screen is
/// popped.
///
/// ** See code in examples/api/lib/widgets/will_pop_scope/will_pop_scope.0.dart **
/// {@end-tool}
///
/// See also:
///
/// * [ModalRoute.addScopedWillPopCallback] and [ModalRoute.removeScopedWillPopCallback],
/// which this widget uses to register and unregister [onWillPop].
/// * [Form], which provides an `onWillPop` callback that enables the form
/// to veto a `pop` initiated by the app's back button.
@Deprecated(
'Use PopScope instead. '
'This feature was deprecated after v3.12.0-1.0.pre.',
)
///
class WillPopScope extends StatefulWidget {
/// Creates a widget that registers a callback to veto attempts by the user to
/// dismiss the enclosing [ModalRoute].
///
/// The [child] argument must not be null.
@Deprecated(
'Use PopScope instead. '
'This feature was deprecated after v3.12.0-1.0.pre.',
)
const WillPopScope({
super.key,
required this.child,
......
......@@ -81,7 +81,6 @@ export 'src/widgets/media_query.dart';
export 'src/widgets/modal_barrier.dart';
export 'src/widgets/navigation_toolbar.dart';
export 'src/widgets/navigator.dart';
export 'src/widgets/navigator_pop_handler.dart';
export 'src/widgets/nested_scroll_view.dart';
export 'src/widgets/notification_listener.dart';
export 'src/widgets/orientation_builder.dart';
......@@ -96,7 +95,6 @@ export 'src/widgets/placeholder.dart';
export 'src/widgets/platform_menu_bar.dart';
export 'src/widgets/platform_selectable_region_context_menu.dart';
export 'src/widgets/platform_view.dart';
export 'src/widgets/pop_scope.dart';
export 'src/widgets/preferred_size.dart';
export 'src/widgets/primary_scroll_controller.dart';
export 'src/widgets/raw_keyboard_listener.dart';
......
......@@ -2,14 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:typed_data';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import '../image_data.dart';
import '../rendering/rendering_tester.dart' show TestCallbackPainter;
import '../widgets/navigator_utils.dart';
late List<int> selectedTabs;
......@@ -1216,132 +1215,6 @@ void main() {
expect(find.text('Content 2'), findsNothing);
expect(find.text('Content 3'), findsNothing);
});
group('Android Predictive Back', () {
bool? lastFrameworkHandlesBack;
setUp(() {
// Initialize to false. Because this uses a static boolean internally, it
// is not reset between tests or calls to pumpWidget. Explicitly setting
// it to false before each test makes them behave deterministically.
SystemNavigator.setFrameworkHandlesBack(false);
lastFrameworkHandlesBack = null;
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(SystemChannels.platform, (MethodCall methodCall) async {
if (methodCall.method == 'SystemNavigator.setFrameworkHandlesBack') {
expect(methodCall.arguments, isA<bool>());
lastFrameworkHandlesBack = methodCall.arguments as bool;
}
return;
});
});
tearDown(() {
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(SystemChannels.platform, null);
SystemNavigator.setFrameworkHandlesBack(true);
});
testWidgets('System back navigation inside of tabs', (WidgetTester tester) async {
await tester.pumpWidget(
CupertinoApp(
home: MediaQuery(
data: const MediaQueryData(
viewInsets: EdgeInsets.only(bottom: 200),
),
child: CupertinoTabScaffold(
tabBar: _buildTabBar(),
tabBuilder: (BuildContext context, int index) {
return CupertinoTabView(
builder: (BuildContext context) {
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
middle: Text('Page 1 of tab ${index + 1}'),
),
child: Center(
child: CupertinoButton(
child: const Text('Next page'),
onPressed: () {
Navigator.of(context).push(
CupertinoPageRoute<void>(
builder: (BuildContext context) {
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
middle: Text('Page 2 of tab ${index + 1}'),
),
child: Center(
child: CupertinoButton(
child: const Text('Back'),
onPressed: () {
Navigator.of(context).pop();
},
),
),
);
},
),
);
},
),
),
);
},
);
},
),
),
),
);
expect(find.text('Page 1 of tab 1'), findsOneWidget);
expect(find.text('Page 2 of tab 1'), findsNothing);
expect(lastFrameworkHandlesBack, isFalse);
await tester.tap(find.text('Next page'));
await tester.pumpAndSettle();
expect(find.text('Page 1 of tab 1'), findsNothing);
expect(find.text('Page 2 of tab 1'), findsOneWidget);
expect(lastFrameworkHandlesBack, isTrue);
await simulateSystemBack();
await tester.pumpAndSettle();
expect(find.text('Page 1 of tab 1'), findsOneWidget);
expect(find.text('Page 2 of tab 1'), findsNothing);
expect(lastFrameworkHandlesBack, isFalse);
await tester.tap(find.text('Next page'));
await tester.pumpAndSettle();
expect(find.text('Page 1 of tab 1'), findsNothing);
expect(find.text('Page 2 of tab 1'), findsOneWidget);
expect(lastFrameworkHandlesBack, isTrue);
await tester.tap(find.text('Tab 2'));
await tester.pumpAndSettle();
expect(find.text('Page 1 of tab 2'), findsOneWidget);
expect(find.text('Page 2 of tab 2'), findsNothing);
expect(lastFrameworkHandlesBack, isFalse);
await tester.tap(find.text('Tab 1'));
await tester.pumpAndSettle();
expect(find.text('Page 1 of tab 1'), findsNothing);
expect(find.text('Page 2 of tab 1'), findsOneWidget);
expect(lastFrameworkHandlesBack, isTrue);
await simulateSystemBack();
await tester.pumpAndSettle();
expect(find.text('Page 1 of tab 1'), findsOneWidget);
expect(find.text('Page 2 of tab 1'), findsNothing);
expect(lastFrameworkHandlesBack, isFalse);
await tester.tap(find.text('Tab 2'));
await tester.pumpAndSettle();
expect(find.text('Page 1 of tab 2'), findsOneWidget);
expect(find.text('Page 2 of tab 2'), findsNothing);
expect(lastFrameworkHandlesBack, isFalse);
},
variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android }),
skip: kIsWeb, // [intended] frameworkHandlesBack not used on web.
);
});
}
CupertinoTabBar _buildTabBar({ int selectedTab = 0 }) {
......
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
/// Simulates a system back, like a back gesture on Android.
///
/// Sends the same platform channel message that the engine sends when it
/// receives a system back.
Future<void> simulateSystemBack() {
return TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.handlePlatformMessage(
'flutter/navigation',
const JSONMessageCodec().encodeMessage(<String, dynamic>{
'method': 'popRoute',
}),
(ByteData? _) {},
);
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment