mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 22:09:06 +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 '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,14 +61,11 @@ class _BookstoreState extends State<Bookstore> {
|
|||||||
notifier: _routeState,
|
notifier: _routeState,
|
||||||
child: BookstoreAuthScope(
|
child: BookstoreAuthScope(
|
||||||
notifier: _auth,
|
notifier: _auth,
|
||||||
child: LibraryScope(
|
|
||||||
library: library,
|
|
||||||
child: MaterialApp.router(
|
child: MaterialApp.router(
|
||||||
routerDelegate: _routerDelegate,
|
routerDelegate: _routerDelegate,
|
||||||
routeInformationParser: _routeParser,
|
routeInformationParser: _routeParser,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
Future<ParsedRoute> _guard(ParsedRoute from) async {
|
Future<ParsedRoute> _guard(ParsedRoute from) async {
|
||||||
|
|||||||
@@ -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);
|
||||||
|
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);
|
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,9 +49,7 @@ class _BooksScreenState extends State<BooksScreen>
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) => Scaffold(
|
||||||
final library = LibraryScope.of(context);
|
|
||||||
return Scaffold(
|
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: const Text('Books'),
|
title: const Text('Books'),
|
||||||
bottom: TabBar(
|
bottom: TabBar(
|
||||||
@@ -77,21 +74,20 @@ class _BooksScreenState extends State<BooksScreen>
|
|||||||
controller: _tabController,
|
controller: _tabController,
|
||||||
children: [
|
children: [
|
||||||
BookList(
|
BookList(
|
||||||
books: library.popularBooks,
|
books: libraryInstance.popularBooks,
|
||||||
onTap: _handleBookTapped,
|
onTap: _handleBookTapped,
|
||||||
),
|
),
|
||||||
BookList(
|
BookList(
|
||||||
books: library.newBooks,
|
books: libraryInstance.newBooks,
|
||||||
onTap: _handleBookTapped,
|
onTap: _handleBookTapped,
|
||||||
),
|
),
|
||||||
BookList(
|
BookList(
|
||||||
books: library.allBooks,
|
books: libraryInstance.allBooks,
|
||||||
onTap: _handleBookTapped,
|
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;
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user