@ -23,7 +23,9 @@ class AppState {
AppState ( this . auth ) ;
AppState ( this . auth ) ;
}
}
/ / / Creates a [ DashboardApi ] when the user is logged in .
/ / / Creates a [ DashboardApi ] for the given user . This allows users of this
/ / / widget to specify whether [ MockDashboardApi ] or [ ApiBuilder ] should be
/ / / created when the user logs in .
typedef DashboardApi ApiBuilder ( User user ) ;
typedef DashboardApi ApiBuilder ( User user ) ;
/ / / An app that displays a personalized dashboard .
/ / / An app that displays a personalized dashboard .
@ -63,41 +65,62 @@ class _DashboardAppState extends State<DashboardApp> {
return Provider . value (
return Provider . value (
value: _appState ,
value: _appState ,
child: MaterialApp (
child: MaterialApp (
home: Builder (
home: SignInSwitcher (
builder: ( context ) = > SignInPage (
appState: _appState ,
auth: _appState . auth ,
apiBuilder: widget . apiBuilder ,
onSuccess: ( user ) = > _handleSignIn ( user , context , _appState ) ,
) ,
) ,
) ,
) ,
) ,
) ;
) ;
}
}
}
/ / / Switches between showing the [ SignInPage ] or [ HomePage ] , depending on
/ / / whether or not the user is signed in .
class SignInSwitcher extends StatefulWidget {
final AppState appState ;
final ApiBuilder apiBuilder ;
SignInSwitcher ( {
this . appState ,
this . apiBuilder ,
} ) ;
@ override
_SignInSwitcherState createState ( ) = > _SignInSwitcherState ( ) ;
}
/ / / Sets the DashboardApi on AppState and navigates to the home page .
class _SignInSwitcherState extends State < SignInSwitcher > {
void _handleSignIn ( User user , BuildContext context , AppState appState ) {
bool _isSignedIn = false ;
appState . api = widget . apiBuilder ( user ) ;
_showPage ( HomePage ( ) , context ) ;
@ override
Widget build ( BuildContext context ) {
return AnimatedSwitcher (
switchInCurve: Curves . easeOut ,
switchOutCurve: Curves . easeOut ,
duration: Duration ( milliseconds: 200 ) ,
child: _isSignedIn
? HomePage (
onSignOut: _handleSignOut ,
)
: SignInPage (
auth: widget . appState . auth ,
onSuccess: _handleSignIn ,
) ,
) ;
}
}
/ / / Navigates to the home page using a fade transition .
void _handleSignIn ( User user ) {
void _showPage ( Widget page , BuildContext context ) {
widget . appState . api = widget . apiBuilder ( user ) ;
var route = _fadeRoute ( page ) ;
Navigator . of ( context ) . pushReplacement ( route ) ;
setState ( ( ) {
_isSignedIn = true ;
} ) ;
}
}
/ / / Creates a [ Route ] that shows [ newPage ] using a fade transition .
Future _handleSignOut ( ) async {
Route < FadeTransition > _fadeRoute ( Widget newPage ) {
await widget . appState . auth . signOut ( ) ;
return PageRouteBuilder < FadeTransition > (
setState ( ( ) {
pageBuilder: ( context , animation , secondaryAnimation ) {
_isSignedIn = false ;
return newPage ;
} ) ;
} ,
transitionsBuilder: ( context , animation , secondaryAnimation , child ) {
return FadeTransition (
opacity: animation . drive ( CurveTween ( curve: Curves . ease ) ) ,
child: child ,
) ;
} ,
) ;
}
}
}
}