-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathhomeScreen.dart
More file actions
283 lines (267 loc) · 8.38 KB
/
homeScreen.dart
File metadata and controls
283 lines (267 loc) · 8.38 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:forest_tagger/components/WelComePage.dart';
import 'package:forest_tagger/components/generatorPage.dart';
import 'package:forest_tagger/components/scannerPage.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:io';
import 'myflatButton.dart';
class HomeScreen extends StatefulWidget {
String name = "User";
HomeScreen(this.name);
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _HomeScreen(this.name);
}
}
class _HomeScreen extends State<HomeScreen> {
String name = "User";
File _image;
var _uploadImg;
// By default profile image set
Image _profileImage = Image.asset(
'assets/images/sample_profile_image.jpg',
fit: BoxFit.fill,
);
_HomeScreen(this.name);
Future getImageFromStorage(BuildContext context) async {
var image = await ImagePicker().getImage(source: ImageSource.gallery);
setState(() {
try {
_image = File(image.path);
_uploadImg = setProfilePic(context);
} catch (e) {
_uploadImg = null;
}
print("Image Path: $_image");
});
}
Future uploadProfilePic(BuildContext context) async {
setState(() {
_uploadImg = null;
});
// Profile Image name Same as user id
String fileName = FirebaseAuth.instance.currentUser.uid;
Reference firebaseStorageRef =
FirebaseStorage.instance.ref().child(fileName);
UploadTask uploadTask = firebaseStorageRef.putFile(_image);
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
duration: Duration(seconds: 5),
content: Text('Wait...Picture is uploading')));
await uploadTask.whenComplete(() {
print("Picture Uploaded");
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text('Picture Uploaded')));
});
}
Future<void> getProfilePicFromFirebase() async {
try {
String fileName = FirebaseAuth.instance.currentUser.uid;
Reference firebaseStorageRef =
FirebaseStorage.instance.ref().child(fileName);
setState(() async {
try {
// Get Profile Image from Firebase Storage
_profileImage = Image.network(
await firebaseStorageRef.getDownloadURL(),
fit: BoxFit.fill,
);
} catch (e) {
// If user not set profile image
print("User not set profile image");
print(e.toString());
setState(() {
_profileImage = Image.asset(
'assets/images/sample_profile_image.jpg',
fit: BoxFit.fill,
);
});
}
});
} catch (e) {
// Sometimes due to lagging, Image not loading...so loading screen
// If you close the app and reopen, profile image automatically come
print("Lagging Problem");
print(e.toString());
setState(() {
_profileImage = Image.asset(
'assets/images/loading.gif',
fit: BoxFit.fill,
);
});
}
}
Widget imageSelect() {
try {
return _image != null
? Image.file(
_image,
fit: BoxFit.fill,
)
: _profileImage;
} catch (e) {
return Image.asset(
'assets/images/sample_profile_image.jpg',
fit: BoxFit.fill,
);
}
}
Widget setProfilePic(BuildContext context) {
return GestureDetector(
child: Column(
children: <Widget>[
Image.asset(
'assets/images/upload.png',
width: 30.0,
),
SizedBox(
height: 5.0,
),
Text("Upload"),
],
),
onTap: () {
print("Upload Selected");
uploadProfilePic(context);
});
}
@override
void initState() {
// TODO: implement initState
super.initState();
_uploadImg = null;
try {
if (name == "User") {
FirebaseFirestore.instance
.collection('users')
.where('email', isEqualTo: FirebaseAuth.instance.currentUser.email)
.get()
.then((querySnapShot) {
querySnapShot.docs.forEach((element) {
setState(() {
print(element.get('user'));
this.name = element.get('user');
});
});
});
}
getProfilePicFromFirebase();
} catch (e) {
print("Fetching Error");
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Stack(
children: [
Align(
alignment: Alignment.center,
child: CircleAvatar(
radius: 100,
backgroundColor: Colors.black,
child: ClipOval(
child: SizedBox(
width: 192.0,
height: 192.0,
child: imageSelect(),
),
),
),
),
Container(
alignment: Alignment.center,
margin: EdgeInsets.only(top: 150, left: 173),
decoration: BoxDecoration(
color: Colors.black,
shape: BoxShape.circle,),
child: IconButton(
icon: Icon(
Icons.camera_alt_rounded,
size: 30.0,
color: Colors.white,
),
onPressed: () {
print("Camera pressed");
getImageFromStorage(context);
},
),
),
Container(
alignment: Alignment.centerRight,
margin: EdgeInsets.only(top: 80, right: 30),
child: _uploadImg,
),
],
),
SizedBox(
height: 20,
),
Column(
children: [
Text(
"Welcome,",
style: TextStyle(
color: Colors.grey[700],
fontSize: 20,
fontWeight: FontWeight.w700,
),
),
SizedBox(
height: 5,
),
Text(
"$name",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w700,
),
),
],
),
SizedBox(
height: 20,
),
MyFlatButton("Scan QR code of a tree", ScannerPage()),
MyFlatButton("Add a new tree", GeneratorPage())
],
),
),
Positioned(
top: MediaQuery.of(context).size.height * 0.07,
right: 20,
child: ElevatedButton(
child: Text(
"LogOut",
style: TextStyle(color: Colors.white),
),
style: ElevatedButton.styleFrom(
padding: EdgeInsets.symmetric(vertical: 15, horizontal: 30),
primary: Colors.green,
shape: RoundedRectangleBorder(
side: BorderSide(color: Colors.green, width: 3.0),
borderRadius: BorderRadius.circular(5.0))),
onPressed: () {
FirebaseAuth.instance.signOut();
Navigator.pop(context);
Navigator.push(
context,
MaterialPageRoute(builder: (_) => Welcome()),
);
},
))
],
),
);
}
}