-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera_after_eat.dart
178 lines (168 loc) · 6.35 KB
/
camera_after_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
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';
class GetImageAfterEat extends StatefulWidget {
const GetImageAfterEat({super.key});
@override
State<GetImageAfterEat> createState() => _GetImageAfterEatState();
}
class _GetImageAfterEatState extends State<GetImageAfterEat> {
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();
}
// Upload the image to 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/afterEat/$fileName');
firebase_storage.UploadTask uploadTask = reference.putFile(image);
firebase_storage.TaskSnapshot takeSnapshot = await uploadTask;
String downloadURLafterEat = await takeSnapshot.ref.getDownloadURL();
await urlReference
.child('afterEat')
.child(formattedDate)
.set(downloadURLafterEat);
return downloadURLafterEat;
} catch (e) {
debugPrint('Error uploading After Eat image: $e');
return null;
}
}
@override
Widget build(BuildContext context) {
var imagePicker = ImagePicker();
return Scaffold(
backgroundColor: const Color(0xff49c14e),
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('After Eat Image Selected');
selectedImage = cameraImage;
setState(() {});
} else {
debugPrint('After Eat 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('After Eat Image Selected');
selectedImage = galleryImage;
setState(() {});
} else {
debugPrint('After Eat 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) {
// debugPrint('afterEat img exists');
String? downloadURLafterEat =
await uploadImage(File(selectedImage!.path));
if (downloadURLafterEat != null) {
debugPrint(
'After Eat Image uploaded. Download URL: $downloadURLafterEat');
Navigator.pop(
context, [downloadURLafterEat, formattedDate]);
}
} else {
debugPrint('After Eat No image Selected');
}
},
style: TextButton.styleFrom(
backgroundColor: const Color.fromARGB(255, 201, 244, 204),
fixedSize: const Size(320, 50),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
child: const Text(
'Upload',
style: TextStyle(
fontSize: 20,
color: Colors.black,
),
),
),
],
),
),
),
);
}
}