From 97e5229cee0fdf717e58eacb4902568c882ddf01 Mon Sep 17 00:00:00 2001 From: jhm991231 <jhm991231@ajou.ac.kr> Date: Wed, 21 May 2025 17:23:51 +0900 Subject: [PATCH] =?UTF-8?q?=ED=86=B5=EC=8B=A0=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/main.dart | 85 +++++++++++++++++++++++++++++++++++++++++++-------- pubspec.lock | 32 +++++++++++++++++++ pubspec.yaml | 4 +-- 3 files changed, 107 insertions(+), 14 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 1e93b3a..7943d1f 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,9 +1,17 @@ +import 'dart:convert'; + import 'package:english_words/english_words.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; +import 'package:http/http.dart' as http; void main() { - runApp(MyApp()); + runApp( + ChangeNotifierProvider( + create: (context) => MyAppState(), + child: const MyApp(), + ), + ); } class MyApp extends StatelessWidget { @@ -11,15 +19,26 @@ class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { - return ChangeNotifierProvider( - create: (context) => MyAppState(), - child: MaterialApp( - title: 'Namer App', - theme: ThemeData( - useMaterial3: true, - colorScheme: ColorScheme.fromSeed(seedColor: Colors.lightBlueAccent), - ), - home: MyHomePage(), + var appState = context.watch<MyAppState>(); + return MaterialApp( + title: 'Namer App', + theme: ThemeData( + useMaterial3: true, + colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepOrange), + ), + home: FutureBuilder<List<WordPair>>( + future: appState.futureFavorites, + builder: (context, snapshot) { + if (snapshot.connectionState == ConnectionState.waiting) { + return const Center(child: CircularProgressIndicator()); + } else if (snapshot.hasError) { + return Center(child: Text('Error: ${snapshot.error}')); + } else if (snapshot.hasData) { + return MyHomePage(); + } else { + return const Center(child: Text('No favorites yet.')); + } + }, ), ); } @@ -27,6 +46,11 @@ class MyApp extends StatelessWidget { class MyAppState extends ChangeNotifier { var current = WordPair.random(); + late Future<List<WordPair>> futureFavorites; + + MyAppState() { + futureFavorites = fetchFavorites(); + } void getNext() { current = WordPair.random(); @@ -35,12 +59,49 @@ class MyAppState extends ChangeNotifier { var favorites = <WordPair>[]; - void toggleFavorite() { + Future<List<WordPair>> fetchFavorites() async { + final response = await http.get(Uri.parse("http://localhost:3000/likes")); + List<WordPair> list = []; + var jsonData = jsonDecode(response.body); + + for (Map<String, dynamic> obj in jsonData) { + List<String> res = obj["id"].split('_'); + list.add(WordPair(res[0], res[1])); + } + + favorites = [...list]; + notifyListeners(); + return list; + } + + Future<void> toggleFavorite() async { + var uri = "http://localhost:3000/likes/${current.first}_${current.second}"; if (favorites.contains(current)) { + final http.Response resp = await http.delete(Uri.parse(uri)); + if (resp.statusCode != 200) { + return; + } + favorites.remove(current); + favorites = [...favorites]; } else { - favorites.add(current); + final http.Response resp = await http.post( + Uri.parse(uri), + headers: <String, String>{ + 'Content-Type': 'application/json; charset=UTF-8', + }, + body: jsonEncode(<String, dynamic>{ + 'collectionId': "${current.first}_${current.second}", + 'like': true, + }), + ); + if (resp.statusCode != 200) { + return; + } + + favorites = [...favorites, current]; } + print(favorites); notifyListeners(); } } diff --git a/pubspec.lock b/pubspec.lock index f5cdc47..d32f44f 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -83,6 +83,22 @@ packages: description: flutter source: sdk version: "0.0.0" + http: + dependency: "direct main" + description: + name: http + sha256: "2c11f3f94c687ee9bad77c171151672986360b2b001d109814ee7140b2cf261b" + url: "https://pub.dev" + source: hosted + version: "1.4.0" + http_parser: + dependency: transitive + description: + name: http_parser + sha256: "178d74305e7866013777bab2c3d8726205dc5a4dd935297175b19a23a2e66571" + url: "https://pub.dev" + source: hosted + version: "4.1.2" leak_tracker: dependency: transitive description: @@ -216,6 +232,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.7.4" + typed_data: + dependency: transitive + description: + name: typed_data + sha256: f9049c039ebfeb4cf7a7104a675823cd72dba8297f264b6637062516699fa006 + url: "https://pub.dev" + source: hosted + version: "1.4.0" vector_math: dependency: transitive description: @@ -232,6 +256,14 @@ packages: url: "https://pub.dev" source: hosted version: "14.3.1" + web: + dependency: transitive + description: + name: web + sha256: "868d88a33d8a87b18ffc05f9f030ba328ffefba92d6c127917a2ba740f9cfe4a" + url: "https://pub.dev" + source: hosted + version: "1.1.1" sdks: dart: ">=3.7.2 <4.0.0" flutter: ">=3.18.0-18.0.pre.54" diff --git a/pubspec.yaml b/pubspec.yaml index 99a7aa3..3755283 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: my_first_flutter -description: "A new Flutter project." +description: 'A new Flutter project.' # The following line prevents the package from being accidentally published to # pub.dev using `flutter pub publish`. This is preferred for private packages. publish_to: 'none' # Remove this line if you wish to publish to pub.dev @@ -36,6 +36,7 @@ dependencies: cupertino_icons: ^1.0.8 english_words: any provider: any + http: ^1.4.0 dev_dependencies: flutter_test: @@ -53,7 +54,6 @@ dev_dependencies: # The following section is specific to Flutter packages. flutter: - # The following line ensures that the Material Icons font is # included with your application, so that you can use the icons in # the material Icons class. -- GitLab