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

Cleanup to navigation_and_routing sample (#881)

- make more things private and final, where possible
- remove unused members
- used expression bodies, where possible
This commit is contained in:
Kevin Moore
2021-08-26 09:16:27 -07:00
committed by GitHub
parent 410e43fbc1
commit fa7dec1475
20 changed files with 221 additions and 284 deletions

View File

@@ -17,25 +17,23 @@ class AuthorDetailsScreen extends StatelessWidget {
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(author.name),
),
body: Center(
child: Column(
children: [
Expanded(
child: BookList(
books: author.books,
onTap: (book) {
RouteStateScope.of(context)!.go('/book/${book.id}');
},
),
),
],
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: Text(author.name),
),
),
);
}
body: Center(
child: Column(
children: [
Expanded(
child: BookList(
books: author.books,
onTap: (book) {
RouteStateScope.of(context)!.go('/book/${book.id}');
},
),
),
],
),
),
);
}

View File

@@ -14,17 +14,15 @@ class AuthorsScreen extends StatelessWidget {
const AuthorsScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: AuthorList(
authors: LibraryScope.of(context).allAuthors,
onTap: (author) {
RouteStateScope.of(context)!.go('/author/${author.id}');
},
),
);
}
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: Text(title),
),
body: AuthorList(
authors: LibraryScope.of(context).allAuthors,
onTap: (author) {
RouteStateScope.of(context)!.go('/author/${author.id}');
},
),
);
}

View File

@@ -45,21 +45,18 @@ class BookDetailsScreen extends StatelessWidget {
onPressed: () {
Navigator.of(context).push<void>(
MaterialPageRoute<void>(
builder: (context) {
return AuthorDetailsScreen(author: book!.author);
},
builder: (context) =>
AuthorDetailsScreen(author: book!.author),
),
);
},
),
Link(
uri: Uri.parse('/author/${book!.author.id}'),
builder: (context, followLink) {
return TextButton(
onPressed: followLink,
child: const Text('View author (Link)'),
);
},
builder: (context, followLink) => TextButton(
onPressed: followLink,
child: const Text('View author (Link)'),
),
),
],
),

View File

