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 {
|
class MyAppState extends ChangeNotifier {
|
||||||
var current = WordPair.random();// ...
|
var current = WordPair.random();
|
||||||
|
|
||||||
// ↓ Add this.
|
|
||||||
void getNext() {
|
void getNext() {
|
||||||
current = WordPair.random();
|
current = WordPair.random();
|
||||||
notifyListeners();
|
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 {
|
class MyHomePage extends StatelessWidget {
|
||||||
@ -41,6 +52,13 @@ class MyHomePage extends StatelessWidget {
|
|||||||
var appState = context.watch<MyAppState>();
|
var appState = context.watch<MyAppState>();
|
||||||
var pair = appState.current;
|
var pair = appState.current;
|
||||||
|
|
||||||
|
IconData icon;
|
||||||
|
if (appState.favorites.contains(pair)) {
|
||||||
|
icon = Icons.favorite;
|
||||||
|
} else {
|
||||||
|
icon = Icons.favorite_border;
|
||||||
|
}
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
body: Center(
|
body: Center(
|
||||||
child: Column(
|
child: Column(
|
||||||
@ -48,6 +66,19 @@ class MyHomePage extends StatelessWidget {
|
|||||||
children: [
|
children: [
|
||||||
BigCard(pair: pair),
|
BigCard(pair: pair),
|
||||||
SizedBox(height: 10.0), // Just a spacer.
|
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(
|
ElevatedButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
appState.getNext();
|
appState.getNext();
|
||||||
@ -56,6 +87,8 @@ class MyHomePage extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user