diff --git a/lib/main.dart b/lib/main.dart index 6507140f916806df6ba7030c4d1accfd8ccd2504..2bfebdbcfd253029604cd7a6166c851cdc791400 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -47,48 +47,87 @@ class MyAppState extends ChangeNotifier { // ... +// ... + +// ... + +// ... + class MyHomePage extends StatefulWidget { @override State<MyHomePage> createState() => _MyHomePageState(); } +// ... + class _MyHomePageState extends State<MyHomePage> { + + var selectedIndex = 0; // ← Add this property. + @override Widget build(BuildContext context) { - return Scaffold( - body: Row( - children: [ - SafeArea( - child: NavigationRail( - extended: false, - destinations: [ - NavigationRailDestination( - icon: Icon(Icons.home), - label: Text('Home'), + + // ... + +Widget page; +switch (selectedIndex) { + case 0: + page = GeneratorPage(); + break; + case 1: + page = FavoritesPage(); + break; + default: + throw UnimplementedError('no widget for $selectedIndex'); +} + +// ... + + return LayoutBuilder( + builder: (context,constrains) { + return Scaffold( + body: Row( + children: [ + SafeArea( + child: NavigationRail( + extended: constrains.maxWidth >= 600, + destinations: [ + NavigationRailDestination( + icon: Icon(Icons.home), + label: Text('Home'), + ), + NavigationRailDestination( + icon: Icon(Icons.favorite), + label: Text('Favorites'), + ), + ], + selectedIndex: selectedIndex, // ← Change to this. + onDestinationSelected: (value) { + + // ↓ Replace print with this. + setState(() { + selectedIndex = value; + }); + + }, ), - NavigationRailDestination( - icon: Icon(Icons.favorite), - label: Text('Favorites'), + ), + Expanded( + child: Container( + color: Theme.of(context).colorScheme.primaryContainer, + child: page, ), - ], - selectedIndex: 0, - onDestinationSelected: (value) { - print('selected: $value'); - }, - ), - ), - Expanded( - child: Container( - color: Theme.of(context).colorScheme.primaryContainer, - child: GeneratorPage(), - ), + ), + ], ), - ], - ), + ); + } ); } } +// ... + class GeneratorPage extends StatelessWidget { @override Widget build(BuildContext context) { @@ -135,6 +174,37 @@ class GeneratorPage extends StatelessWidget { // ... +class FavoritesPage extends StatelessWidget { + @override + Widget build(BuildContext context) { + var appState = context.watch<MyAppState>(); + + if (appState.favorites.isEmpty) { + return Center( + child: Text('No favorites yet.'), + ); + } + + return ListView( + children: [ + Padding( + padding: const EdgeInsets.all(20), + child: Text('You have ' + '${appState.favorites.length} favorites:'), + ), + for (var pair in appState.favorites) + ListTile( + leading: Icon(Icons.favorite), + title: Text(pair.asLowerCase), + ), + ], + ); + } +} +// ... + +// ... + // ... class BigCard extends StatelessWidget {