@@ -10,11 +10,8 @@ import '../widgets/book_list.dart';
import '../widgets/library_scope.dart';
class BooksScreen extends StatefulWidget {
final ParsedRoute currentRoute;
const BooksScreen({
Key? key,
required this.currentRoute,
}) : super(key: key);
@override
@@ -36,7 +33,7 @@ class _BooksScreenState extends State<BooksScreen>
void didChangeDependencies() {
super.didChangeDependencies();
final newPath = routeState.route.pathTemplate;
final newPath = _routeState.route.pathTemplate;
if (newPath.startsWith('/books/popular')) {
_tabController.index = 0;
} else if (newPath.startsWith('/books/new')) {
@@ -96,35 +93,23 @@ class _BooksScreenState extends State<BooksScreen>
);
}
String get title {
switch (_tabController.index) {
case 1:
return 'New';
case 2:
return 'All';
case 0:
default:
return 'Popular';
}
}
RouteState get routeState => RouteStateScope.of(context)!;
RouteState get _routeState => RouteStateScope.of(context)!;
void _handleBookTapped(Book book) {
routeState.go('/book/${book.id}');
_routeState.go('/book/${book.id}');
}
void _handleTabIndexChanged() {
switch (_tabController.index) {
case 1:
routeState.go('/books/new');
_routeState.go('/books/new');
break;
case 2:
routeState.go('/books/all');
_routeState.go('/books/all');
break;
case 0:
default:
routeState.go('/books/popular');
_routeState.go('/books/popular');
break;
}
}

View File

@@ -30,10 +30,10 @@ class BookstoreNavigator extends StatefulWidget {
}
class _BookstoreNavigatorState extends State<BookstoreNavigator> {
final signInKey = const ValueKey('Sign in');
final scaffoldKey = const ValueKey<String>('App scaffold');
final bookDetailsKey = const ValueKey<String>('Book details screen');
final authorDetailsKey = const ValueKey<String>('Author details screen');
final _signInKey = const ValueKey('Sign in');
final _scaffoldKey = const ValueKey<String>('App scaffold');
final _bookDetailsKey = const ValueKey<String>('Book details screen');
final _authorDetailsKey = const ValueKey<String>('Author details screen');
@override
Widget build(BuildContext context) {
@@ -60,12 +60,12 @@ class _BookstoreNavigatorState extends State<BookstoreNavigator> {
// When a page that is stacked on top of the scaffold is popped, display
// the /books or /authors tab in BookstoreScaffold.
if (route.settings is Page &&
(route.settings as Page).key == bookDetailsKey) {
(route.settings as Page).key == _bookDetailsKey) {
routeState.go('/books/popular');
}
if (route.settings is Page &&
(route.settings as Page).key == authorDetailsKey) {
(route.settings as Page).key == _authorDetailsKey) {
routeState.go('/authors');
}
@@ -75,7 +75,7 @@ class _BookstoreNavigatorState extends State<BookstoreNavigator> {
if (routeState.route.pathTemplate == '/signin')
// Display the sign in screen.
FadeTransitionPage<void>(
key: signInKey,
key: _signInKey,
child: SignInScreen(
onSignIn: (credentials) async {
var signedIn = await authState.signIn(
@@ -89,21 +89,21 @@ class _BookstoreNavigatorState extends State<BookstoreNavigator> {
else ...[
// Display the app
FadeTransitionPage<void>(
key: scaffoldKey,
key: _scaffoldKey,
child: const BookstoreScaffold(),
),
// Add an additional page to the stack if the user is viewing a book
// or an author
if (selectedBook != null)
MaterialPage<void>(
key: bookDetailsKey,
key: _bookDetailsKey,
child: BookDetailsScreen(
book: selectedBook,
),
)
else if (selectedAuthor != null)
MaterialPage<void>(
key: authorDetailsKey,
key: _authorDetailsKey,
child: AuthorDetailsScreen(
author: selectedAuthor,
),

View File

@@ -41,9 +41,9 @@ class BookstoreScaffoldBody extends StatelessWidget {
)
else if (currentRoute.pathTemplate.startsWith('/books') ||
currentRoute.pathTemplate == '/')
FadeTransitionPage<void>(
key: const ValueKey('books'),
child: BooksScreen(currentRoute: currentRoute),
const FadeTransitionPage<void>(
key: ValueKey('books'),
child: BooksScreen(),
)
// Avoid building a Navigator with an empty `pages` list when the

View File

@@ -17,26 +17,24 @@ class SettingsScreen extends StatefulWidget {
class _SettingsScreenState extends State<SettingsScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: SingleChildScrollView(
child: Align(
alignment: Alignment.topCenter,
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 400),
child: const Card(
child: Padding(
padding: EdgeInsets.symmetric(vertical: 18, horizontal: 12),
child: SettingsContent(),
Widget build(BuildContext context) => Scaffold(
body: SafeArea(
child: SingleChildScrollView(
child: Align(
alignment: Alignment.topCenter,
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 400),
child: const Card(
child: Padding(
padding: EdgeInsets.symmetric(vertical: 18, horizontal: 12),
child: SettingsContent(),
),
),
),
),
),
),
),
);
}
);
}
class SettingsContent extends StatelessWidget {
@@ -45,57 +43,53 @@ class SettingsContent extends StatelessWidget {
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
children: [
...[
Text(
'Settings',
style: Theme.of(context).textTheme.headline4,
),
ElevatedButton(
onPressed: () {
BookstoreAuthScope.of(context)!.signOut();
},
child: const Text('Sign out'),
),
Link(
uri: Uri.parse('/book/0'),
builder: (context, followLink) {
return TextButton(
Widget build(BuildContext context) => Column(
children: [
...[
Text(
'Settings',
style: Theme.of(context).textTheme.headline4,
),
ElevatedButton(
onPressed: () {
BookstoreAuthScope.of(context)!.signOut();
},
child: const Text('Sign out'),
),
Link(
uri: Uri.parse('/book/0'),
builder: (context, followLink) => TextButton(
onPressed: followLink,
child: const Text('Go directly to /book/0 (Link)'),
);
},
),
TextButton(
child: const Text('Go directly to /book/0 (RouteState)'),
onPressed: () {
RouteStateScope.of(context)!.go('/book/0');
},
),
].map((w) => Padding(padding: const EdgeInsets.all(8), child: w)),
TextButton(
onPressed: () => showDialog<String>(
context: context,
builder: (context) => AlertDialog(
title: const Text('Alert!'),
content: const Text('The alert description goes here.'),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.pop(context, 'Cancel'),
child: const Text('Cancel'),
),
TextButton(
onPressed: () => Navigator.pop(context, 'OK'),
child: const Text('OK'),
),
],
),
),
),
child: const Text('Show Dialog'),
)
],
);
}
TextButton(
child: const Text('Go directly to /book/0 (RouteState)'),
onPressed: () {
RouteStateScope.of(context)!.go('/book/0');
},
),
].map((w) => Padding(padding: const EdgeInsets.all(8), child: w)),
TextButton(
onPressed: () => showDialog<String>(
context: context,
builder: (context) => AlertDialog(
title: const Text('Alert!'),
content: const Text('The alert description goes here.'),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.pop(context, 'Cancel'),
child: const Text('Cancel'),
),
TextButton(
onPressed: () => Navigator.pop(context, 'OK'),
child: const Text('OK'),
),
],
),
),
child: const Text('Show Dialog'),
)
],
);
}

View File

@@ -29,43 +29,41 @@ class _SignInScreenState extends State<SignInScreen> {
final _passwordController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Card(
child: Container(
constraints: BoxConstraints.loose(const Size(600, 600)),
padding: const EdgeInsets.all(8),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Text('Sign in', style: Theme.of(context).textTheme.headline4),
TextField(
decoration: const InputDecoration(labelText: 'Username'),
controller: _usernameController,
),
TextField(
decoration: const InputDecoration(labelText: 'Password'),
obscureText: true,
controller: _passwordController,
),
Padding(
padding: const EdgeInsets.all(16),
child: TextButton(
onPressed: () async {
widget.onSignIn(Credentials(
_usernameController.value.text,
_passwordController.value.text));
},
child: const Text('Sign in'),
Widget build(BuildContext context) => Scaffold(
body: Center(
child: Card(
child: Container(
constraints: BoxConstraints.loose(const Size(600, 600)),
padding: const EdgeInsets.all(8),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Text('Sign in', style: Theme.of(context).textTheme.headline4),
TextField(
decoration: const InputDecoration(labelText: 'Username'),
controller: _usernameController,
),
),
],
TextField(
decoration: const InputDecoration(labelText: 'Password'),
obscureText: true,
controller: _passwordController,
),
Padding(
padding: const EdgeInsets.all(16),
child: TextButton(
onPressed: () async {
widget.onSignIn(Credentials(
_usernameController.value.text,
_passwordController.value.text));
},
child: const Text('Sign in'),
),
),
],
),
),
),
),
),
);
}
);
}