'Flutter - How do i access the Drawer from a nested Scaffold
This parent Bottom Navigation bar
import 'package:flutter/material.dart';
class BottomNavigation extends StatefulWidget {
BottomNavigation({
Key? key,
}) : super(key: key);
@override
_BottomNavigationState createState() => _BottomNavigationState();
}
class _BottomNavigationState extends State<OARBottomNavigation> {
int _selectedIndex = 0;
List<Widget> _widgetOptions = [
Screen1(),
Screen2(),
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
drawer: SideDrawer(),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.assignment_turned_in),
label: 'Screen 1',
),
BottomNavigationBarItem(
icon: Icon(Icons.people),
label: 'Screen 2',
),
],
currentIndex: _selectedIndex,
onTap: _onItemTapped,
),
body: Scaffold(
body: _widgetOptions.elementAt(_selectedIndex),
),
);
}
}
Now on Screen 1 I want to open the Drawer().
import 'package:flutter/material.dart';
class Screen1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Screen 1 Title'),
),
body: Container(
child: Text('This is Screen 1'),
),
),
);
}
}
How can I open the Drawer from Screen 1?
Solution 1:[1]
Please check the following code. There are BottomNavigationBar
and Drawer
(top of the BottomNavigationBar)
return Scaffold(
appBar: AppBar(),
drawer: Drawer(
child: Container(
color: Colors.red,
child: ListView.builder(
itemBuilder: (context, index) {
return ListTile(
title: InkWell(
onTap: () {},
child: Text(
index.toString(),
),
),
);
},
itemCount: 3,
),
),
),
bottomNavigationBar: BottomNavigationBar(items: [
BottomNavigationBarItem(icon: Icon(Icons.home), label: "Home"),
BottomNavigationBarItem(icon: Icon(Icons.settings), label: "Settings"),
]),
);
Solution 2:[2]
Better use:
Scaffold.of(context).openDrawer();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jimenez |
Solution 2 | ax1mx2 |