-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera_before_eat.dart
182 lines (172 loc) · 6.39 KB
/
camera_before_eat.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:firebase_storage/firebase_storage.dart' as firebase_storage;
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:intl/intl.dart';
// import 'home.dart';
// import 'package:path/path.dart';
class GetImageBeforeEat extends StatefulWidget {
const GetImageBeforeEat({super.key});
@override
State<GetImageBeforeEat> createState() => _GetImageBeforeEatState();
}
class _GetImageBeforeEatState extends State<GetImageBeforeEat> {
// File _image = File('');
XFile? selectedImage;
firebase_storage.Reference? storageReference;
String formattedDate = DateFormat('yyyyMMddHHmmss').format(DateTime.now());
@override
void initState() {
super.initState();
initializeFirebase();
}
Future<void> initializeFirebase() async {
await Firebase.initializeApp();
storageReference = firebase_storage.FirebaseStorage.instance.ref();
}
// 이미지 storage에 업로드
Future<String?> uploadImage(File image) async {
try {
DatabaseReference urlReference = FirebaseDatabase.instance.ref();
String fileName = DateTime.now().millisecondsSinceEpoch.toString();
firebase_storage.Reference reference =
storageReference!.child('images/beforeEat/$fileName');
firebase_storage.UploadTask uploadTask = reference.putFile(image);
firebase_storage.TaskSnapshot taskSnapshot = await uploadTask;
String downloadURLbeforeEat = await taskSnapshot.ref.getDownloadURL();
//url도 realtime에 업로드
await urlReference
.child(formattedDate)
.child('foodURL')
.set(downloadURLbeforeEat);
return downloadURLbeforeEat;
} catch (e) {
debugPrint('Error uploading image: $e');
return null;
}
}
@override
Widget build(BuildContext context) {
var imagePicker = ImagePicker();
return Scaffold(
backgroundColor: Colors.white,
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 80),
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
if (selectedImage != null)
ClipRRect(
child: Image.asset(
selectedImage!.path,
width: 350,
fit: BoxFit.cover,
),
),
const SizedBox(height: 70),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextButton(
style: TextButton.styleFrom(
backgroundColor: Colors.white,
side: const BorderSide(color: Colors.black),
fixedSize: const Size(150, 50),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
onPressed: () async {
var cameraImage = await imagePicker.pickImage(
source: ImageSource.camera,
);
if (cameraImage != null) {
debugPrint('Image Selected');
selectedImage = cameraImage;
setState(() {});
} else {
debugPrint('Nothing Selected');
}
},
child: const Text(
'Camera',
style: TextStyle(
color: Colors.black,
fontSize: 20,
),
),
),
const SizedBox(width: 20),
TextButton(
onPressed: () async {
var galleryImage = await imagePicker.pickImage(
source: ImageSource.gallery,
);
if (galleryImage != null) {
debugPrint('Image Selected');
selectedImage = galleryImage;
setState(() {});
} else {
debugPrint('Nothing Selected');
}
},
style: TextButton.styleFrom(
backgroundColor: Colors.white,
side: const BorderSide(color: Colors.black),
fixedSize: const Size(150, 50),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
child: const Text(
'Gallery',
style: TextStyle(
color: Colors.black,
fontSize: 20,
),
),
),
],
),
const SizedBox(height: 20),
TextButton(
onPressed: () async {
if (selectedImage != null) {
String? downloadURLbeforeEat =
await uploadImage(File(selectedImage!.path));
if (downloadURLbeforeEat != null) {
debugPrint(
'Image uploaded. Download URL: $downloadURLbeforeEat');
//Navigator.pop(context, downloadURLbeforeEat);
Navigator.pop(
context, [downloadURLbeforeEat, formattedDate]);
}
} else {
debugPrint('No image selected');
}
},
style: TextButton.styleFrom(
backgroundColor: const Color(0xff49c14e),
fixedSize: const Size(320, 50),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
child: const Text(
'Upload',
style: TextStyle(
fontSize: 20,
color: Colors.white,
),
),
),
],
),
),
),
);
}
}