-
Notifications
You must be signed in to change notification settings - Fork 3
/
imageUploader.dart
150 lines (139 loc) · 4.28 KB
/
imageUploader.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
import 'dart:io';
import 'package:firebase_storage/firebase_storage.dart'; // For File Upload To Firestore
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart'; // For Image Picker
import 'package:path/path.dart' as Path;
import 'mainScreen.dart';
void main() => runApp(ImageUploaderRun());
//need this class to test class because Stateless widget provides the required MediaQuery
class ImageUploaderRun extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: ImageUploader(),
);
}
// ImageUploader.
}
class ImageUploader extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _ImageUploaderState();
}
}
class _ImageUploaderState extends State<ImageUploader> {
File _image; //image will be stored here
String _uploadedFileURL; //storage url will be stored here
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Upload Car Image'),
),
body: Center(
child: Column(
children: <Widget>[
Text('Selected Image:'),
_image != null
? Image.asset(
_image.path,
height: 150,
)
: Container(height: 150),
SizedBox(height: 25),
_image == null
? RaisedButton(
child: Text('Choose File'),
onPressed: chooseFile,
color: Colors.cyan,
)
: Container(),
_image != null
? RaisedButton(
child: Text('Upload File'),
onPressed: callUploadFile,
color: Colors.cyan,
)
: Container(),
_image != null
? RaisedButton(
child: Text('Clear Selection'),
onPressed: clearSelection,
)
: Container(),
SizedBox(height: 25),
Text('Image Uploaded:'),
_uploadedFileURL != null
? Image.network(
_uploadedFileURL,
height: 150,
)
: Container(),
SizedBox(height: 25),
_uploadedFileURL == null
? RaisedButton(
child: Text('Done'),
onPressed: null,
color: Colors.cyan,
)
: Container(),
_uploadedFileURL != null
? RaisedButton(
child: Text('Done'),
onPressed: () {
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => SwipeMyRideMain()),
);
},
color: Colors.cyan,
)
: Container(),
],
),
),
);
}
void callUploadFile() {
UploadFile uploadFile = new UploadFile();
uploadFile.uploadFile(_image);
StorageReference storageReference = uploadFile.getStorageReference();
storageReference.getDownloadURL().then((fileURL) {
setState(() {
_uploadedFileURL = fileURL;
});
});
}
Future chooseFile() async {
await ImagePicker.pickImage(source: ImageSource.gallery).then((image) {
setState(() {
_image = image;
});
});
}
void clearSelection() {
this._uploadedFileURL = null;
setState(() {
_image = null;
});
}
}
class UploadFile {
StorageReference _storageReference;
Future uploadFile(File image) async {
_storageReference = FirebaseStorage.instance
.ref()
.child('users/${Path.basename(image.path)}}');
StorageUploadTask uploadTask = _storageReference.putFile(image);
await uploadTask.onComplete;
print('File Uploaded');
}
StorageReference getStorageReference() {
if (_storageReference != null) {
return _storageReference;
} else {
return null;
}
}
}