1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-08 22:09:06 +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

@@ -18,12 +18,11 @@ class Bookstore extends StatefulWidget {
}
class _BookstoreState extends State<Bookstore> {
final auth = BookstoreAuth();
late final BookstoreRouteGuard guard;
late final RouteState routeState;
late final SimpleRouterDelegate routerDelegate;
late final TemplateRouteParser routeParser;
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
final _auth = BookstoreAuth();
final _navigatorKey = GlobalKey<NavigatorState>();
late final RouteState _routeState;
late final SimpleRouterDelegate _routerDelegate;
late final TemplateRouteParser _routeParser;
final library = Library()
..addBook(
@@ -49,10 +48,10 @@ class _BookstoreState extends State<Bookstore> {
@override
void initState() {
guard = BookstoreRouteGuard(auth: auth);
final guard = BookstoreRouteGuard(auth: _auth);
/// Configure the parser with all of the app's allowed path templates.
routeParser = TemplateRouteParser(
_routeParser = TemplateRouteParser(
allowedPaths: [
'/signin',
'/authors',
@@ -67,50 +66,48 @@ class _BookstoreState extends State<Bookstore> {
initialRoute: '/signin',
);
routeState = RouteState(routeParser);
_routeState = RouteState(_routeParser);
routerDelegate = SimpleRouterDelegate(
routeState: routeState,
navigatorKey: navigatorKey,
_routerDelegate = SimpleRouterDelegate(
routeState: _routeState,
navigatorKey: _navigatorKey,
builder: (context) => BookstoreNavigator(
navigatorKey: navigatorKey,
navigatorKey: _navigatorKey,
),
);
// Listen for when the user logs out and display the signin screen.
auth.addListener(_handleAuthStateChanged);
_auth.addListener(_handleAuthStateChanged);
super.initState();
}
@override
Widget build(BuildContext context) {
return RouteStateScope(
notifier: routeState,
child: BookstoreAuthScope(
notifier: auth,
child: LibraryScope(
library: library,
child: MaterialApp.router(
routerDelegate: routerDelegate,
routeInformationParser: routeParser,
Widget build(BuildContext context) => RouteStateScope(
notifier: _routeState,
child: BookstoreAuthScope(
notifier: _auth,
child: LibraryScope(
library: library,
child: MaterialApp.router(
routerDelegate: _routerDelegate,
routeInformationParser: _routeParser,
),
),
),
),
);
}
);
void _handleAuthStateChanged() {
if (!auth.signedIn) {
routeState.go('/signin');
if (!_auth.signedIn) {
_routeState.go('/signin');
}
}
@override
void dispose() {
auth.removeListener(_handleAuthStateChanged);
routeState.dispose();
routerDelegate.dispose();
_auth.removeListener(_handleAuthStateChanged);
_routeState.dispose();
_routerDelegate.dispose();
super.dispose();
}
}