From 9c100443878276d62d69ae12431fd6a82b3347b7 Mon Sep 17 00:00:00 2001 From: unknown <asdfqwer1234@ajou.ac.kr> Date: Tue, 24 Sep 2024 18:13:41 +0900 Subject: [PATCH] codelab6 --- lib/main.dart | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 0007bda..3e22e69 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -32,6 +32,17 @@ 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,19 +51,39 @@ 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( mainAxisAlignment: MainAxisAlignment.center, children: [ - 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'), + ), + SizedBox(width: 10), + + ElevatedButton( + onPressed: () { + appState.getNext(); + }, + child: Text('Next'), + ), + ], ), ], ), -- GitLab