From ab0b551a6419924512b9c9618d8f0ba4ecbd7c66 Mon Sep 17 00:00:00 2001 From: Hyperling Date: Thu, 30 Jan 2025 12:18:58 -0700 Subject: [PATCH] Add navigation with expanding sidebar. --- namer_app/lib/main.dart | 122 ++++++++++++++++++++++++++++++---------- 1 file changed, 91 insertions(+), 31 deletions(-) diff --git a/namer_app/lib/main.dart b/namer_app/lib/main.dart index 65cf4eb..aa40d63 100644 --- a/namer_app/lib/main.dart +++ b/namer_app/lib/main.dart @@ -46,7 +46,71 @@ class MyAppState extends ChangeNotifier { } } -class MyHomePage extends StatelessWidget { +class MyHomePage extends StatefulWidget { + @override + State createState() => _MyHomePageState(); +} + +class _MyHomePageState extends State { + + var selectedIndex = 0; + + @override + Widget build(BuildContext context) { + + Widget page; + switch (selectedIndex) { + case 0: + page = GeneratorPage(); + break; + case 1: + page = Placeholder(); + break; + default: + throw UnimplementedError('no widget for $selectedIndex'); + } + + return LayoutBuilder( + builder: (context, constraints) { + return Scaffold( + body: Row( + children: [ + SafeArea( + child: NavigationRail( + extended: constraints.maxWidth >= 600, + destinations: [ + NavigationRailDestination( + icon: Icon(Icons.home), + label: Text('Home'), + ), + NavigationRailDestination( + icon: Icon(Icons.favorite), + label: Text('Favorites'), + ), + ], + selectedIndex: selectedIndex, + onDestinationSelected: (value) { + setState(() { + selectedIndex = value; + }); + } + ), + ), + Expanded( + child: Container( + color: Theme.of(context).colorScheme.primaryContainer, + child: page, + ), + ), + ], + ), + ); + } + ); + } +} + +class GeneratorPage extends StatelessWidget { @override Widget build(BuildContext context) { var appState = context.watch(); @@ -59,36 +123,32 @@ class MyHomePage extends StatelessWidget { icon = Icons.favorite_border; } - return Scaffold( - body: Center( - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - BigCard(pair: pair), - SizedBox(height: 10.0), // Just a spacer. - Row( - mainAxisSize: MainAxisSize.min, - children: [ - ElevatedButton.icon( - onPressed: () { - appState.toggleFavorite(); - }, - icon: Icon(icon), - label: Text('Favorite'), - ), - - SizedBox(width: 10.0), - - ElevatedButton( - onPressed: () { - appState.getNext(); - }, - child: Text('Next'), - ), - ], - ), - ], - ), + return Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + BigCard(pair: pair), + SizedBox(height: 10), + Row( + mainAxisSize: MainAxisSize.min, + children: [ + ElevatedButton.icon( + onPressed: () { + appState.toggleFavorite(); + }, + icon: Icon(icon), + label: Text('Like'), + ), + SizedBox(width: 10), + ElevatedButton( + onPressed: () { + appState.getNext(); + }, + child: Text('Next'), + ), + ], + ), + ], ), ); }