From 7f006f21f8a0eb8a5e1ebcf5857a207f9f0dd9ba Mon Sep 17 00:00:00 2001 From: RyuZU <dbrua1222@naver.com> Date: Wed, 5 Jun 2024 18:54:33 +0900 Subject: [PATCH] 6 --- lib/main.dart | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/main.dart b/lib/main.dart index 1394672..b4643af 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -32,6 +32,18 @@ class MyAppState extends ChangeNotifier { current = WordPair.random(); notifyListeners(); } + + 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; + IconData icon; + if(appState.favorites.contains(pair)){ + icon = Icons.favorite; + } + else{ + icon = Icons.favorite_border; + } + return Scaffold( body: Center( child: Column( @@ -48,7 +68,13 @@ class MyHomePage extends StatelessWidget { // Text('A random AWESOME idea:'), BigCard(pair: pair), SizedBox(height: 10,), - ElevatedButton(onPressed: (){appState.getNext();}, child: Text('Next'),), + Row( + mainAxisSize: MainAxisSize.min, + children: [ + ElevatedButton.icon(onPressed: (){appState.toggleFavorite();}, icon:Icon(icon), label: Text('Like'),), + ElevatedButton(onPressed: (){appState.getNext();}, child: Text('Next'),), + ], + ), ], ), ), -- GitLab