Reputation: 8683
I have created a Navigation Drawer in Flutter.
Source available here - https://github.com/deadcoder0904/flutter_navigation_drawer
It looks like this -
When I click the button, it goes to
When I click the button, it goes to
When I click the hamburger icon on First Screen
, it goes to
Now when I click on the 2nd List Item on Drawer Screen, I get the Second Screen like this
class NavigationDrawer extends StatefulWidget {
_NavigationDrawerState createState() => _NavigationDrawerState();
}
class _NavigationDrawerState extends State<NavigationDrawer> {
int _selectionIndex = 0;
final drawerItems = [
DrawerItem("First Screen", Icons.looks_one),
DrawerItem("Second Screen", Icons.looks_two),
DrawerItem("Tabs", Icons.tab),
];
_getDrawerItemScreen(int pos) {
switch (pos) {
case 1:
return SecondScreen();
case 2:
return Tabs();
default:
return FirstScreen();
}
}
_onSelectItem(int index) {
setState(() {
_selectionIndex = index;
_getDrawerItemScreen(_selectionIndex);
});
Navigator.of(context).pop();
}
@override
Widget build(BuildContext context) {
List<Widget> drawerOptions = [];
for (var i = 0; i < drawerItems.length; i++) {
var d = drawerItems[i];
drawerOptions.add(ListTile(
leading: Icon(d.icon),
title: Text(
d.title,
style: TextStyle(fontSize: 18.0, fontWeight: FontWeight.w400),
),
selected: i == _selectionIndex,
onTap: () => _onSelectItem(i),
));
}
return Scaffold(
appBar: AppBar(
title: Text('First Screen'),
),
drawer: Drawer(
child: Column(
children: <Widget>[
UserAccountsDrawerHeader(
accountName: Text('Akshay Kadam (A2K)'),
accountEmail: Text('[email protected]'),
),
Column(
children: drawerOptions,
),
],
),
),
body: _getDrawerItemScreen(_selectionIndex),
);
}
}
How do I get the Second Screen without the Hamburger Icon
& First Screen
title?
Upvotes: 1
Views: 2334
Reputation: 47069
First, change your code to set HomePage
body: _getDrawerItemScreen(_selectionIndex),
to
body: FirstScreen(),
Secondly,
_onSelectItem(int index) {
setState(() {
_selectionIndex = index;
_getDrawerItemScreen(_selectionIndex);
});
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => _getDrawerItemScreen(_selectionIndex),
),
);
}
Upvotes: 1