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',
theme: ThemeData(
useMaterial3: true,
colorScheme: ColorScheme.fromSeed(seedColor: Colors.green),
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepOrange),
),
home: MyHomePage(),
),
@@ -27,11 +27,21 @@ class MyApp extends StatelessWidget {
class MyAppState extends ChangeNotifier {
var current = WordPair.random();
var favourites = <WordPair>[];
void getNext() {
current = WordPair.random();
notifyListeners();
}
void toggleFavourites() {
if (favourites.contains(current)) {
favourites.remove(current);
} else {
favourites.add(current);
}
notifyListeners();
}
}
class BigCard extends StatelessWidget {
@@ -53,23 +63,82 @@ class BigCard extends StatelessWidget {
color: theme.colorScheme.primary,
child: Padding(
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 {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Row(
children: [
SafeArea(
child: NavigationRail(
extended: true,
destinations: [
NavigationRailDestination(
icon: Icon(Icons.home),
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;
return Scaffold(
body: Column(
IconData icon;
if (appState.favourites.contains(pair)) {
icon = Icons.favorite;
} else {
icon = Icons.favorite_border;
}
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('A random idea:'),
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();
@@ -78,6 +147,10 @@ class MyHomePage extends StatelessWidget {
),
],
),
],
),
);
}
}
// ...