Efficient navigation is crucial for a smooth user experience in any Flutter application. The NavigationRail widget serves as a powerful tool for creating vertical navigation bars, offering users quick access to different sections of your app. In this blog post, we will explore the attributes of the NavigationRail widget and provide a detailed example to showcase its implementation.
The NavigationRail widget provides a vertical navigation bar, typically positioned on the side of the screen. It's commonly used for lateral navigation, allowing users to seamlessly switch between various sections or functionalities within the app.
Attributes of NavigationRail Widget:
destinations: A list of NavigationRailDestination objects representing each navigation item. Each destination typically includes an icon and a label.
selectedIndex: Specifies the index of the currently selected navigation item. The navigation rail highlights the selected item, indicating the active section.
onDestinationSelected: A callback function triggered when a navigation item is selected. Developers can use this callback to update the state and manage the content to display based on the selected item.
Full Example of NavigationRail Widget:
Let's dive into a comprehensive example where we create a Flutter app with a NavigationRail containing three destinations: Home, Search, and Profile. Each destination is associated with a corresponding screen, and tapping on a destination triggers the display of the respective screen.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _selectedIndex = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter NavigationRail Example'),
),
body: Row(
children: [
NavigationRail(
destinations: [
NavigationRailDestination(icon: Icon(Icons.home), label: Text('Home')),
NavigationRailDestination(icon: Icon(Icons.search), label: Text('Search')),
NavigationRailDestination(icon: Icon(Icons.person), label: Text('Profile')),
],
selectedIndex: _selectedIndex,
onDestinationSelected: (index) {
setState(() {
_selectedIndex = index;
});
},
),
VerticalDivider(thickness: 1, width: 1),
// Display the content based on the selected index
Expanded(
child: _getScreen(_selectedIndex),
),
],
),
);
}
Widget _getScreen(int index) {
switch (index) {
case 0:
return Center(child: Text('Home Screen'));
case 1:
return Center(child: Text('Search Screen'));
case 2:
return Center(child: Text('Profile Screen'));
default:
return Container();
}
}
}
In this example:
We create a Flutter app with a Scaffold containing an AppBar and a horizontal Row with a NavigationRail and a content area.
The NavigationRail has three destinations: Home, Search, and Profile.
Tapping on a destination updates the _selectedIndex state, triggering the display of the corresponding screen in the content area.
The _getScreen method returns the appropriate screen based on the selected index.
By incorporating the NavigationRail widget, developers can provide users with an efficient and visually appealing means of navigating through different sections of their Flutter applications. Feel free to customize the icons, labels, and content to suit the design and requirements of your app.