Add navigation bar

This commit is contained in:
2025-05-01 20:56:00 +02:00
parent 15c296d9a3
commit 88f0b8d4d1

View File

@@ -17,7 +17,7 @@ class MyApp extends StatelessWidget {
title: 'Namer App', title: 'Namer App',
theme: ThemeData( theme: ThemeData(
useMaterial3: true, useMaterial3: true,
colorScheme: ColorScheme.fromSeed(seedColor: Colors.green), colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepOrange),
), ),
home: MyHomePage(), home: MyHomePage(),
), ),
@@ -27,11 +27,21 @@ class MyApp extends StatelessWidget {
class MyAppState extends ChangeNotifier { class MyAppState extends ChangeNotifier {
var current = WordPair.random(); var current = WordPair.random();
var favourites = <WordPair>[];
void getNext() { void getNext() {
current = WordPair.random(); current = WordPair.random();
notifyListeners(); notifyListeners();
} }
void toggleFavourites() {
if (favourites.contains(current)) {
favourites.remove(current);
} else {
favourites.add(current);
}
notifyListeners();
}
} }
class BigCard extends StatelessWidget { class BigCard extends StatelessWidget {
@@ -53,31 +63,94 @@ class BigCard extends StatelessWidget {
color: theme.colorScheme.primary, color: theme.colorScheme.primary,
child: Padding( child: Padding(
padding: const EdgeInsets.all(20.0), padding: const EdgeInsets.all(20.0),
child: Text(pair.asLowerCase, style: style), child: Text(pair.asLowerCase,
semanticsLabel: "${pair.first} ${pair.second}", style: style),
), ),
); );
} }
} }
// ...
class MyHomePage extends StatelessWidget { class MyHomePage extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var appState = context.watch<MyAppState>();
var pair = appState.current;
return Scaffold( return Scaffold(
body: Column( body: Row(
children: [ children: [
Text('A random idea:'), SafeArea(
BigCard(pair: pair), child: NavigationRail(
ElevatedButton( extended: true,
onPressed: () { destinations: [
appState.getNext(); NavigationRailDestination(
}, icon: Icon(Icons.home),
child: Text('Next'), label: Text('Home'),
),
NavigationRailDestination(
icon: Icon(Icons.favorite),
label: Text('Favorites'),
),
],
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) {
var appState = context.watch<MyAppState>();
var pair = appState.current;
IconData icon;
if (appState.favourites.contains(pair)) {
icon = Icons.favorite;
} else {
icon = Icons.favorite_border;
}
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
BigCard(pair: pair),
SizedBox(height: 10),
Row(
mainAxisSize: MainAxisSize.min,
children: [
ElevatedButton.icon(
onPressed: () {
appState.toggleFavourites();
},
icon: Icon(icon),
label: Text('Like'),
),
SizedBox(width: 10),
ElevatedButton(
onPressed: () {
appState.getNext();
},
child: Text('Next'),
),
],
),
],
),
);
}
}
// ...