import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
String _selectedItem = 'Home Screen';
void _selectItem(String item) {
setState(() {
_selectedItem = item;
});
Navigator.of(context).pop(); // Close the drawer
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Home Screen'),
leading: IconButton(
icon: const Icon(Icons.menu),
onPressed: () {
Scaffold.of(context).openDrawer();
},
tooltip: 'Open navigation menu',
),
),
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: [
const DrawerHeader(
decoration: BoxDecoration(color: Colors.blue),
child: Text('Menu', style: TextStyle(color: Colors.white, fontSize: 24)),
),
ListTile(
leading: const Icon(Icons.home),
title: const Text('Home'),
onTap: () => _selectItem('Home Screen'),
),
ListTile(
leading: const Icon(Icons.person),
title: const Text('Profile'),
onTap: () => _selectItem('Profile Screen'),
),
ListTile(
leading: const Icon(Icons.settings),
title: const Text('Settings'),
onTap: () => _selectItem('Settings Screen'),
),
],
),
),
body: Center(
child: Text(_selectedItem, style: const TextStyle(fontSize: 24)),
),
);
}
}
We added a leading menu icon button in the AppBar that opens the drawer when tapped. The drawer contains a header and three ListTile items: Home, Profile, and Settings. Each ListTile has an icon and label. When tapped, the drawer closes and the main screen text updates to show which menu item was selected. We used Navigator.of(context).pop() to close the drawer. This creates a simple and clear drawer navigation experience.