diff --git a/.github/workflows/flutter_application_1/assign/lib/main.dart b/.github/workflows/flutter_application_1/assign/lib/main.dart index ce149e53284699d9ceb9173bd38af3e8efeffc31..4251d2ca8121df2e78b6a64409f05a194034248f 100644 --- a/.github/workflows/flutter_application_1/assign/lib/main.dart +++ b/.github/workflows/flutter_application_1/assign/lib/main.dart @@ -27,11 +27,23 @@ class MyApp extends StatelessWidget { class MyAppState extends ChangeNotifier { var current = WordPair.random(); - // ↓ Add this. + void getNext() { current = WordPair.random(); notifyListeners(); } + + // ↓ Add the code below. + var favorites = <WordPair>[]; + + void toggleFavorite() { + if (favorites.contains(current)) { + favorites.remove(current); + } else { + favorites.add(current); + } + notifyListeners(); + } } class MyHomePage extends StatelessWidget { @@ -40,6 +52,14 @@ class MyHomePage extends StatelessWidget { var appState = context.watch<MyAppState>(); var pair = appState.current; + // ↓ Add this. + IconData icon; + if (appState.favorites.contains(pair)) { + icon = Icons.favorite; + } else { + icon = Icons.favorite_border; + } + return Scaffold( body: Center( child: Column( @@ -47,13 +67,28 @@ class MyHomePage extends StatelessWidget { children: [ BigCard(pair: pair), SizedBox(height: 10), - ElevatedButton( - onPressed: () { - appState.getNext(); - }, - child: Text('Next'), + Row( + mainAxisSize: MainAxisSize.min, + children: [ + + // ↓ And this. + ElevatedButton.icon( + onPressed: () { + appState.toggleFavorite(); + }, + icon: Icon(icon), + label: Text('Like'), + ), + SizedBox(width: 10), + + ElevatedButton( + onPressed: () { + appState.getNext(); + }, + child: Text('Next'), + ), + ], ), - ], ), ),