@ -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 ) ,
) ,
) ,
) ,
) ,
) ,
) ;
) ;
}
}
}
/ / / Sets the DashboardApi on AppState and navigates to the home page .
/ / / Switches between showing the [ SignInPage ] or [ HomePage ] , depending on
void _handleSignIn ( User user , BuildContext context , AppState appState ) {
/ / / whether or not the user is signed in .
appState . api = widget . apiBuilder ( user ) ;
class SignInSwitcher extends StatefulWidget {
final AppState appState ;
final ApiBuilder apiBuilder ;
_showPage ( HomePage ( ) , context ) ;
SignInSwitcher ( {
}
this . appState ,
this . apiBuilder ,
} ) ;
/ / / Navigates to the home page using a fade transition .
@ override
void _showPage ( Widget page , BuildContext context ) {
_SignInSwitcherState createState ( ) = > _SignInSwitcherState ( ) ;
var route = _fadeRoute ( page ) ;
Navigator . of ( context ) . pushReplacement ( route ) ;
}
}
/ / / Creates a [ Route ] that shows [ newPage ] using a fade transition .
class _SignInSwitcherState extends State < SignInSwitcher > {
Route < FadeTransition > _fadeRoute ( Widget newPage ) {
bool _isSignedIn = false ;
return PageRouteBuilder < FadeTransition > (
pageBuilder: ( context , animation , secondaryAnimation ) {
@ override
return newPage ;
Widget build ( BuildContext context ) {
} ,
return AnimatedSwitcher (
transitionsBuilder: ( context , animation , secondaryAnimation , child ) {
switchInCurve: Curves . easeOut ,
return FadeTransition (
switchOutCurve: Curves . easeOut ,
opacity: animation . drive ( CurveTween ( curve: Curves . ease ) ) ,
duration: Duration ( milliseconds: 200 ) ,
child: child ,
child: _isSignedIn
) ;
? HomePage (
} ,
onSignOut: _handleSignOut ,
)
: SignInPage (
auth: widget . appState . auth ,
onSuccess: _handleSignIn ,
) ,
) ;
) ;
}
}
void _handleSignIn ( User user ) {
widget . appState . api = widget . apiBuilder ( user ) ;
setState ( ( ) {
_isSignedIn = true ;
} ) ;
}
Future _handleSignOut ( ) async {
await widget . appState . auth . signOut ( ) ;
setState ( ( ) {
_isSignedIn = false ;
} ) ;
}
}
}