mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 13:58:47 +00:00
Add navigator 2 samples (#606)
* add navigation_and_routing sample * Add navigation samples and README * remove "goals" section * add newlines * add copyright headers * Update README, remove pubspec comments, add description
This commit is contained in:
61
navigation_and_routing/lib/nav_1/anonymous_routes.dart
Normal file
61
navigation_and_routing/lib/nav_1/anonymous_routes.dart
Normal file
@@ -0,0 +1,61 @@
|
||||
// Copyright 2020, the Flutter project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
/// Shows how to use [Navigator] APIs to push and pop an anonymous
|
||||
/// route. In this case, it is an instance of [MaterialPageRoute].
|
||||
library anonymous_routes;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() {
|
||||
runApp(Nav2App());
|
||||
}
|
||||
|
||||
class Nav2App extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
home: HomeScreen(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class HomeScreen extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: Center(
|
||||
child: FlatButton(
|
||||
child: Text('View Details'),
|
||||
onPressed: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) {
|
||||
return DetailScreen();
|
||||
}),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class DetailScreen extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: Center(
|
||||
child: FlatButton(
|
||||
child: Text('Pop!'),
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
62
navigation_and_routing/lib/nav_1/named_routes.dart
Normal file
62
navigation_and_routing/lib/nav_1/named_routes.dart
Normal file
@@ -0,0 +1,62 @@
|
||||
// Copyright 2020, the Flutter project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
/// Shows how to use define named routes via the `routes` parameter on
|
||||
/// MaterialApp, and navigate using Navigator.pushNamed.
|
||||
library named_routes;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() {
|
||||
runApp(Nav2App());
|
||||
}
|
||||
|
||||
class Nav2App extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
routes: {
|
||||
'/': (context) => HomeScreen(),
|
||||
'/details': (context) => DetailScreen(),
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class HomeScreen extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: Center(
|
||||
child: FlatButton(
|
||||
child: Text('View Details'),
|
||||
onPressed: () {
|
||||
Navigator.pushNamed(
|
||||
context,
|
||||
'/details',
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class DetailScreen extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: Center(
|
||||
child: FlatButton(
|
||||
child: Text('Pop!'),
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
98
navigation_and_routing/lib/nav_1/on_generate_route.dart
Normal file
98
navigation_and_routing/lib/nav_1/on_generate_route.dart
Normal file
@@ -0,0 +1,98 @@
|
||||
// Copyright 2020, the Flutter project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
/// Shows how to handle arbitrary named routes using the `onGenerateRoute`
|
||||
/// callback defined in the `MaterialApp` constructor.
|
||||
library on_generate_route;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() {
|
||||
runApp(Nav2App());
|
||||
}
|
||||
|
||||
class Nav2App extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
onGenerateRoute: (settings) {
|
||||
// Handle '/'
|
||||
if (settings.name == '/') {
|
||||
return MaterialPageRoute(builder: (context) => HomeScreen());
|
||||
}
|
||||
|
||||
// Handle '/details/:id'
|
||||
var uri = Uri.parse(settings.name);
|
||||
if (uri.pathSegments.length == 2 &&
|
||||
uri.pathSegments.first == 'details') {
|
||||
var id = uri.pathSegments[1];
|
||||
return MaterialPageRoute(builder: (context) => DetailScreen(id: id));
|
||||
}
|
||||
|
||||
return MaterialPageRoute(builder: (context) => UnknownScreen());
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class HomeScreen extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: Center(
|
||||
child: FlatButton(
|
||||
child: Text('View Details'),
|
||||
onPressed: () {
|
||||
Navigator.pushNamed(
|
||||
context,
|
||||
'/details/1',
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class DetailScreen extends StatelessWidget {
|
||||
String id;
|
||||
|
||||
DetailScreen({
|
||||
this.id,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text('Viewing details for item $id'),
|
||||
FlatButton(
|
||||
child: Text('Pop!'),
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class UnknownScreen extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: Center(
|
||||
child: Text('404!'),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
142
navigation_and_routing/lib/nav_2/pages.dart
Normal file
142
navigation_and_routing/lib/nav_2/pages.dart
Normal file
@@ -0,0 +1,142 @@
|
||||
// Copyright 2020, the Flutter project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
/// Shows how to define a list of [Page] objects on Navigator declaratively.
|
||||
library nav2_pages;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() {
|
||||
runApp(BooksApp());
|
||||
}
|
||||
|
||||
class Book {
|
||||
final String title;
|
||||
final String author;
|
||||
|
||||
Book(this.title, this.author);
|
||||
}
|
||||
|
||||
class BooksApp extends StatefulWidget {
|
||||
@override
|
||||
State<StatefulWidget> createState() => _BooksAppState();
|
||||
}
|
||||
|
||||
class _BooksAppState extends State<BooksApp> {
|
||||
Book _selectedBook;
|
||||
|
||||
List<Book> books = [
|
||||
Book('Stranger in a Strange Land', 'Robert A. Heinlein'),
|
||||
Book('Foundation', 'Isaac Asimov'),
|
||||
Book('Fahrenheit 451', 'Ray Bradbury'),
|
||||
];
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'Books App',
|
||||
home: Navigator(
|
||||
pages: [
|
||||
MaterialPage(
|
||||
key: ValueKey('BooksListPage'),
|
||||
child: BooksListScreen(
|
||||
books: books,
|
||||
onTapped: _handleBookTapped,
|
||||
),
|
||||
),
|
||||
if (_selectedBook != null) BookDetailsPage(book: _selectedBook)
|
||||
],
|
||||
onPopPage: (route, result) {
|
||||
if (!route.didPop(result)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Update the list of pages by setting _selectedBook to null
|
||||
setState(() {
|
||||
_selectedBook = null;
|
||||
});
|
||||
|
||||
return true;
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _handleBookTapped(Book book) {
|
||||
setState(() {
|
||||
_selectedBook = book;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class BookDetailsPage extends Page {
|
||||
final Book book;
|
||||
|
||||
BookDetailsPage({
|
||||
this.book,
|
||||
}) : super(key: ValueKey(book));
|
||||
|
||||
Route createRoute(BuildContext context) {
|
||||
return MaterialPageRoute(
|
||||
settings: this,
|
||||
builder: (BuildContext context) {
|
||||
return BookDetailsScreen(book: book);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class BooksListScreen extends StatelessWidget {
|
||||
final List<Book> books;
|
||||
final ValueChanged<Book> onTapped;
|
||||
|
||||
BooksListScreen({
|
||||
@required this.books,
|
||||
@required this.onTapped,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: ListView(
|
||||
children: [
|
||||
for (var book in books)
|
||||
ListTile(
|
||||
title: Text(book.title),
|
||||
subtitle: Text(book.author),
|
||||
onTap: () => onTapped(book),
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class BookDetailsScreen extends StatelessWidget {
|
||||
final Book book;
|
||||
|
||||
BookDetailsScreen({
|
||||
@required this.book,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (book != null) ...[
|
||||
Text(book.title, style: Theme.of(context).textTheme.headline6),
|
||||
Text(book.author, style: Theme.of(context).textTheme.subtitle1),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
263
navigation_and_routing/lib/nav_2/router.dart
Normal file
263
navigation_and_routing/lib/nav_2/router.dart
Normal file
@@ -0,0 +1,263 @@
|
||||
// Copyright 2020, the Flutter project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
/// Full sample that shows a custom RouteInformationParser and RouterDelegate
|
||||
/// parsing named routes and declaratively building the stack of pages for the
|
||||
/// [Navigator].
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() {
|
||||
runApp(BooksApp());
|
||||
}
|
||||
|
||||
class Book {
|
||||
final String title;
|
||||
final String author;
|
||||
|
||||
Book(this.title, this.author);
|
||||
}
|
||||
|
||||
class BooksApp extends StatefulWidget {
|
||||
@override
|
||||
State<StatefulWidget> createState() => _BooksAppState();
|
||||
}
|
||||
|
||||
class _BooksAppState extends State<BooksApp> {
|
||||
BookRouterDelegate _routerDelegate = BookRouterDelegate();
|
||||
BookRouteInformationParser _routeInformationParser =
|
||||
BookRouteInformationParser();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp.router(
|
||||
title: 'Books App',
|
||||
routerDelegate: _routerDelegate,
|
||||
routeInformationParser: _routeInformationParser,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class BookRouteInformationParser extends RouteInformationParser<BookRoutePath> {
|
||||
@override
|
||||
Future<BookRoutePath> parseRouteInformation(
|
||||
RouteInformation routeInformation) async {
|
||||
final uri = Uri.parse(routeInformation.location);
|
||||
// Handle '/'
|
||||
if (uri.pathSegments.length == 0) {
|
||||
return BookRoutePath.home();
|
||||
}
|
||||
|
||||
// Handle '/book/:id'
|
||||
if (uri.pathSegments.length == 2) {
|
||||
if (uri.pathSegments[0] != 'book') return BookRoutePath.unknown();
|
||||
var remaining = uri.pathSegments[1];
|
||||
var id = int.tryParse(remaining);
|
||||
if (id == null) return BookRoutePath.unknown();
|
||||
return BookRoutePath.details(id);
|
||||
}
|
||||
|
||||
// Handle unknown routes
|
||||
return BookRoutePath.unknown();
|
||||
}
|
||||
|
||||
@override
|
||||
RouteInformation restoreRouteInformation(BookRoutePath path) {
|
||||
if (path.isUnknown) {
|
||||
return RouteInformation(location: '/404');
|
||||
}
|
||||
if (path.isHomePage) {
|
||||
return RouteInformation(location: '/');
|
||||
}
|
||||
if (path.isDetailsPage) {
|
||||
return RouteInformation(location: '/book/${path.id}');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
class BookRouterDelegate extends RouterDelegate<BookRoutePath>
|
||||
with ChangeNotifier, PopNavigatorRouterDelegateMixin<BookRoutePath> {
|
||||
final GlobalKey<NavigatorState> navigatorKey;
|
||||
|
||||
Book _selectedBook;
|
||||
bool show404 = false;
|
||||
|
||||
List<Book> books = [
|
||||
Book('Stranger in a Strange Land', 'Robert A. Heinlein'),
|
||||
Book('Foundation', 'Isaac Asimov'),
|
||||
Book('Fahrenheit 451', 'Ray Bradbury'),
|
||||
];
|
||||
|
||||
BookRouterDelegate() : navigatorKey = GlobalKey<NavigatorState>();
|
||||
|
||||
BookRoutePath get currentConfiguration {
|
||||
if (show404) {
|
||||
return BookRoutePath.unknown();
|
||||
}
|
||||
return _selectedBook == null
|
||||
? BookRoutePath.home()
|
||||
: BookRoutePath.details(books.indexOf(_selectedBook));
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Navigator(
|
||||
key: navigatorKey,
|
||||
pages: [
|
||||
MaterialPage(
|
||||
key: ValueKey('BooksListPage'),
|
||||
child: BooksListScreen(
|
||||
books: books,
|
||||
onTapped: _handleBookTapped,
|
||||
),
|
||||
),
|
||||
if (show404)
|
||||
MaterialPage(key: ValueKey('UnknownPage'), child: UnknownScreen())
|
||||
else if (_selectedBook != null)
|
||||
BookDetailsPage(book: _selectedBook)
|
||||
],
|
||||
onPopPage: (route, result) {
|
||||
if (!route.didPop(result)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Update the list of pages by setting _selectedBook to null
|
||||
_selectedBook = null;
|
||||
show404 = false;
|
||||
notifyListeners();
|
||||
|
||||
return true;
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> setNewRoutePath(BookRoutePath path) async {
|
||||
if (path.isUnknown) {
|
||||
_selectedBook = null;
|
||||
show404 = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (path.isDetailsPage) {
|
||||
if (path.id < 0 || path.id > books.length - 1) {
|
||||
show404 = true;
|
||||
return;
|
||||
}
|
||||
|
||||
_selectedBook = books[path.id];
|
||||
} else {
|
||||
_selectedBook = null;
|
||||
}
|
||||
|
||||
show404 = false;
|
||||
}
|
||||
|
||||
void _handleBookTapped(Book book) {
|
||||
_selectedBook = book;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
class BookDetailsPage extends Page {
|
||||
final Book book;
|
||||
|
||||
BookDetailsPage({
|
||||
this.book,
|
||||
}) : super(key: ValueKey(book));
|
||||
|
||||
Route createRoute(BuildContext context) {
|
||||
return MaterialPageRoute(
|
||||
settings: this,
|
||||
builder: (BuildContext context) {
|
||||
return BookDetailsScreen(book: book);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class BookRoutePath {
|
||||
final int id;
|
||||
final bool isUnknown;
|
||||
|
||||
BookRoutePath.home()
|
||||
: id = null,
|
||||
isUnknown = false;
|
||||
|
||||
BookRoutePath.details(this.id) : isUnknown = false;
|
||||
|
||||
BookRoutePath.unknown()
|
||||
: id = null,
|
||||
isUnknown = true;
|
||||
|
||||
bool get isHomePage => id == null;
|
||||
|
||||
bool get isDetailsPage => id != null;
|
||||
}
|
||||
|
||||
class BooksListScreen extends StatelessWidget {
|
||||
final List<Book> books;
|
||||
final ValueChanged<Book> onTapped;
|
||||
|
||||
BooksListScreen({
|
||||
@required this.books,
|
||||
@required this.onTapped,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: ListView(
|
||||
children: [
|
||||
for (var book in books)
|
||||
ListTile(
|
||||
title: Text(book.title),
|
||||
subtitle: Text(book.author),
|
||||
onTap: () => onTapped(book),
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class BookDetailsScreen extends StatelessWidget {
|
||||
final Book book;
|
||||
|
||||
BookDetailsScreen({
|
||||
@required this.book,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (book != null) ...[
|
||||
Text(book.title, style: Theme.of(context).textTheme.headline6),
|
||||
Text(book.author, style: Theme.of(context).textTheme.subtitle1),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class UnknownScreen extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: Center(
|
||||
child: Text('404!'),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
406
navigation_and_routing/lib/nav_2_advanced/nested_router.dart
Normal file
406
navigation_and_routing/lib/nav_2_advanced/nested_router.dart
Normal file
@@ -0,0 +1,406 @@
|
||||
// Copyright 2020, the Flutter project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
/// Shows two [RouterDelegate], one nested within the other. A
|
||||
/// [BottomNavigationBar] can be used to select the route of the outer
|
||||
/// RouterDelegate, and additional routes can be pushed onto the inner
|
||||
/// RouterDelegate / Navigator.
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() {
|
||||
runApp(NestedRouterDemo());
|
||||
}
|
||||
|
||||
class Book {
|
||||
final String title;
|
||||
final String author;
|
||||
|
||||
Book(this.title, this.author);
|
||||
}
|
||||
|
||||
class NestedRouterDemo extends StatefulWidget {
|
||||
@override
|
||||
_NestedRouterDemoState createState() => _NestedRouterDemoState();
|
||||
}
|
||||
|
||||
class _NestedRouterDemoState extends State<NestedRouterDemo> {
|
||||
BookRouterDelegate _routerDelegate = BookRouterDelegate();
|
||||
BookRouteInformationParser _routeInformationParser =
|
||||
BookRouteInformationParser();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp.router(
|
||||
title: 'Books App',
|
||||
routerDelegate: _routerDelegate,
|
||||
routeInformationParser: _routeInformationParser,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class BooksAppState extends ChangeNotifier {
|
||||
int _selectedIndex;
|
||||
|
||||
Book _selectedBook;
|
||||
|
||||
final List<Book> books = [
|
||||
Book('Stranger in a Strange Land', 'Robert A. Heinlein'),
|
||||
Book('Foundation', 'Isaac Asimov'),
|
||||
Book('Fahrenheit 451', 'Ray Bradbury'),
|
||||
];
|
||||
|
||||
BooksAppState() : _selectedIndex = 0;
|
||||
|
||||
int get selectedIndex => _selectedIndex;
|
||||
|
||||
set selectedIndex(int idx) {
|
||||
_selectedIndex = idx;
|
||||
if (_selectedIndex == 1) {
|
||||
// Remove this line if you want to keep the selected book when navigating
|
||||
// between "settings" and "home" which book was selected when Settings is
|
||||
// tapped.
|
||||
selectedBook = null;
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Book get selectedBook => _selectedBook;
|
||||
|
||||
set selectedBook(Book book) {
|
||||
_selectedBook = book;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
int getSelectedBookById() {
|
||||
if (!books.contains(_selectedBook)) return 0;
|
||||
return books.indexOf(_selectedBook);
|
||||
}
|
||||
|
||||
void setSelectedBookById(int id) {
|
||||
if (id < 0 || id > books.length - 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
_selectedBook = books[id];
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
class BookRouteInformationParser extends RouteInformationParser<BookRoutePath> {
|
||||
@override
|
||||
Future<BookRoutePath> parseRouteInformation(
|
||||
RouteInformation routeInformation) async {
|
||||
final uri = Uri.parse(routeInformation.location);
|
||||
|
||||
if (uri.pathSegments.isNotEmpty && uri.pathSegments.first == 'settings') {
|
||||
return BooksSettingsPath();
|
||||
} else {
|
||||
if (uri.pathSegments.length >= 2) {
|
||||
if (uri.pathSegments[0] == 'book') {
|
||||
return BooksDetailsPath(int.tryParse(uri.pathSegments[1]));
|
||||
}
|
||||
}
|
||||
return BooksListPath();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
RouteInformation restoreRouteInformation(BookRoutePath configuration) {
|
||||
if (configuration is BooksListPath) {
|
||||
return RouteInformation(location: '/home');
|
||||
}
|
||||
if (configuration is BooksSettingsPath) {
|
||||
return RouteInformation(location: '/settings');
|
||||
}
|
||||
if (configuration is BooksDetailsPath) {
|
||||
return RouteInformation(location: '/book/${configuration.id}');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
class BookRouterDelegate extends RouterDelegate<BookRoutePath>
|
||||
with ChangeNotifier, PopNavigatorRouterDelegateMixin<BookRoutePath> {
|
||||
final GlobalKey<NavigatorState> navigatorKey;
|
||||
|
||||
BooksAppState appState = BooksAppState();
|
||||
|
||||
BookRouterDelegate() : navigatorKey = GlobalKey<NavigatorState>() {
|
||||
appState.addListener(notifyListeners);
|
||||
}
|
||||
|
||||
BookRoutePath get currentConfiguration {
|
||||
if (appState.selectedIndex == 1) {
|
||||
return BooksSettingsPath();
|
||||
} else {
|
||||
if (appState.selectedBook == null) {
|
||||
return BooksListPath();
|
||||
} else {
|
||||
return BooksDetailsPath(appState.getSelectedBookById());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Navigator(
|
||||
key: navigatorKey,
|
||||
pages: [
|
||||
MaterialPage(
|
||||
child: AppShell(appState: appState),
|
||||
),
|
||||
],
|
||||
onPopPage: (route, result) {
|
||||
if (!route.didPop(result)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (appState.selectedBook != null) {
|
||||
appState.selectedBook = null;
|
||||
}
|
||||
notifyListeners();
|
||||
return true;
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> setNewRoutePath(BookRoutePath path) async {
|
||||
if (path is BooksListPath) {
|
||||
appState.selectedIndex = 0;
|
||||
appState.selectedBook = null;
|
||||
} else if (path is BooksSettingsPath) {
|
||||
appState.selectedIndex = 1;
|
||||
} else if (path is BooksDetailsPath) {
|
||||
appState.setSelectedBookById(path.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Routes
|
||||
abstract class BookRoutePath {}
|
||||
|
||||
class BooksListPath extends BookRoutePath {}
|
||||
|
||||
class BooksSettingsPath extends BookRoutePath {}
|
||||
|
||||
class BooksDetailsPath extends BookRoutePath {
|
||||
final int id;
|
||||
|
||||
BooksDetailsPath(this.id);
|
||||
}
|
||||
|
||||
// Widget that contains the AdaptiveNavigationScaffold
|
||||
class AppShell extends StatefulWidget {
|
||||
final BooksAppState appState;
|
||||
|
||||
AppShell({
|
||||
@required this.appState,
|
||||
});
|
||||
|
||||
@override
|
||||
_AppShellState createState() => _AppShellState();
|
||||
}
|
||||
|
||||
class _AppShellState extends State<AppShell> {
|
||||
InnerRouterDelegate _routerDelegate;
|
||||
ChildBackButtonDispatcher _backButtonDispatcher;
|
||||
|
||||
void initState() {
|
||||
super.initState();
|
||||
_routerDelegate = InnerRouterDelegate(widget.appState);
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant AppShell oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
_routerDelegate.appState = widget.appState;
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
// Defer back button dispatching to the child router
|
||||
_backButtonDispatcher = Router.of(context)
|
||||
.backButtonDispatcher
|
||||
.createChildBackButtonDispatcher();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var appState = widget.appState;
|
||||
|
||||
// Claim priority, If there are parallel sub router, you will need
|
||||
// to pick which one should take priority;
|
||||
_backButtonDispatcher.takePriority();
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: Router(
|
||||
routerDelegate: _routerDelegate,
|
||||
backButtonDispatcher: _backButtonDispatcher,
|
||||
),
|
||||
bottomNavigationBar: BottomNavigationBar(
|
||||
items: [
|
||||
BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
|
||||
BottomNavigationBarItem(
|
||||
icon: Icon(Icons.settings), label: 'Settings'),
|
||||
],
|
||||
currentIndex: appState.selectedIndex,
|
||||
onTap: (newIndex) {
|
||||
appState.selectedIndex = newIndex;
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class InnerRouterDelegate extends RouterDelegate<BookRoutePath>
|
||||
with ChangeNotifier, PopNavigatorRouterDelegateMixin<BookRoutePath> {
|
||||
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
|
||||
BooksAppState get appState => _appState;
|
||||
BooksAppState _appState;
|
||||
set appState(BooksAppState value) {
|
||||
if (value == _appState) {
|
||||
return;
|
||||
}
|
||||
_appState = value;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
InnerRouterDelegate(this._appState);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Navigator(
|
||||
key: navigatorKey,
|
||||
pages: [
|
||||
if (appState.selectedIndex == 0) ...[
|
||||
FadeAnimationPage(
|
||||
child: BooksListScreen(
|
||||
books: appState.books,
|
||||
onTapped: _handleBookTapped,
|
||||
),
|
||||
key: ValueKey('BooksListPage'),
|
||||
),
|
||||
if (appState.selectedBook != null)
|
||||
MaterialPage(
|
||||
key: ValueKey(appState.selectedBook),
|
||||
child: BookDetailsScreen(book: appState.selectedBook),
|
||||
),
|
||||
] else
|
||||
FadeAnimationPage(
|
||||
child: SettingsScreen(),
|
||||
key: ValueKey('SettingsPage'),
|
||||
),
|
||||
],
|
||||
onPopPage: (route, result) {
|
||||
appState.selectedBook = null;
|
||||
notifyListeners();
|
||||
return route.didPop(result);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> setNewRoutePath(BookRoutePath path) async {
|
||||
// This is not required for inner router delegate because it does not
|
||||
// parse route
|
||||
assert(false);
|
||||
}
|
||||
|
||||
void _handleBookTapped(Book book) {
|
||||
appState.selectedBook = book;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
class FadeAnimationPage extends Page {
|
||||
final Widget child;
|
||||
|
||||
FadeAnimationPage({Key key, this.child}) : super(key: key);
|
||||
|
||||
Route createRoute(BuildContext context) {
|
||||
return PageRouteBuilder(
|
||||
settings: this,
|
||||
pageBuilder: (context, animation, animation2) {
|
||||
var curveTween = CurveTween(curve: Curves.easeIn);
|
||||
return FadeTransition(
|
||||
opacity: animation.drive(curveTween),
|
||||
child: child,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Screens
|
||||
class BooksListScreen extends StatelessWidget {
|
||||
final List<Book> books;
|
||||
final ValueChanged<Book> onTapped;
|
||||
|
||||
BooksListScreen({
|
||||
@required this.books,
|
||||
@required this.onTapped,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: ListView(
|
||||
children: [
|
||||
for (var book in books)
|
||||
ListTile(
|
||||
title: Text(book.title),
|
||||
subtitle: Text(book.author),
|
||||
onTap: () => onTapped(book),
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class BookDetailsScreen extends StatelessWidget {
|
||||
final Book book;
|
||||
|
||||
BookDetailsScreen({
|
||||
@required this.book,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
FlatButton(
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
child: Text('Back'),
|
||||
),
|
||||
if (book != null) ...[
|
||||
Text(book.title, style: Theme.of(context).textTheme.headline6),
|
||||
Text(book.author, style: Theme.of(context).textTheme.subtitle1),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class SettingsScreen extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: Center(
|
||||
child: Text('Settings screen'),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
// Copyright 2020, the Flutter project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
/// Shows how a custom TransitionDelegate can be used to customized when
|
||||
/// transition animations are shown. (For example, [when two routes are popped
|
||||
/// off the stack](https://github.com/flutter/flutter/issues/12146), however the
|
||||
/// default TransitionDelegate will handle this if you are using Navigator 2.0)
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() {
|
||||
runApp(BooksApp());
|
||||
}
|
||||
|
||||
class Book {
|
||||
final String title;
|
||||
final String author;
|
||||
|
||||
Book(this.title, this.author);
|
||||
}
|
||||
|
||||
class BooksApp extends StatefulWidget {
|
||||
@override
|
||||
State<StatefulWidget> createState() => _BooksAppState();
|
||||
}
|
||||
|
||||
class _BooksAppState extends State<BooksApp> {
|
||||
BookRouterDelegate _routerDelegate = BookRouterDelegate();
|
||||
BookRouteInformationParser _routeInformationParser =
|
||||
BookRouteInformationParser();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp.router(
|
||||
title: 'Books App',
|
||||
routerDelegate: _routerDelegate,
|
||||
routeInformationParser: _routeInformationParser,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class BookRouteInformationParser extends RouteInformationParser<BookRoutePath> {
|
||||
@override
|
||||
Future<BookRoutePath> parseRouteInformation(
|
||||
RouteInformation routeInformation) async {
|
||||
final uri = Uri.parse(routeInformation.location);
|
||||
|
||||
if (uri.pathSegments.length >= 2) {
|
||||
var remaining = uri.pathSegments[1];
|
||||
return BookRoutePath.details(int.tryParse(remaining));
|
||||
} else {
|
||||
return BookRoutePath.home();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
RouteInformation restoreRouteInformation(BookRoutePath path) {
|
||||
if (path.isHomePage) {
|
||||
return RouteInformation(location: '/');
|
||||
}
|
||||
if (path.isDetailsPage) {
|
||||
return RouteInformation(location: '/book/${path.id}');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
class BookRouterDelegate extends RouterDelegate<BookRoutePath>
|
||||
with ChangeNotifier, PopNavigatorRouterDelegateMixin<BookRoutePath> {
|
||||
final GlobalKey<NavigatorState> navigatorKey;
|
||||
|
||||
Book _selectedBook;
|
||||
|
||||
List<Book> books = [
|
||||
Book('Stranger in a Strange Land', 'Robert A. Heinlein'),
|
||||
Book('Foundation', 'Isaac Asimov'),
|
||||
Book('Fahrenheit 451', 'Ray Bradbury'),
|
||||
];
|
||||
|
||||
BookRouterDelegate() : navigatorKey = GlobalKey<NavigatorState>();
|
||||
|
||||
BookRoutePath get currentConfiguration => _selectedBook == null
|
||||
? BookRoutePath.home()
|
||||
: BookRoutePath.details(books.indexOf(_selectedBook));
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Navigator(
|
||||
key: navigatorKey,
|
||||
transitionDelegate: NoAnimationTransitionDelegate(),
|
||||
pages: [
|
||||
MaterialPage(
|
||||
key: ValueKey('BooksListPage'),
|
||||
child: BooksListScreen(
|
||||
books: books,
|
||||
onTapped: _handleBookTapped,
|
||||
),
|
||||
),
|
||||
if (_selectedBook != null) BookDetailsPage(book: _selectedBook)
|
||||
],
|
||||
onPopPage: (route, result) {
|
||||
if (!route.didPop(result)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Update the list of pages by setting _selectedBook to null
|
||||
_selectedBook = null;
|
||||
notifyListeners();
|
||||
|
||||
return true;
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> setNewRoutePath(BookRoutePath path) async {
|
||||
if (path.isDetailsPage) {
|
||||
_selectedBook = books[path.id];
|
||||
}
|
||||
}
|
||||
|
||||
void _handleBookTapped(Book book) {
|
||||
_selectedBook = book;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
class BookDetailsPage extends Page {
|
||||
final Book book;
|
||||
|
||||
BookDetailsPage({
|
||||
this.book,
|
||||
}) : super(key: ValueKey(book));
|
||||
|
||||
Route createRoute(BuildContext context) {
|
||||
return MaterialPageRoute(
|
||||
settings: this,
|
||||
builder: (BuildContext context) {
|
||||
return BookDetailsScreen(book: book);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class BookRoutePath {
|
||||
final int id;
|
||||
|
||||
BookRoutePath.home() : id = null;
|
||||
|
||||
BookRoutePath.details(this.id);
|
||||
|
||||
bool get isHomePage => id == null;
|
||||
|
||||
bool get isDetailsPage => id != null;
|
||||
}
|
||||
|
||||
class BooksListScreen extends StatelessWidget {
|
||||
final List<Book> books;
|
||||
final ValueChanged<Book> onTapped;
|
||||
|
||||
BooksListScreen({
|
||||
@required this.books,
|
||||
@required this.onTapped,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: ListView(
|
||||
children: [
|
||||
for (var book in books)
|
||||
ListTile(
|
||||
title: Text(book.title),
|
||||
subtitle: Text(book.author),
|
||||
onTap: () => onTapped(book),
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class BookDetailsScreen extends StatelessWidget {
|
||||
final Book book;
|
||||
|
||||
BookDetailsScreen({
|
||||
@required this.book,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (book != null) ...[
|
||||
Text(book.title, style: Theme.of(context).textTheme.headline6),
|
||||
Text(book.author, style: Theme.of(context).textTheme.subtitle1),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class NoAnimationTransitionDelegate extends TransitionDelegate<void> {
|
||||
@override
|
||||
Iterable<RouteTransitionRecord> resolve({
|
||||
List<RouteTransitionRecord> newPageRouteHistory,
|
||||
Map<RouteTransitionRecord, RouteTransitionRecord>
|
||||
locationToExitingPageRoute,
|
||||
Map<RouteTransitionRecord, List<RouteTransitionRecord>>
|
||||
pageRouteToPagelessRoutes,
|
||||
}) {
|
||||
final results = <RouteTransitionRecord>[];
|
||||
|
||||
for (final pageRoute in newPageRouteHistory) {
|
||||
if (pageRoute.isWaitingForEnteringDecision) {
|
||||
pageRoute.markForAdd();
|
||||
}
|
||||
results.add(pageRoute);
|
||||
}
|
||||
|
||||
for (final exitingPageRoute in locationToExitingPageRoute.values) {
|
||||
if (exitingPageRoute.isWaitingForExitingDecision) {
|
||||
exitingPageRoute.markForRemove();
|
||||
final pagelessRoutes = pageRouteToPagelessRoutes[exitingPageRoute];
|
||||
if (pagelessRoutes != null) {
|
||||
for (final pagelessRoute in pagelessRoutes) {
|
||||
pagelessRoute.markForRemove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
results.add(exitingPageRoute);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user