mirror of
https://github.com/flutter/samples.git
synced 2025-11-09 06:18:49 +00:00
Move library to a top-level variable, since it never changes (#887)
This commit is contained in:
@@ -5,10 +5,8 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'auth.dart';
|
||||
import 'data.dart';
|
||||
import 'routing.dart';
|
||||
import 'screens/navigator.dart';
|
||||
import 'widgets/library_scope.dart';
|
||||
|
||||
class Bookstore extends StatefulWidget {
|
||||
const Bookstore({Key? key}) : super(key: key);
|
||||
@@ -24,28 +22,6 @@ class _BookstoreState extends State<Bookstore> {
|
||||
late final SimpleRouterDelegate _routerDelegate;
|
||||
late final TemplateRouteParser _routeParser;
|
||||
|
||||
final library = Library()
|
||||
..addBook(
|
||||
title: 'Left Hand of Darkness',
|
||||
authorName: 'Ursula K. Le Guin',
|
||||
isPopular: true,
|
||||
isNew: true)
|
||||
..addBook(
|
||||
title: 'Too Like the Lightning',
|
||||
authorName: 'Ada Palmer',
|
||||
isPopular: false,
|
||||
isNew: true)
|
||||
..addBook(
|
||||
title: 'Kindred',
|
||||
authorName: 'Octavia E. Butler',
|
||||
isPopular: true,
|
||||
isNew: false)
|
||||
..addBook(
|
||||
title: 'The Lathe of Heaven',
|
||||
authorName: 'Ursula K. Le Guin',
|
||||
isPopular: false,
|
||||
isNew: false);
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
/// Configure the parser with all of the app's allowed path templates.
|
||||
@@ -85,12 +61,9 @@ class _BookstoreState extends State<Bookstore> {
|
||||
notifier: _routeState,
|
||||
child: BookstoreAuthScope(
|
||||
notifier: _auth,
|
||||
child: LibraryScope(
|
||||
library: library,
|
||||
child: MaterialApp.router(
|
||||
routerDelegate: _routerDelegate,
|
||||
routeInformationParser: _routeParser,
|
||||
),
|
||||
child: MaterialApp.router(
|
||||
routerDelegate: _routerDelegate,
|
||||
routeInformationParser: _routeParser,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -7,7 +7,7 @@ import 'book.dart';
|
||||
class Author {
|
||||
final int id;
|
||||
final String name;
|
||||
final List<Book> books;
|
||||
final List<Book> books = <Book>[];
|
||||
|
||||
Author(this.id, this.name, this.books);
|
||||
Author(this.id, this.name);
|
||||
}
|
||||
|
||||
@@ -7,9 +7,9 @@ import 'author.dart';
|
||||
class Book {
|
||||
final int id;
|
||||
final String title;
|
||||
late final Author author;
|
||||
final Author author;
|
||||
final bool isPopular;
|
||||
final bool isNew;
|
||||
|
||||
Book(this.id, this.title, this.isPopular, this.isNew);
|
||||
Book(this.id, this.title, this.isPopular, this.isNew, this.author);
|
||||
}
|
||||
|
||||
@@ -2,11 +2,31 @@
|
||||
// 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.
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
|
||||
import 'author.dart';
|
||||
import 'book.dart';
|
||||
|
||||
final libraryInstance = Library()
|
||||
..addBook(
|
||||
title: 'Left Hand of Darkness',
|
||||
authorName: 'Ursula K. Le Guin',
|
||||
isPopular: true,
|
||||
isNew: true)
|
||||
..addBook(
|
||||
title: 'Too Like the Lightning',
|
||||
authorName: 'Ada Palmer',
|
||||
isPopular: false,
|
||||
isNew: true)
|
||||
..addBook(
|
||||
title: 'Kindred',
|
||||
authorName: 'Octavia E. Butler',
|
||||
isPopular: true,
|
||||
isNew: false)
|
||||
..addBook(
|
||||
title: 'The Lathe of Heaven',
|
||||
authorName: 'Ursula K. Le Guin',
|
||||
isPopular: false,
|
||||
isNew: false);
|
||||
|
||||
class Library {
|
||||
final List<Book> allBooks = [];
|
||||
final List<Author> allAuthors = [];
|
||||
@@ -17,18 +37,17 @@ class Library {
|
||||
required bool isPopular,
|
||||
required bool isNew,
|
||||
}) {
|
||||
var author =
|
||||
allAuthors.firstWhereOrNull((author) => author.name == authorName);
|
||||
var book = Book(allBooks.length, title, isPopular, isNew);
|
||||
var author = allAuthors.firstWhere(
|
||||
(author) => author.name == authorName,
|
||||
orElse: () {
|
||||
final value = Author(allAuthors.length, authorName);
|
||||
allAuthors.add(value);
|
||||
return value;
|
||||
},
|
||||
);
|
||||
var book = Book(allBooks.length, title, isPopular, isNew, author);
|
||||
|
||||
if (author == null) {
|
||||
author = Author(allAuthors.length, authorName, [book]);
|
||||
allAuthors.add(author);
|
||||
} else {
|
||||
author.books.add(book);
|
||||
}
|
||||
|
||||
book.author = author;
|
||||
author.books.add(book);
|
||||
allBooks.add(book);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,9 +4,9 @@
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../data/library.dart';
|
||||
import '../routing.dart';
|
||||
import '../widgets/author_list.dart';
|
||||
import '../widgets/library_scope.dart';
|
||||
|
||||
class AuthorsScreen extends StatelessWidget {
|
||||
final String title = 'Authors';
|
||||
@@ -19,7 +19,7 @@ class AuthorsScreen extends StatelessWidget {
|
||||
title: Text(title),
|
||||
),
|
||||
body: AuthorList(
|
||||
authors: LibraryScope.of(context).allAuthors,
|
||||
authors: libraryInstance.allAuthors,
|
||||
onTap: (author) {
|
||||
RouteStateScope.of(context).go('/author/${author.id}');
|
||||
},
|
||||
|
||||
@@ -7,7 +7,6 @@ import 'package:flutter/material.dart';
|
||||
import '../data.dart';
|
||||
import '../routing.dart';
|
||||
import '../widgets/book_list.dart';
|
||||
import '../widgets/library_scope.dart';
|
||||
|
||||
class BooksScreen extends StatefulWidget {
|
||||
const BooksScreen({
|
||||
@@ -50,48 +49,45 @@ class _BooksScreenState extends State<BooksScreen>
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final library = LibraryScope.of(context);
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Books'),
|
||||
bottom: TabBar(
|
||||
Widget build(BuildContext context) => Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Books'),
|
||||
bottom: TabBar(
|
||||
controller: _tabController,
|
||||
tabs: const [
|
||||
Tab(
|
||||
text: 'Popular',
|
||||
icon: Icon(Icons.people),
|
||||
),
|
||||
Tab(
|
||||
text: 'New',
|
||||
icon: Icon(Icons.new_releases),
|
||||
),
|
||||
Tab(
|
||||
text: 'All',
|
||||
icon: Icon(Icons.list),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
body: TabBarView(
|
||||
controller: _tabController,
|
||||
tabs: const [
|
||||
Tab(
|
||||
text: 'Popular',
|
||||
icon: Icon(Icons.people),
|
||||
children: [
|
||||
BookList(
|
||||
books: libraryInstance.popularBooks,
|
||||
onTap: _handleBookTapped,
|
||||
),
|
||||
Tab(
|
||||
text: 'New',
|
||||
icon: Icon(Icons.new_releases),
|
||||
BookList(
|
||||
books: libraryInstance.newBooks,
|
||||
onTap: _handleBookTapped,
|
||||
),
|
||||
Tab(
|
||||
text: 'All',
|
||||
icon: Icon(Icons.list),
|
||||
BookList(
|
||||
books: libraryInstance.allBooks,
|
||||
onTap: _handleBookTapped,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
body: TabBarView(
|
||||
controller: _tabController,
|
||||
children: [
|
||||
BookList(
|
||||
books: library.popularBooks,
|
||||
onTap: _handleBookTapped,
|
||||
),
|
||||
BookList(
|
||||
books: library.newBooks,
|
||||
onTap: _handleBookTapped,
|
||||
),
|
||||
BookList(
|
||||
books: library.allBooks,
|
||||
onTap: _handleBookTapped,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
RouteState get _routeState => RouteStateScope.of(context);
|
||||
|
||||
|
||||
@@ -7,10 +7,10 @@ import 'package:flutter/material.dart';
|
||||
|
||||
import '../auth.dart';
|
||||
import '../data.dart';
|
||||
import '../data/library.dart';
|
||||
import '../routing.dart';
|
||||
import '../screens/sign_in.dart';
|
||||
import '../widgets/fade_transition_page.dart';
|
||||
import '../widgets/library_scope.dart';
|
||||
import 'author_details.dart';
|
||||
import 'book_details.dart';
|
||||
import 'scaffold.dart';
|
||||
@@ -40,17 +40,16 @@ class _BookstoreNavigatorState extends State<BookstoreNavigator> {
|
||||
final routeState = RouteStateScope.of(context);
|
||||
final authState = BookstoreAuthScope.of(context);
|
||||
final pathTemplate = routeState.route.pathTemplate;
|
||||
final library = LibraryScope.of(context);
|
||||
|
||||
Book? selectedBook;
|
||||
if (pathTemplate == '/book/:bookId') {
|
||||
selectedBook = library.allBooks.firstWhereOrNull(
|
||||
selectedBook = libraryInstance.allBooks.firstWhereOrNull(
|
||||
(b) => b.id.toString() == routeState.route.parameters['bookId']);
|
||||
}
|
||||
|
||||
Author? selectedAuthor;
|
||||
if (pathTemplate == '/author/:authorId') {
|
||||
selectedAuthor = library.allAuthors.firstWhereOrNull(
|
||||
selectedAuthor = libraryInstance.allAuthors.firstWhereOrNull(
|
||||
(b) => b.id.toString() == routeState.route.parameters['authorId']);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
// Copyright 2021, 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.
|
||||
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import '../data.dart';
|
||||
|
||||
class LibraryScope extends InheritedWidget {
|
||||
final Library library;
|
||||
|
||||
const LibraryScope({
|
||||
Key? key,
|
||||
required this.library,
|
||||
required Widget child,
|
||||
}) : super(key: key, child: child);
|
||||
|
||||
@override
|
||||
bool updateShouldNotify(LibraryScope oldWidget) =>
|
||||
library != oldWidget.library;
|
||||
|
||||
static Library of(BuildContext context) =>
|
||||
context.dependOnInheritedWidgetOfExactType<LibraryScope>()!.library;
|
||||
}
|
||||
Reference in New Issue
Block a user