Flutter Drawer 在主页以外的页面上不起作用
Flutter Drawer is not working on pages except home
我有一个包含正文和抽屉的页面,登录后打开。
问题是抽屉只有在设置为 MaterialApp 的主页时才有效,但在我的例子中,主页是我的登录页面,用户应该登录才能继续
有什么办法可以不用设置为主页也能正常使用吗?
因为我是 flutter 的新手,所以我被困在这里,我需要你的帮助!提前感谢您的帮助。
主页
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _key = GlobalKey<FormState>();
TextEditingController _emailContoller = TextEditingController();
TextEditingController _passwordController = TextEditingController();
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
debugShowCheckedModeBanner: false,
home: OurLogin(
key2: _key,
emailContoller: _emailContoller,
passwordController: _passwordController,
),
);
}
}
主页的一部分(登录后打开)
class Home extends KFDrawerContent {
Home({
Key key,
});
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: ListView(
children: <Widget>[
Column(
children: <Widget>[
Row(
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(32.0)),
child: Material(
shadowColor: Colors.transparent,
color: Colors.transparent,
child: IconButton(
icon: Icon(
Icons.menu,
color: Colors.black,
),
onPressed: widget.onMenuPressed,
),
),
)
抽屉
class MainWidget extends StatefulWidget {
MainWidget({Key key, this.title}) : super(key: key);
final String title;
@override
_MainWidgetState createState() => _MainWidgetState();
}
class _MainWidgetState extends State<MainWidget> with TickerProviderStateMixin {
KFDrawerController _drawerController;
@override
void initState() {
super.initState();
_drawerController = KFDrawerController(
initialPage: ClassBuilder.fromString('Home'),
items: [
KFDrawerItem.initWithPage(
text:
Text('Home', style: TextStyle(color: Colors.white, fontSize: 18)),
icon: Icon(Icons.home, color: Colors.white),
page: Home(),
),
KFDrawerItem.initWithPage(
text: Text(
'Profile',
style: TextStyle(color: Colors.white, fontSize: 18),
),
icon: Icon(Icons.account_box, color: Colors.white),
page: Profile(),
),
KFDrawerItem.initWithPage(
text: Text(
'Notifications',
style: TextStyle(color: Colors.white, fontSize: 18),
),
icon: Icon(Icons.notifications_active, color: Colors.white),
page: ClassBuilder.fromString('Notifications'),
),
KFDrawerItem.initWithPage(
text: Text(
'Stats',
style: TextStyle(color: Colors.white, fontSize: 18),
),
icon: Icon(Icons.trending_up, color: Colors.white),
page: Stats(),
),
KFDrawerItem.initWithPage(
text: Text(
'Schedules',
style: TextStyle(color: Colors.white, fontSize: 18),
),
icon: Icon(Icons.av_timer, color: Colors.white),
page: Schedules(),
),
],
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: KFDrawer(
controller: _drawerController,
header: Align(
alignment: Alignment.centerLeft,
child: Container(
padding: EdgeInsets.symmetric(horizontal: 16.0),
width: MediaQuery.of(context).size.width * 0.8,
child: Row(
children: <Widget>[
Container(
height: 50,
width: 50,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(25),
image: DecorationImage(
image: AssetImage('images/image.jpg'),
fit: BoxFit.cover)),
),
SizedBox(
width: 10,
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Text('Scarlett Johansson',
style:
new TextStyle(fontSize: 17, color: Colors.white)),
new SizedBox(height: 2),
new Text('Actress',
style: new TextStyle(fontSize: 15, color: Colors.grey)),
],
)
],
),
),
),
footer: KFDrawerItem(
text: Text(
'Logout',
style: TextStyle(color: Colors.grey, fontSize: 18),
),
),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
Color.fromRGBO(31, 58, 47, 1.0),
Color.fromRGBO(31, 58, 47, 1.0)
],
tileMode: TileMode.repeated,
),
),
),
);
}
}
问题已解决,分享解决方案,因为它可能会有所帮助:
问题是登录后,它被推送到主页,但主页也包含抽屉小部件,所以我还需要指示抽屉小部件的路径才能正常工作。
我就是这样做的
//login page
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Stack(
children: [
Home(),
MainWidget(),
],
),
),
);
我有一个包含正文和抽屉的页面,登录后打开。
问题是抽屉只有在设置为 MaterialApp 的主页时才有效,但在我的例子中,主页是我的登录页面,用户应该登录才能继续
有什么办法可以不用设置为主页也能正常使用吗? 因为我是 flutter 的新手,所以我被困在这里,我需要你的帮助!提前感谢您的帮助。
主页
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _key = GlobalKey<FormState>();
TextEditingController _emailContoller = TextEditingController();
TextEditingController _passwordController = TextEditingController();
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
debugShowCheckedModeBanner: false,
home: OurLogin(
key2: _key,
emailContoller: _emailContoller,
passwordController: _passwordController,
),
);
}
}
主页的一部分(登录后打开)
class Home extends KFDrawerContent {
Home({
Key key,
});
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: ListView(
children: <Widget>[
Column(
children: <Widget>[
Row(
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(32.0)),
child: Material(
shadowColor: Colors.transparent,
color: Colors.transparent,
child: IconButton(
icon: Icon(
Icons.menu,
color: Colors.black,
),
onPressed: widget.onMenuPressed,
),
),
)
抽屉
class MainWidget extends StatefulWidget {
MainWidget({Key key, this.title}) : super(key: key);
final String title;
@override
_MainWidgetState createState() => _MainWidgetState();
}
class _MainWidgetState extends State<MainWidget> with TickerProviderStateMixin {
KFDrawerController _drawerController;
@override
void initState() {
super.initState();
_drawerController = KFDrawerController(
initialPage: ClassBuilder.fromString('Home'),
items: [
KFDrawerItem.initWithPage(
text:
Text('Home', style: TextStyle(color: Colors.white, fontSize: 18)),
icon: Icon(Icons.home, color: Colors.white),
page: Home(),
),
KFDrawerItem.initWithPage(
text: Text(
'Profile',
style: TextStyle(color: Colors.white, fontSize: 18),
),
icon: Icon(Icons.account_box, color: Colors.white),
page: Profile(),
),
KFDrawerItem.initWithPage(
text: Text(
'Notifications',
style: TextStyle(color: Colors.white, fontSize: 18),
),
icon: Icon(Icons.notifications_active, color: Colors.white),
page: ClassBuilder.fromString('Notifications'),
),
KFDrawerItem.initWithPage(
text: Text(
'Stats',
style: TextStyle(color: Colors.white, fontSize: 18),
),
icon: Icon(Icons.trending_up, color: Colors.white),
page: Stats(),
),
KFDrawerItem.initWithPage(
text: Text(
'Schedules',
style: TextStyle(color: Colors.white, fontSize: 18),
),
icon: Icon(Icons.av_timer, color: Colors.white),
page: Schedules(),
),
],
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: KFDrawer(
controller: _drawerController,
header: Align(
alignment: Alignment.centerLeft,
child: Container(
padding: EdgeInsets.symmetric(horizontal: 16.0),
width: MediaQuery.of(context).size.width * 0.8,
child: Row(
children: <Widget>[
Container(
height: 50,
width: 50,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(25),
image: DecorationImage(
image: AssetImage('images/image.jpg'),
fit: BoxFit.cover)),
),
SizedBox(
width: 10,
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Text('Scarlett Johansson',
style:
new TextStyle(fontSize: 17, color: Colors.white)),
new SizedBox(height: 2),
new Text('Actress',
style: new TextStyle(fontSize: 15, color: Colors.grey)),
],
)
],
),
),
),
footer: KFDrawerItem(
text: Text(
'Logout',
style: TextStyle(color: Colors.grey, fontSize: 18),
),
),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
Color.fromRGBO(31, 58, 47, 1.0),
Color.fromRGBO(31, 58, 47, 1.0)
],
tileMode: TileMode.repeated,
),
),
),
);
}
}
问题已解决,分享解决方案,因为它可能会有所帮助:
问题是登录后,它被推送到主页,但主页也包含抽屉小部件,所以我还需要指示抽屉小部件的路径才能正常工作。
我就是这样做的
//login page
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Stack(
children: [
Home(),
MainWidget(),
],
),
),
);