mirror of https://github.com/flutter/samples.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
277 lines
6.7 KiB
277 lines
6.7 KiB
4 years ago
|
// 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].
|
||
4 years ago
|
import 'package:flutter/foundation.dart';
|
||
4 years ago
|
import 'package:flutter/material.dart';
|
||
|
|
||
|
void main() {
|
||
4 years ago
|
runApp(const BooksApp());
|
||
4 years ago
|
}
|
||
|
|
||
|
class Book {
|
||
|
final String title;
|
||
|
final String author;
|
||
|
|
||
|
Book(this.title, this.author);
|
||
|
}
|
||
|
|
||
|
class BooksApp extends StatefulWidget {
|
||
4 years ago
|
const BooksApp({Key? key}) : super(key: key);
|
||
|
|
||
4 years ago
|
@override
|
||
|
State<StatefulWidget> createState() => _BooksAppState();
|
||
|
}
|
||
|
|
||
|
class _BooksAppState extends State<BooksApp> {
|
||
4 years ago
|
final BookRouterDelegate _routerDelegate = BookRouterDelegate();
|
||
|
final BookRouteInformationParser _routeInformationParser =
|
||
4 years ago
|
BookRouteInformationParser();
|
||
4 years ago
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
return MaterialApp.router(
|
||
|
title: 'Books App',
|
||
|
routerDelegate: _routerDelegate,
|
||
|
routeInformationParser: _routeInformationParser,
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class BookRouteInformationParser extends RouteInformationParser<BookRoutePath> {
|
||
|
@override
|
||
|
Future<BookRoutePath> parseRouteInformation(
|
||
4 years ago
|
RouteInformation routeInformation,
|
||
|
) {
|
||
4 years ago
|
final uri = Uri.parse(routeInformation.location ?? '');
|
||
4 years ago
|
// Handle '/'
|
||
4 years ago
|
if (uri.pathSegments.isEmpty) {
|
||
4 years ago
|
return SynchronousFuture(BookRoutePath.home());
|
||
4 years ago
|
}
|
||
|
|
||
|
// Handle '/book/:id'
|
||
4 years ago
|
if (uri.pathSegments.length == 2 && uri.pathSegments[0] == 'book') {
|
||
4 years ago
|
var remaining = uri.pathSegments[1];
|
||
|
var id = int.tryParse(remaining);
|
||
4 years ago
|
if (id != null) {
|
||
|
return SynchronousFuture(BookRoutePath.details(id));
|
||
|
}
|
||
4 years ago
|
}
|
||
|
|
||
|
// Handle unknown routes
|
||
4 years ago
|
return SynchronousFuture(BookRoutePath.unknown());
|
||
4 years ago
|
}
|
||
|
|
||
|
@override
|
||
4 years ago
|
RouteInformation? restoreRouteInformation(BookRoutePath configuration) {
|
||
|
if (configuration.isUnknown) {
|
||
|
return const RouteInformation(location: '/404');
|
||
4 years ago
|
}
|
||
4 years ago
|
if (configuration.isHomePage) {
|
||
|
return const RouteInformation(location: '/');
|
||
4 years ago
|
}
|
||
4 years ago
|
if (configuration.isDetailsPage) {
|
||
|
return RouteInformation(location: '/book/${configuration.id}');
|
||
4 years ago
|
}
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class BookRouterDelegate extends RouterDelegate<BookRoutePath>
|
||
|
with ChangeNotifier, PopNavigatorRouterDelegateMixin<BookRoutePath> {
|
||
4 years ago
|
@override
|
||
4 years ago
|
final GlobalKey<NavigatorState> navigatorKey;
|
||
|
|
||
4 years ago
|
Book? _selectedBook;
|
||
4 years ago
|
bool show404 = false;
|
||
|
|
||
4 years ago
|
final List<Book> books = [
|
||
|
Book('Left Hand of Darkness', 'Ursula K. Le Guin'),
|
||
|
Book('Too Like the Lightning', 'Ada Palmer'),
|
||
|
Book('Kindred', 'Octavia E. Butler'),
|
||
4 years ago
|
];
|
||
|
|
||
|
BookRouterDelegate() : navigatorKey = GlobalKey<NavigatorState>();
|
||
|
|
||
4 years ago
|
@override
|
||
4 years ago
|
BookRoutePath get currentConfiguration {
|
||
|
if (show404) {
|
||
|
return BookRoutePath.unknown();
|
||
|
}
|
||
|
return _selectedBook == null
|
||
|
? BookRoutePath.home()
|
||
4 years ago
|
: BookRoutePath.details(books.indexOf(_selectedBook!));
|
||
4 years ago
|
}
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
return Navigator(
|
||
|
key: navigatorKey,
|
||
|
pages: [
|
||
4 years ago
|
MaterialPage<void>(
|
||
|
key: const ValueKey('BooksListPage'),
|
||
4 years ago
|
child: BooksListScreen(
|
||
|
books: books,
|
||
|
onTapped: _handleBookTapped,
|
||
|
),
|
||
|
),
|
||
|
if (show404)
|
||
4 years ago
|
const MaterialPage<void>(
|
||
|
key: ValueKey('UnknownPage'),
|
||
|
child: UnknownScreen(),
|
||
|
)
|
||
4 years ago
|
else if (_selectedBook != null)
|
||
4 years ago
|
BookDetailsPage(book: _selectedBook!)
|
||
4 years ago
|
],
|
||
4 years ago
|
onPopPage: (route, dynamic result) {
|
||
4 years ago
|
if (!route.didPop(result)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
// Update the list of pages by setting _selectedBook to null
|
||
|
_selectedBook = null;
|
||
|
show404 = false;
|
||
|
notifyListeners();
|
||
|
|
||
|
return true;
|
||
|
},
|
||
|
);
|
||
|
}
|
||
|
|
||
|
@override
|
||
4 years ago
|
Future<void> setNewRoutePath(BookRoutePath configuration) {
|
||
|
if (configuration.isUnknown) {
|
||
4 years ago
|
_selectedBook = null;
|
||
|
show404 = true;
|
||
4 years ago
|
return SynchronousFuture<void>(null);
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
if (configuration.isDetailsPage) {
|
||
|
if (configuration.id! < 0 || configuration.id! > books.length - 1) {
|
||
4 years ago
|
show404 = true;
|
||
4 years ago
|
return SynchronousFuture<void>(null);
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
_selectedBook = books[configuration.id!];
|
||
4 years ago
|
} else {
|
||
|
_selectedBook = null;
|
||
|
}
|
||
|
|
||
|
show404 = false;
|
||
4 years ago
|
return SynchronousFuture<void>(null);
|
||
4 years ago
|
}
|
||
|
|
||
|
void _handleBookTapped(Book book) {
|
||
|
_selectedBook = book;
|
||
|
notifyListeners();
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
class BookDetailsPage extends Page<void> {
|
||
4 years ago
|
final Book book;
|
||
|
|
||
|
BookDetailsPage({
|
||
4 years ago
|
required this.book,
|
||
4 years ago
|
}) : super(key: ValueKey(book));
|
||
|
|
||
4 years ago
|
@override
|
||
4 years ago
|
Route createRoute(BuildContext context) {
|
||
4 years ago
|
return MaterialPageRoute<void>(
|
||
4 years ago
|
settings: this,
|
||
4 years ago
|
builder: (context) {
|
||
4 years ago
|
return BookDetailsScreen(book: book);
|
||
|
},
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class BookRoutePath {
|
||
4 years ago
|
final int? id;
|
||
4 years ago
|
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;
|
||
|
|
||
4 years ago
|
const BooksListScreen({
|
||
|
required this.books,
|
||
|
required this.onTapped,
|
||
|
Key? key,
|
||
|
}) : super(key: key);
|
||
4 years ago
|
|
||
|
@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;
|
||
|
|
||
4 years ago
|
const BookDetailsScreen({
|
||
|
required this.book,
|
||
|
Key? key,
|
||
|
}) : super(key: key);
|
||
4 years ago
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
return Scaffold(
|
||
|
appBar: AppBar(),
|
||
|
body: Padding(
|
||
|
padding: const EdgeInsets.all(8.0),
|
||
|
child: Column(
|
||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||
|
children: [
|
||
4 years ago
|
Text(book.title, style: Theme.of(context).textTheme.headline6),
|
||
|
Text(book.author, style: Theme.of(context).textTheme.subtitle1),
|
||
4 years ago
|
],
|
||
|
),
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class UnknownScreen extends StatelessWidget {
|
||
4 years ago
|
const UnknownScreen({Key? key}) : super(key: key);
|
||
|
|
||
4 years ago
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
return Scaffold(
|
||
|
appBar: AppBar(),
|
||
4 years ago
|
body: const Center(
|
||
4 years ago
|
child: Text('404!'),
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
}
|