Skip to content
Snippets Groups Projects
Commit 737cd2fe authored by Park Tae hyeon's avatar Park Tae hyeon
Browse files

20250521

parent 539ce7ff
No related branches found
No related tags found
No related merge requests found
// main.dart
import 'package:english_words/english_words.dart';
import 'package:flutter/material.dart';
import 'package:namer_app/checklist_view.dart';
import 'package:namer_app/checklist_view.dart';
void main() {
runApp(const MyApp());
runApp(
ChangeNotifierProvider(
create: (context) => MyAppState(),
child: const MyApp(),
),
);
}
class MyApp extends StatelessWidget {
......@@ -13,12 +19,88 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
var appState = context.watch<MyAppState>();
return MaterialApp(
home: const MyHomePage(title: 'My Flutter App'),
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 const MyHomePage();
} else {
return const Center(child: Text('No favorites yet.'));
}
},
),
);
}
}
class MyAppstate extends ChangeNotifier {
var current = WordPair.random();
List<WordPair> favorites = [];
late Future<List<WordPair>> futureFavorites;
MyAppstate() {
futureFavorites = futureFavorites;
}
Future<List<WordPair>> fetchFavorites() async {
final response = await http.get(Uri.parse('https://example.com/favorites'));
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 {
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();
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
......
......@@ -75,6 +75,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:
......@@ -208,6 +224,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:
......@@ -224,6 +248,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "15.0.0"
web:
dependency: transitive
description:
name: web
sha256: "868d88a33d8a87b18ffc05f9f030ba328ffefba92d6c127917a2ba740f9cfe4a"
url: "https://pub.dev"
source: hosted
version: "1.1.1"
sdks:
dart: ">=3.7.0-0 <4.0.0"
flutter: ">=3.18.0-18.0.pre.54"
......@@ -14,6 +14,7 @@ dependencies:
english_words: ^4.0.0
provider: ^6.0.0
http: ^1.4.0
dev_dependencies:
flutter_test:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment