mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 22:09:06 +00:00
Replace navigation_and_routing with a new sample (#832)
* move snippets into old_snippets directory * add new navigation_and_routing sample * add copyright headers * Apply #827 to old_snippets/ directory and upgrade them to null safety * Code review comments - Move Guard class into parser.dart - Move usage of guards from Delegate to RouteInformationParser - Rename delegate to SimpleRouterDelegate * clean up imports * refactor settings screen, fix bug * avoid conflicting paths /books/new and /books/1 - rename to book/1 * dispose fields in _BookstoreState class * remove /books path This was causing problems * add comment * Change BookstoreAuthScope and BookstoreAuthScope to InheritedNotifier * fix warnings * Make the initial route configurable, set to '/signin' * Enable deep linking https://flutter.dev/docs/development/ui/navigation/deep-linking * use path URL strategy on the web. * remove TODO, add comment
This commit is contained in:
147
navigation_and_routing/old_snippets/router/pages.dart
Normal file
147
navigation_and_routing/old_snippets/router/pages.dart
Normal file
@@ -0,0 +1,147 @@
|
||||
// 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(const BooksApp());
|
||||
}
|
||||
|
||||
class Book {
|
||||
final String title;
|
||||
final String author;
|
||||
|
||||
Book(this.title, this.author);
|
||||
}
|
||||
|
||||
class BooksApp extends StatefulWidget {
|
||||
const BooksApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _BooksAppState();
|
||||
}
|
||||
|
||||
class _BooksAppState extends State<BooksApp> {
|
||||
Book? _selectedBook;
|
||||
|
||||
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'),
|
||||
];
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'Books App',
|
||||
home: Navigator(
|
||||
pages: [
|
||||
MaterialPage<void>(
|
||||
key: const ValueKey('BooksListPage'),
|
||||
child: BooksListScreen(
|
||||
books: books,
|
||||
onTapped: _handleBookTapped,
|
||||
),
|
||||
),
|
||||
if (_selectedBook != null) BookDetailsPage(book: _selectedBook)
|
||||
],
|
||||
onPopPage: (route, dynamic 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<void> {
|
||||
final Book? book;
|
||||
|
||||
BookDetailsPage({
|
||||
this.book,
|
||||
}) : super(key: ValueKey(book));
|
||||
|
||||
@override
|
||||
Route createRoute(BuildContext context) {
|
||||
return MaterialPageRoute<void>(
|
||||
settings: this,
|
||||
builder: (context) {
|
||||
return BookDetailsScreen(book: book);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class BooksListScreen extends StatelessWidget {
|
||||
final List<Book> books;
|
||||
final ValueChanged<Book> onTapped;
|
||||
|
||||
const BooksListScreen({
|
||||
required this.books,
|
||||
required this.onTapped,
|
||||
Key? key,
|
||||
}) : super(key: key);
|
||||
|
||||
@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;
|
||||
|
||||
const BookDetailsScreen({
|
||||
@required this.book,
|
||||
Key? key,
|
||||
}) : super(key: key);
|
||||
|
||||
@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),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user