当我单击抽屉项目(主页)时,它可以正常工作,并且可以从主页屏幕的应用栏访问抽屉。现在的问题是当我从首页导航到另一个屏幕(初学者计划)时, 抽屉消失了。
import 'package:fitnessapp/components/constants.dart';
import 'package:fitnessapp/screens/NavPages/beginnerplan.dart';
import 'package:fitnessapp/screens/NavPages/running.dart';
import 'NavPages/homepage.dart';
import 'NavPages/profile.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:flutter/material.dart';
class DrawerItem {
String title;
IconData icon;
DrawerItem(this.title, this.icon);
}
class navdrawer extends StatefulWidget {
final drawerItems = [
new DrawerItem("Home Page", Icons.home),
new DrawerItem("Profile", Icons.account_circle),
new DrawerItem("Running", Icons.directions_run),
];
@override
State<StatefulWidget> createState() {
return new navdrawerState();
}
}
class navdrawerState extends State<navdrawer> {
int _selectedDrawerIndex = 0;
navdrawerState();
_getDrawerItemWidget(int pos) {
switch (pos) {
case 0:
return new homepage();
case 1:
return new profilepage();
case 2:
return new RunningPage();
default:
return Container();
}
}
onSelectItem(int index) {
setState(() => _selectedDrawerIndex = index);
Navigator.of(context).pop(); // close the drawer
}
@override
Widget build(BuildContext context) {
var drawerOptions = <Widget>[];
for (var i = 0; i < widget.drawerItems.length; i++) {
var d = widget.drawerItems[i];
drawerOptions.add(new ListTile(
leading: new Icon(d.icon),
title: new Text(d.title),
selected: i == _selectedDrawerIndex,
onTap: () => onSelectItem(i),
));
}
return new Scaffold(
appBar: new AppBar(
// here we display the title corresponding to the fragment
// you can instead choose to have a static title
title: new Text(widget.drawerItems[_selectedDrawerIndex].title),
),
drawer: new Drawer(
child: Column(
children: [
Expanded(flex: 1, child: UserHeader()),
Expanded(
flex: 2,
child: new Column(children: drawerOptions),
)
],
),
),
body: _getDrawerItemWidget(_selectedDrawerIndex),
);
}
}
class UserHeader extends StatelessWidget {
const UserHeader({
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
color: mainaccent,
width: MediaQuery.of(context).size.width * 0.85,
child: DrawerHeader(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("images/minerva_tp.png"),
fit: BoxFit.cover)),
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
SizedBox(
height: 50,
),
Text(
"Shahroz Javed",
style: TextStyle(color: Colors.white,fontSize: 20),
),
Text(
"shahrozjaved39@gmail.com",
style: TextStyle(color: Colors.white,fontSize: 15),
),
],
),
),
);
}
}
ontap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => beginnerplan()),
);
import 'package:fitnessapp/screens/Navdrawer.dart';
import 'package:flutter/material.dart';
class beginnerplan extends StatefulWidget {
@override
_beginnerplanState createState() => _beginnerplanState();
}
class _beginnerplanState extends State<beginnerplan> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Text("Beginner plan"),
);
}
}