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.
143 lines
3.1 KiB
143 lines
3.1 KiB
// 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),
|
|
],
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|