I create a profile page in my APP for users which can be logged in and logged out with their own account number and passcode. I use SharedPreference to store datas in local devices, including the bool 'logined', which will be set to 'true' after users logged in (default is 'false'). Here comes the problem. I want to show users their own profile page every time they navigate to this page without going to login page again after they have logged in. So, before I build this page, I have to check whether the 'logined' variable is 'true' or 'false'. If true, navigate to profile page. If false, show the login page. But, in fact, the login page is always showed before the LoginedCheck function(which is used to check the 'logined' variable and navigate) done its work. The login page will be showed and then quickly transfer to profile page. I've tried to use 'delay' to wait for LoginedCheck(), but the screen will turn black before the profile page created. Any ideas to solve the problem? Thanks for instance.
The Build method:
Widget build(BuildContext context) {
LoginedCheck();
return Scaffold(
resizeToAvoidBottomPadding: false,
body: Stack(
children: <Widget>[
Container(
height: double.infinity,
width: double.infinity,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color(0xFF73AEF5),
Color(0xFF61A4F1),
Color(0xFF478DE0),
Color(0xFF398AE5),
],
stops: [0.1,0.4,0.7,0.9],
)),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(
height: 10,
),
Icon(
Icons.account_circle,
size: 150.0,
),
SizedBox(height: 30),
Container(
width: 300,
child: Theme(
data: ThemeData(
primaryColor: Colors.white,
primaryColorDark: Colors.yellowAccent,
),
child: TextField(
controller: _LtextFieldAccount,
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: '請輸入帳號',
prefixIcon: const Icon(Icons.person)),
)),
),
SizedBox(height: 20),
Container(
width: 300,
child: Theme(
data: ThemeData(
primaryColor: Colors.white,
primaryColorDark: Colors.yellowAccent,
),
child: TextField(
controller: _LtextFieldID,
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: '請輸入密碼',
prefixIcon: const Icon(Icons.lock)),
)),
),
SizedBox(height: 30),
RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(25.0)),
elevation: 4.0,
color: Colors.white,
child: Container(
padding: EdgeInsets.only(
left: 10,right: 10,top: 10,bottom: 10),
child: Text(
'登入',
style:
TextStyle(fontSize: 25,color: Colors.blueAccent),
),
),
onPressed: Login,
),
SizedBox(height: 20),
RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(25.0)),
elevation: 4.0,
color: Colors.white,
child: Container(
padding: EdgeInsets.only(
left: 10,right: 10,top: 10,bottom: 10),
child: Text(
'新用戶註冊',
style:
TextStyle(fontSize: 25,color: Colors.blueAccent),
),
),
onPressed: (){
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => RegisterPage()));
})
],
),
),
),
],
),
bottomNavigationBar: BottomNavigationBar(
onTap: onTabTapped,
currentIndex: _currentIndex, //thiswillbesetwhenanewtabistapped
items: [
new BottomNavigationBarItem(
icon: new Icon(Icons.school),
title: new Text('大學'),
),
new BottomNavigationBarItem(
icon: new Icon(Icons.directions_subway),
title: new Text('交通'),
),
new BottomNavigationBarItem(
icon: new Icon(Icons.person),
title: new Text('個人'),
)
],
),
);
}
The LoginedCheck() function:
Future LoginedCheck() async{
SharedPreferences prefs = await SharedPreferences.getInstance();
bool logined = prefs.getBool(_StorageLogined) ?? false;
int GetUserNum = prefs.getInt(_StorageUserNum) ?? 0;
if(logined == true) {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => ShowProfile(UserNum: GetUserNum,)));
}
}