Instantly share code, notes, and snippets.
Last active
January 21, 2023 22:08
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save mihalycsaba/fcd39cbd2289847918408ece61b8d525 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(const MyApp()); | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData( | |
colorSchemeSeed: const Color(0xff6750a4), useMaterial3: true), | |
home: const NavRailExample(), | |
); | |
} | |
} | |
class NavRailExample extends StatefulWidget { | |
const NavRailExample({super.key}); | |
@override | |
State<NavRailExample> createState() => _NavRailExampleState(); | |
} | |
class _NavRailExampleState extends State<NavRailExample> { | |
int _selectedIndex = 0; | |
NavigationRailLabelType labelType = NavigationRailLabelType.all; | |
bool showLeading = false; | |
bool showTrailing = false; | |
double groupAligment = -1.0; | |
bool _isClosed = false; | |
Widget _getWidget(int index) { | |
switch (index) { | |
case 1: | |
return GestureDetector( | |
child: const Text('Tap!'), | |
onTap: () => setState(() { | |
_isClosed = true; | |
}), | |
); | |
case 2: | |
return const Text('empty'); | |
default: | |
return ListView( | |
children: const [ | |
ExpansionTile( | |
title: Text('whatev'), | |
children: [Text('1'), Text('2')], | |
), | |
ListTile( | |
title: Text('adfafdafaf'), | |
) | |
], | |
); | |
} | |
} | |
Widget _getPage(int index) { | |
switch (index) { | |
case 1: | |
return const Center(child: Text('sheeesh')); | |
case 2: | |
return const Center(child: Text('empty')); | |
default: | |
return const Center(child: Text('yolo'),); | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: SafeArea( | |
child: Row( | |
children: <Widget>[ | |
NavigationRail( | |
selectedIndex: _selectedIndex, | |
groupAlignment: groupAligment, | |
onDestinationSelected: (int index) { | |
setState(() { | |
_isClosed = (_selectedIndex == index || _isClosed) | |
? !_isClosed | |
: _isClosed; | |
_selectedIndex = index; | |
}); | |
}, | |
labelType: labelType, | |
leading: showLeading | |
? FloatingActionButton( | |
elevation: 0, | |
onPressed: () { | |
// Add your onPressed code here! | |
}, | |
child: const Icon(Icons.add), | |
) | |
: const SizedBox(), | |
trailing: showTrailing | |
? IconButton( | |
onPressed: () { | |
// Add your onPressed code here! | |
}, | |
icon: const Icon(Icons.more_horiz_rounded), | |
) | |
: const SizedBox(), | |
destinations: const <NavigationRailDestination>[ | |
NavigationRailDestination( | |
icon: Icon(Icons.favorite_border), | |
selectedIcon: Icon(Icons.favorite), | |
label: Text('First'), | |
), | |
NavigationRailDestination( | |
icon: Icon(Icons.bookmark_border), | |
selectedIcon: Icon(Icons.book), | |
label: Text('Second'), | |
), | |
NavigationRailDestination( | |
icon: Icon(Icons.star_border), | |
selectedIcon: Icon(Icons.star), | |
label: Text('Third'), | |
), | |
], | |
), | |
Visibility( | |
maintainState: false, | |
visible: !_isClosed, | |
child: Row( | |
children: [ | |
const VerticalDivider(thickness: 1, width: 1), | |
SizedBox( | |
height: double.infinity, | |
width: 200, | |
child: _getWidget(_selectedIndex), | |
) | |
], | |
), | |
), | |
const VerticalDivider(thickness: 1, width: 1), | |
// This is the main content. | |
Expanded( | |
child: _getPage(_selectedIndex), | |
), | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment