Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/#16-api #21

Merged
merged 1 commit into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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