如何用if(登录帐号firebase)在flutter中更改抽屉幻灯片



我有两个抽屉幻灯片(一个用于注册和登录,另一个用于配置文件和设置(如果没有登录帐户,我想点击菜单显示菜单1,当选择登录帐户时显示菜单2

我完成了这个代码,但没有工作这个问题的解决办法是什么

class PageMain extends StatefulWidget {
static const String id = "HOMESCREEN";
final FirebaseUser user;
const PageMain({Key key, this.user}) : super(key: key);
@override
_PageMainState createState() => _PageMainState();
}
class _PageMainState extends State<PageMain> {
final FirebaseAuth _auth = FirebaseAuth.instance;
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
Future<void> _handleDrawer()async{
_scaffoldKey.currentState.openEndDrawer();
var current_user = await _auth.currentUser();
if (current_user != null) {
return Menu();
} else {
return Menu2();
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
endDrawer: new Drawer(
child: _handleDrawer(),
),
backgroundColor: Color(0xFFa49cee),
body: ListView(
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 15.0, left: 10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
IconButton(
icon: Icon(Icons.arrow_back_ios),
color: Colors.white,
onPressed: () {},
),
Container(
width: 125.0,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
IconButton(
icon: Icon(Icons.info),
color: Colors.white,
onPressed: () {},
),
IconButton(
icon: Icon(Icons.menu),
color: Colors.white,
onPressed: () {
_scaffoldKey.currentState.openEndDrawer();
_handleDrawer();

},
),
],
),
),
],
),
),
SizedBox(
height: 35.0,
),

);
debugShowCheckedModeBanner:
false;
}
}
class Menu extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Drawer(
child: ListView(
padding: EdgeInsets.only(top: 20.0),
children: <Widget>[
DrawerHeader(
child: Text(
'Side menu',
style: TextStyle(color: Colors.white, fontSize: 25),
),
decoration: BoxDecoration(
color: Colors.green,
image: DecorationImage(
fit: BoxFit.fill,
image: AssetImage('assets/images/shaar.jpg'))),
),
ListTile(
leading: Icon(Icons.input,
textDirection: TextDirection.rtl, color: Colors.red),
title: Text('welcome',
textDirection: TextDirection.rtl,
style: TextStyle(color: Colors.black, fontSize: 18.0)),
onTap: () => {},
),
ListTile(
leading: Icon(Icons.email),
title: Text('login',
textDirection: TextDirection.rtl,
style: TextStyle(color: Colors.black, fontSize: 18.0)),
onTap: () => {Navigator.of(context).pushNamed(Login.id)},
),
ListTile(
leading: Icon(Icons.add),
title: Text('register',
textDirection: TextDirection.rtl,
style: TextStyle(color: Colors.black, fontSize: 18.0)),
onTap: () => {Navigator.of(context).pushNamed(Registration.id)},
),
ListTile(
leading: Icon(Icons.insert_emoticon),
title: Text('info',
textDirection: TextDirection.rtl,
style: TextStyle(color: Colors.black, fontSize: 18.0)),
onTap: () => {Navigator.of(context).pop()},
),
],
),
);
}
}
class Menu2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Drawer(
child: ListView(
padding: EdgeInsets.only(top: 20.0),
children: <Widget>[
DrawerHeader(
child: Text(
'Side menu',
style: TextStyle(color: Colors.white, fontSize: 25),
),
decoration: BoxDecoration(
color: Colors.green,
image: DecorationImage(
fit: BoxFit.fill,
image: AssetImage('assets/images/shaar.jpg'))),
),
ListTile(
leading: Icon(Icons.input),
title: Text('welcom', textDirection: TextDirection.rtl),
onTap: () => {},
),
ListTile(
leading: Icon(Icons.verified_user),
title: Text('Profile'),
onTap: () => {},
),
ListTile(
leading: Icon(Icons.settings),
title: Text('Settings'),
onTap: () => {},
),
ListTile(
leading: Icon(Icons.exit_to_app),
title: Text('Logout'),
onTap: () => {},
),
],
),
);
}
}

我认为_handleDrawer((不能返回Future。相反,请尝试使用FutureBuilder。将not SignedIn小部件显示为默认值,并等待将来检查用户是否已登录并相应地重新生成。

你的endDrawer应该是这样的:

FutureBuilder(
future: _auth.currentUser(),
builder: (context, AsyncSnapshot<FirebaseUser> snapshot) {

if(snapshot.data != null) {
return Menu();
} else {
return Menu2();
}

},
)

相关内容

最新更新