Skip to content

Instantly share code, notes, and snippets.

@Bryanmuloni
Created January 12, 2021 03:51
Show Gist options
  • Save Bryanmuloni/47e11badc04e1222ef7040033b1525f4 to your computer and use it in GitHub Desktop.
Save Bryanmuloni/47e11badc04e1222ef7040033b1525f4 to your computer and use it in GitHub Desktop.
This gist demonstrates the use of the Flutter Navigation rail widget to implement a responsive side menu. Feel free to improve it.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(title: 'Events'),
);
}
}
class HomePage extends StatefulWidget {
HomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int _selectedIndex = 0;
bool _extended = false;
List<NavigationRailDestination> _buildDestinations() {
return [
NavigationRailDestination(
icon: Icon(
Icons.drafts_rounded,
color: Colors.black87,
),
selectedIcon: Icon(
Icons.drafts_rounded,
color: Colors.indigoAccent,
),
label: Text('Saved Drafts'),
),
NavigationRailDestination(
icon: Icon(
Icons.pending_rounded,
color: Colors.black87,
),
selectedIcon: Icon(
Icons.pending_rounded,
color: Colors.indigoAccent,
),
label: Text('Pending'),
),
NavigationRailDestination(
icon: Icon(
Icons.done_rounded,
color: Colors.black87,
),
selectedIcon: Icon(
Icons.done_rounded,
color: Colors.indigoAccent,
),
label: Text('Review'),
),
NavigationRailDestination(
icon: Icon(
Icons.done_all_rounded,
color: Colors.black87,
),
selectedIcon: Icon(
Icons.done_all_rounded,
color: Colors.indigoAccent,
),
label: Text('Validated'),
),
NavigationRailDestination(
icon: Icon(
Icons.check_circle_rounded,
color: Colors.black87,
),
selectedIcon: Icon(
Icons.check_circle_rounded,
color: Colors.indigoAccent,
),
label: Text('Approved'),
),
NavigationRailDestination(
icon: Icon(
Icons.cancel_rounded,
color: Colors.black87,
),
selectedIcon: Icon(
Icons.cancel_rounded,
color: Colors.indigoAccent,
),
label: Text('Rejected'),
),
];
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Container(
width: MediaQuery.of(context).size.width,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Expanded(
child: Row(
children: [
Expanded(
flex:3,
child: Row(
children: [
NavigationRail(
selectedLabelTextStyle: TextStyle(
color: Colors.indigoAccent,
fontSize: 14,
letterSpacing: 1,
decorationThickness: 2.0,
),
unselectedLabelTextStyle: TextStyle(
color: Colors.black87,
fontSize: 13,
letterSpacing: 0.8,
),
leading: _extended
? Padding(
padding:
const EdgeInsets.only(left: 16.0, right: 16.0, top: 8.0, bottom: 16.0),
child: FloatingActionButton.extended(
backgroundColor: Colors.indigoAccent,
onPressed: () {},
label: Text('Create Event'),
icon: Icon(Icons.create_new_folder_rounded),
),
)
: IconButton(
icon: Icon(
Icons.create_new_folder_rounded,
color: Colors.indigoAccent,
size: 40,
),
onPressed: () {}),
trailing: Padding(
padding: const EdgeInsets.all(16.0),
child: Container(
alignment: Alignment.bottomRight,
child: IconButton(
onPressed: () {
setState(() {
_extended = !_extended;
});
},
icon: _extended
? Icon(
Icons.arrow_back_ios_rounded,
color: Colors.indigoAccent,
)
: Icon(
Icons.arrow_back_ios_rounded,
color: Colors.black87,
),
)),
),
extended: _extended,
minExtendedWidth: 200,
elevation: 2.0,
// backgroundColor: Color.fromRGBO(32, 176, 44, 0.96),
labelType: NavigationRailLabelType.none,
selectedIndex: _selectedIndex,
destinations: _buildDestinations(),
onDestinationSelected: (int index) {
setState(() {
_selectedIndex = index;
});
},
),
VerticalDivider(),
Expanded(
child: _renderMainContent(_selectedIndex),
),
],
),
),
Expanded(flex:1,child: Container(color: Colors.black54,child: Column(children: [
Text("Event Detail Overview",style: TextStyle(color: Colors.white),)
],),)),
],
),
),
],
),
));
}
Widget _renderMainContent(int index) {
switch (index) {
case 0:
return Column(
children: [
Container(
child: Text('Saved Drafts'),
),
],
);
break;
case 1:
return Column(
children: [
Container(
child: Text('Pending'),
),
],
);
break;
case 2:
return Column(
children: [
Container(
child: Text('Review'),
),
],
);
break;
case 3:
return Column(
children: [
Container(
child: Text('Validated'),
),
],
);
break;
case 4:
return Column(
children: [
Container(
child: Text('Approved'),
),
],
);
break;
case 5:
return Column(
children: [
Container(
child: Text('Rejected'),
),
],
);
break;
default:
return Center(
child: Container(
child: Text("Something feels fishy! :P"),
),
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment