-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_wallpaper.dart
171 lines (158 loc) · 5.24 KB
/
add_wallpaper.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
import 'package:path/path.dart' as path;
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_ml_vision/firebase_ml_vision.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:io';
import 'package:flutter/material.dart';
class AddWallpaper extends StatefulWidget {
@override
_AddWallpaperState createState() => _AddWallpaperState();
}
class _AddWallpaperState extends State<AddWallpaper> {
bool _uploading = false;
bool _isUploaded = false;
File _image;
final ImageLabeler labeler = FirebaseVision.instance.imageLabeler();
List<ImageLabel> detectedLabels;
List<String> labelsInString = [];
Firestore _db = Firestore.instance;
final FirebaseAuth _auth = FirebaseAuth.instance;
final FirebaseStorage _storage = FirebaseStorage.instance;
@override
void dispose() {
labeler.close(); //thus terminating the labeler after it is used.
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Upload a wallpaper'),
),
body: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Container(
// height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: Column(
children: <Widget>[
Expanded(
child: InkWell(
onTap: () {
_loadImage();
},
child: _image != null
? Image.file(_image)
: Image.asset('assets/placeholder.jpg'),
),
),
SizedBox(
height: 20,
),
Text('Click on image to upload wallpaper'),
SizedBox(
height: 20,
),
detectedLabels != null
? Padding(
padding: const EdgeInsets.all(16.0),
child: Wrap(
spacing: 12,
children: detectedLabels.map((label) {
return Chip(
label: Text(label.text),
);
}).toList(),
),
)
: Container(),
SizedBox(height: 40),
_uploading ? Text('Uploading wallpaper') : Text('.'),
_isUploaded ? Text('Upload completed') : Text('.'),
SizedBox(
height: 40,
),
RaisedButton(
onPressed: () => _uploadWallpaper(),
child: Text('Upload'),
),
],
),
),
));
}
void _loadImage() async {
// ignore: deprecated_member_use
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
final FirebaseVisionImage visionImage = FirebaseVisionImage.fromFile(image);
final List<ImageLabel> labels = await labeler.processImage(visionImage);
List labelsInString;
setState(() {
detectedLabels = labels;
_image = image;
});
for (ImageLabel l in labels) {
labelsInString.add(l.text);
}
}
void _uploadWallpaper() async {
if (_image != null) {
String fileName = path.basename(_image.path);
print(fileName);
FirebaseUser user = await _auth.currentUser();
String uid = user.uid;
StorageUploadTask task = _storage
.ref()
.child('Wallpapers')
.child(uid)
.child(fileName)
.putFile(_image);
task.events.listen((e) {
if (e.type == StorageTaskEventType.progress) {
setState(() {
_uploading = true;
});
}
if (e.type == StorageTaskEventType.success) {
e.snapshot.ref.getDownloadURL().then((url) {
_db.collection('Wallpapers').add({
"url": url,
"date": DateTime.now(),
"uploaded_by": uid,
"tags": labelsInString.toList(),
}
);
Navigator.pop(context);
});
setState(() {
_uploading = false;
_isUploaded = true;
});
}
});
} else {
showDialog(
context: context,
builder: (ctx) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18),
),
title: Text('ERROR'),
content: Text("Select image to upload"),
actions: <Widget>[
RaisedButton(
child: Text('OK'),
onPressed: () => Navigator.pop(ctx),
)
],
);
});
}
}
}
// for(var label in labels){
// print("${label.text} [${label.confidence}]");
// }