-
Notifications
You must be signed in to change notification settings - Fork 26.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactors page API #137792
Merged
+358
−76
Merged
Refactors page API #137792
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// 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 PageApiExampleApp()); | ||
|
||
class PageApiExampleApp extends StatefulWidget { | ||
const PageApiExampleApp({super.key}); | ||
|
||
@override | ||
State<PageApiExampleApp> createState() => _PageApiExampleAppState(); | ||
} | ||
|
||
class _PageApiExampleAppState extends State<PageApiExampleApp> { | ||
final RouterDelegate<Object> delegate = MyRouterDelegate(); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp.router( | ||
routerDelegate: delegate, | ||
); | ||
} | ||
} | ||
|
||
class MyRouterDelegate extends RouterDelegate<Object> with PopNavigatorRouterDelegateMixin<Object>, ChangeNotifier { | ||
// This example doesn't use RouteInformationProvider. | ||
@override | ||
Future<void> setNewRoutePath(Object configuration) async => throw UnimplementedError(); | ||
|
||
@override | ||
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>(); | ||
|
||
static MyRouterDelegate of(BuildContext context) => Router.of(context).routerDelegate as MyRouterDelegate; | ||
|
||
bool get showDetailPage => _showDetailPage; | ||
bool _showDetailPage = false; | ||
set showDetailPage(bool value) { | ||
if (_showDetailPage == value) { | ||
return; | ||
} | ||
_showDetailPage = value; | ||
notifyListeners(); | ||
} | ||
|
||
Future<bool> _showConfirmDialog() async { | ||
return await showDialog<bool>( | ||
context: navigatorKey.currentContext!, | ||
barrierDismissible: false, | ||
builder: (BuildContext context) { | ||
return AlertDialog( | ||
title: const Text('Are you sure?'), | ||
actions: <Widget>[ | ||
TextButton( | ||
child: const Text('Cancel'), | ||
onPressed: () { | ||
Navigator.of(context).pop(false); | ||
}, | ||
), | ||
TextButton( | ||
child: const Text('Confirm'), | ||
onPressed: () { | ||
Navigator.of(context).pop(true); | ||
}, | ||
), | ||
], | ||
); | ||
}, | ||
) ?? false; | ||
} | ||
|
||
Future<void> _handlePopDetails(bool didPop, void result) async { | ||
if (didPop) { | ||
showDetailPage = false; | ||
return; | ||
} | ||
final bool confirmed = await _showConfirmDialog(); | ||
if (confirmed) { | ||
showDetailPage = false; | ||
} | ||
} | ||
|
||
List<Page<Object?>> _getPages() { | ||
return <Page<Object?>>[ | ||
const MaterialPage<void>(key: ValueKey<String>('home'), child: _HomePage()), | ||
if (showDetailPage) | ||
MaterialPage<void>( | ||
key: const ValueKey<String>('details'), | ||
child: const _DetailsPage(), | ||
canPop: false, | ||
onPopInvoked: _handlePopDetails, | ||
), | ||
]; | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Navigator( | ||
key: navigatorKey, | ||
pages: _getPages(), | ||
onDidRemovePage: (Page<Object?> page) { | ||
assert(page.key == const ValueKey<String>('details')); | ||
showDetailPage = false; | ||
}, | ||
); | ||
} | ||
} | ||
|
||
class _HomePage extends StatefulWidget { | ||
const _HomePage(); | ||
|
||
@override | ||
State<_HomePage> createState() => _HomePageState(); | ||
} | ||
|
||
class _HomePageState extends State<_HomePage> { | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar(title: const Text('Home')), | ||
body: Center( | ||
child: TextButton( | ||
onPressed: () { | ||
MyRouterDelegate.of(context).showDetailPage = true; | ||
}, | ||
child: const Text('Go to details'), | ||
), | ||
), | ||
); | ||
} | ||
} | ||
|
||
class _DetailsPage extends StatefulWidget { | ||
const _DetailsPage(); | ||
|
||
@override | ||
State<_DetailsPage> createState() => _DetailsPageState(); | ||
} | ||
|
||
class _DetailsPageState extends State<_DetailsPage> { | ||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar(title: const Text('Details')), | ||
body: Center( | ||
child: TextButton( | ||
onPressed: () { | ||
Navigator.of(context).maybePop(); | ||
}, | ||
child: const Text('Go back'), | ||
), | ||
), | ||
); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// 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/page/page_can_pop.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.PageApiExampleApp(), | ||
); | ||
|
||
expect(find.text('Home'), findsOneWidget); | ||
|
||
await tester.tap(find.text('Go to details')); | ||
await tester.pumpAndSettle(); | ||
expect(find.text('Home'), findsNothing); | ||
expect(find.text('Details'), findsOneWidget); | ||
|
||
await simulateSystemBack(); | ||
await tester.pumpAndSettle(); | ||
expect(find.text('Are you sure?'), findsOneWidget); | ||
|
||
await tester.tap(find.text('Cancel')); | ||
await tester.pumpAndSettle(); | ||
expect(find.text('Home'), findsNothing); | ||
expect(find.text('Details'), findsOneWidget); | ||
}); | ||
|
||
testWidgets('Can choose to go back', (WidgetTester tester) async { | ||
await tester.pumpWidget( | ||
const example.PageApiExampleApp(), | ||
); | ||
|
||
expect(find.text('Home'), findsOneWidget); | ||
|
||
await tester.tap(find.text('Go to details')); | ||
await tester.pumpAndSettle(); | ||
expect(find.text('Home'), findsNothing); | ||
expect(find.text('Details'), findsOneWidget); | ||
|
||
await simulateSystemBack(); | ||
await tester.pumpAndSettle(); | ||
expect(find.text('Are you sure?'), findsOneWidget); | ||
|
||
await tester.tap(find.text('Confirm')); | ||
await tester.pumpAndSettle(); | ||
expect(find.text('Details'), findsNothing); | ||
expect(find.text('Home'), findsOneWidget); | ||
}); | ||
} |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this is called when a system back happens I guess?