Move library to a top-level variable, since it never changes (#887)

pull/888/head
Kevin Moore 3 years ago committed by GitHub
parent ecf716dcab
commit c9688ca34b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -5,10 +5,8 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'auth.dart'; import 'auth.dart';
import 'data.dart';
import 'routing.dart'; import 'routing.dart';
import 'screens/navigator.dart'; import 'screens/navigator.dart';
import 'widgets/library_scope.dart';
class Bookstore extends StatefulWidget { class Bookstore extends StatefulWidget {
const Bookstore({Key? key}) : super(key: key); const Bookstore({Key? key}) : super(key: key);
@ -24,28 +22,6 @@ class _BookstoreState extends State<Bookstore> {
late final SimpleRouterDelegate _routerDelegate; late final SimpleRouterDelegate _routerDelegate;
late final TemplateRouteParser _routeParser; 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 @override
void initState() { void initState() {
/// Configure the parser with all of the app's allowed path templates. /// Configure the parser with all of the app's allowed path templates.
@ -85,12 +61,9 @@ class _BookstoreState extends State<Bookstore> {
notifier: _routeState, notifier: _routeState,
child: BookstoreAuthScope( child: BookstoreAuthScope(
notifier: _auth, notifier: _auth,
child: LibraryScope( child: MaterialApp.router(
library: library, routerDelegate: _routerDelegate,
child: MaterialApp.router( routeInformationParser: _routeParser,
routerDelegate: _routerDelegate,
routeInformationParser: _routeParser,
),
), ),
), ),
); );

@ -7,7 +7,7 @@ import 'book.dart';
class Author { class Author {
final int id; final int id;
final String name; 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 { class Book {
final int id; final int id;
final String title; final String title;
late final Author author; final Author author;
final bool isPopular; final bool isPopular;
final bool isNew; 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 // 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. // BSD-style license that can be found in the LICENSE file.
import 'package:collection/collection.dart';
import 'author.dart'; import 'author.dart';
import 'book.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 { class Library {
final List<Book> allBooks = []; final List<Book> allBooks = [];
final List<Author> allAuthors = []; final List<Author> allAuthors = [];
@ -17,18 +37,17 @@ class Library {
required bool isPopular, required bool isPopular,
required bool isNew, required bool isNew,
}) { }) {
var author = var author = allAuthors.firstWhere(
allAuthors.firstWhereOrNull((author) => author.name == authorName); (author) => author.name == authorName,
var book = Book(allBooks.length, title, isPopular, isNew); orElse: () {
final value = Author(allAuthors.length, authorName);
if (author == null) { allAuthors.add(value);
author = Author(allAuthors.length, authorName, [book]); return value;
allAuthors.add(author); },
} else { );
author.books.add(book); var book = Book(allBooks.length, title, isPopular, isNew, author);
}
author.books.add(book);
book.author = author;
allBooks.add(book); allBooks.add(book);
} }

@ -4,9 +4,9 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import '../data/library.dart';
import '../routing.dart'; import '../routing.dart';
import '../widgets/author_list.dart'; import '../widgets/author_list.dart';
import '../widgets/library_scope.dart';
class AuthorsScreen extends StatelessWidget { class AuthorsScreen extends StatelessWidget {
final String title = 'Authors'; final String title = 'Authors';
@ -19,7 +19,7 @@ class AuthorsScreen extends StatelessWidget {
title: Text(title), title: Text(title),
), ),
body: AuthorList( body: AuthorList(
authors: LibraryScope.of(context).allAuthors, authors: libraryInstance.allAuthors,
onTap: (author) { onTap: (author) {
RouteStateScope.of(context).go('/author/${author.id}'); RouteStateScope.of(context).go('/author/${author.id}');
}, },

@ -7,7 +7,6 @@ import 'package:flutter/material.dart';
import '../data.dart'; import '../data.dart';
import '../routing.dart'; import '../routing.dart';
import '../widgets/book_list.dart'; import '../widgets/book_list.dart';
import '../widgets/library_scope.dart';
class BooksScreen extends StatefulWidget { class BooksScreen extends StatefulWidget {
const BooksScreen({ const BooksScreen({
@ -50,48 +49,45 @@ class _BooksScreenState extends State<BooksScreen>
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) => Scaffold(
final library = LibraryScope.of(context); appBar: AppBar(
return Scaffold( title: const Text('Books'),
appBar: AppBar( bottom: TabBar(
title: const Text('Books'), controller: _tabController,
bottom: TabBar( 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, controller: _tabController,
tabs: const [ children: [
Tab( BookList(
text: 'Popular', books: libraryInstance.popularBooks,
icon: Icon(Icons.people), onTap: _handleBookTapped,
), ),
Tab( BookList(
text: 'New', books: libraryInstance.newBooks,
icon: Icon(Icons.new_releases), onTap: _handleBookTapped,
), ),
Tab( BookList(
text: 'All', books: libraryInstance.allBooks,
icon: Icon(Icons.list), 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); RouteState get _routeState => RouteStateScope.of(context);

@ -7,10 +7,10 @@ import 'package:flutter/material.dart';
import '../auth.dart'; import '../auth.dart';
import '../data.dart'; import '../data.dart';
import '../data/library.dart';
import '../routing.dart'; import '../routing.dart';
import '../screens/sign_in.dart'; import '../screens/sign_in.dart';
import '../widgets/fade_transition_page.dart'; import '../widgets/fade_transition_page.dart';
import '../widgets/library_scope.dart';
import 'author_details.dart'; import 'author_details.dart';
import 'book_details.dart'; import 'book_details.dart';
import 'scaffold.dart'; import 'scaffold.dart';
@ -40,17 +40,16 @@ class _BookstoreNavigatorState extends State<BookstoreNavigator> {
final routeState = RouteStateScope.of(context); final routeState = RouteStateScope.of(context);
final authState = BookstoreAuthScope.of(context); final authState = BookstoreAuthScope.of(context);
final pathTemplate = routeState.route.pathTemplate; final pathTemplate = routeState.route.pathTemplate;
final library = LibraryScope.of(context);
Book? selectedBook; Book? selectedBook;
if (pathTemplate == '/book/:bookId') { if (pathTemplate == '/book/:bookId') {
selectedBook = library.allBooks.firstWhereOrNull( selectedBook = libraryInstance.allBooks.firstWhereOrNull(
(b) => b.id.toString() == routeState.route.parameters['bookId']); (b) => b.id.toString() == routeState.route.parameters['bookId']);
} }
Author? selectedAuthor; Author? selectedAuthor;
if (pathTemplate == '/author/:authorId') { if (pathTemplate == '/author/:authorId') {
selectedAuthor = library.allAuthors.firstWhereOrNull( selectedAuthor = libraryInstance.allAuthors.firstWhereOrNull(
(b) => b.id.toString() == routeState.route.parameters['authorId']); (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;
}
Loading…
Cancel
Save