Skip to content

Commit

Permalink
[Feat] Connect server api (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
kminji127 committed Mar 17, 2023
1 parent 248da85 commit 32f44cb
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 146 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Environment variable
*.env

# Miscellaneous
*.class
*.log
Expand Down
2 changes: 2 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:i_can_read/screens/home_screen.dart';
import 'package:i_can_read/screens/camera/camera_screen.dart';
import 'package:i_can_read/screens/recommend/recommend_result.dart';
Expand All @@ -17,6 +18,7 @@ Future<void> main() async {
} on CameraException catch (e) {
print('Error in fetching the cameras: $e');
}
await dotenv.load(fileName: '.env');
runApp(const MyApp());
}

Expand Down
2 changes: 1 addition & 1 deletion lib/screens/camera/display_picture_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class DisplayPictureScreen extends StatelessWidget {
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => ResultScreen(),
builder: (context) => ResultScreen(imagePath: imagePath),
)
);
},
Expand Down
62 changes: 46 additions & 16 deletions lib/screens/camera/result_screen.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:i_can_read/configs/color.dart';
import 'package:i_can_read/configs/curve_painter.dart';
import 'package:i_can_read/models/menu.dart';
import 'package:i_can_read/models/menu_repository.dart';
import 'package:i_can_read/screens/camera/detailed_result_true.dart';

class ResultScreen extends StatelessWidget {
ResultScreen({super.key});
// final List<Menu> menus = MenuRepository().getMenu();
Future<List<Menu>> fetchMenu() async {
await Future.delayed(const Duration(seconds: 5));
return MenuRepository().getMenu();
final String imagePath;
ResultScreen({super.key, required this.imagePath});

Future<List<Menu>> getMenu() async {
final formData = FormData.fromMap({
'file': await MultipartFile.fromFile(imagePath),
});
try {
final response = await Dio().post('${dotenv.env['BASE_URL']}/api/v1/menu/list', data: formData);
if (response.statusCode == 200) {
return response.data.map<Menu>(
(item) =>
Menu(
name: item['name'],
description: item['meaning'],
image: item['image'],
),
).toList();
} else {
return Future.error("Error");
}
} on DioError catch (e) {
print(e);
return Future.error(e.toString());
}
}

@override
Expand All @@ -23,18 +44,27 @@ class ResultScreen extends StatelessWidget {
iconTheme: const IconThemeData(color: Colors.black),
),
body: FutureBuilder<List<Menu>>(
future: fetchMenu(),
future: getMenu(),
builder: (context, snapshot) {
if (snapshot.hasData == false) {
if (snapshot.hasError) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Text('였λ₯˜κ°€ λ°œμƒν•˜μ˜€μŠ΅λ‹ˆλ‹€.'),
],
)
);
} else if (snapshot.hasData == false) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
CircularProgressIndicator(),
SizedBox(height: 20),
Text('μ΅œλŒ€ 1λΆ„ μ†Œμš”λ  수 μžˆμŠ΅λ‹ˆλ‹€.'),
],
)
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
CircularProgressIndicator(),
SizedBox(height: 20),
Text('μ΅œλŒ€ 1λΆ„ μ†Œμš”λ  수 μžˆμŠ΅λ‹ˆλ‹€.'),
],
)
);
} else {
return CustomPaint(
Expand Down
Loading

0 comments on commit 32f44cb

Please sign in to comment.