Added the Favorites button.
This commit is contained in:
parent
71b76ecb2d
commit
082dff1810
@ -26,13 +26,24 @@ class MyApp extends StatelessWidget {
|
||||
}
|
||||
|
||||
class MyAppState extends ChangeNotifier {
|
||||
var current = WordPair.random();// ...
|
||||
var current = WordPair.random();
|
||||
|
||||
// ↓ Add this.
|
||||
void getNext() {
|
||||
current = WordPair.random();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
var favorites = <WordPair>[];
|
||||
|
||||
void toggleFavorite() {
|
||||
if (favorites.contains(current)) {
|
||||
favorites.remove(current);
|
||||
} else {
|
||||
favorites.add(current);
|
||||
}
|
||||
notifyListeners();
|
||||
print("Favorites are: ${favorites.toString()}");
|
||||
}
|
||||
}
|
||||
|
||||
class MyHomePage extends StatelessWidget {
|
||||
@ -41,6 +52,13 @@ class MyHomePage extends StatelessWidget {
|
||||
var appState = context.watch<MyAppState>();
|
||||
var pair = appState.current;
|
||||
|
||||
IconData icon;
|
||||
if (appState.favorites.contains(pair)) {
|
||||
icon = Icons.favorite;
|
||||
} else {
|
||||
icon = Icons.favorite_border;
|
||||
}
|
||||
|
||||
return Scaffold(
|
||||
body: Center(
|
||||
child: Column(
|
||||
@ -48,11 +66,26 @@ class MyHomePage extends StatelessWidget {
|
||||
children: [
|
||||
BigCard(pair: pair),
|
||||
SizedBox(height: 10.0), // Just a spacer.
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
appState.getNext();
|
||||
},
|
||||
child: Text('Next'),
|
||||
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'),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
|
Loading…
x
Reference in New Issue
Block a user