Step10 Change the content by user's login state
Goal of this step
- Change content by user's login state
In this step, we will
- Pass down state from top to bottom.
- Change the content by using passed down state.
lib/main.dart
StatefulWidget
Make Convert lib/main.dart
from stateless widget to StatefulWidget.
This is because I want to handle state in this widget.
onAuthStateChanged
and check isAuthenticated
Add firebase - Declare
isAuthenticated
initState()
is LifeCycle methodonAuthStateChanged
is triggered on sign-in or sign-out. https://firebase.google.com/docs/reference/js/firebase.auth.Auth.html#onauthstatechanged- In
onAuthStateChanged
if user exists, setState() =>isAuthenticated
true. - Pass down
isAuthenticated
state toHomePage
isAuthenticated
props
Edit HomePage to accept You can imitate this by lib/main_initial.dart
Here, we change the content by user's login state passed down from parent widget.
Add Firebase Auth Sign Out code
Sign out code is... https://pub.dev/documentation/firebase_auth/latest/firebase_auth/FirebaseAuth-class.html
Test it
By sign out and login, please check the content changes.
Summery
So, we were able to pass down props and change the content by using passed down props!!
This is 1 level down. So not troublesome.
In the next section we will pass down props 2 level down.
Edit Drawer
1. Separate Drawer widgets
- Make
widgets
folder underlib
. - Make
lib/widgets/home_drawer.dart
Extract Drawer
from HomePage.
widget.isAuthenticated
to home_drawer.dart
2. Pass 3. Edit HomeDrawer to accept isAuthenticated
isAuthenticated
4. Change Drawer content by - If you see sdk version warning, please click quick fix of vscode.
Review pass down props
Like this example, passing down isAuthenticated
is troublesome...
So, in the next step I will introduce Provider!