1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-11 15:28:44 +00:00

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

This commit is contained in:
Kevin Moore
2021-08-26 16:18:40 -07:00
committed by GitHub
parent ecf716dcab
commit c9688ca34b
8 changed files with 77 additions and 114 deletions

View File

@@ -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}');
},

View File

@@ -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);

View File

@@ -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']);
}