-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPick_pictures.dart
135 lines (129 loc) · 3.88 KB
/
Pick_pictures.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
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:insta_trame/Classes/AddImages.dart';
import 'dart:io';
import 'package:path/path.dart' as Path;
import 'package:image_picker/image_picker.dart';
import 'package:firebase_storage/firebase_storage.dart' as firebase;
class Pick_pictures extends StatefulWidget{
@override
_Pick_picturesState createState() => _Pick_picturesState();
}
class _Pick_picturesState extends State<Pick_pictures> {
bool uploading = false;
double val = 0;
CollectionReference imgRef ;
firebase.Reference ref ;
List<File> _image = [] ;
File images;
final imagePicker = ImagePicker();
final picker = ImagePicker();
Future getImage() async{
final image = await imagePicker.getImage(source: ImageSource.camera);
setState(() {
images = File(image.path);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Images'),
centerTitle: true,
actions: [
FlatButton(onPressed: () {
setState(() {
uploading = true ;
});
uploadFile().whenComplete(() => Navigator.of(context).push(MaterialPageRoute(builder: (context)=> AddImages())));},
child: Text('UPLOAD'))
],
),
body:Stack(
children: [
GridView.builder(gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3),
itemCount: _image.length+1,
itemBuilder: (context,index){
return index == 0
? Center(
child: IconButton(
icon: Icon(Icons.add),
onPressed: () {
chooseImage();
},
),
)
:Container(margin: EdgeInsets.all(3.0),
decoration: BoxDecoration(
image: DecorationImage(
image: FileImage(_image[index-1]),
fit: BoxFit.cover,
)
),);
}
),
uploading? Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(child: CircularProgressIndicator(value: val,),),
],
),)
:Container(),
],
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.blue,
child: Icon(Icons.camera_alt_sharp),
onPressed: (){
getImage();
},
),
);
}
chooseImage() async{
final pickedFile = await picker.getImage(source: ImageSource.gallery);
setState(() {
_image.add(File(pickedFile?.path));
});
if(pickedFile.path==null) retrLevel();
}
Future<void > retrLevel() async{
final LostData response = await picker.getLostData();
if(response.isEmpty){
return ;
}
if(response.file!= null ){
setState(() {
_image.add(File(response.file.path));
});
}else{
print(response.file);
}
}
Future uploadFile() async{
int j =1 ;
for(var i in _image){
setState(() {
val = j /_image.length ;
});
String p = Path.basename(i.path);
ref = firebase.FirebaseStorage.instance
.ref()
.child('Images/$p');
await ref.putFile(i).whenComplete(() async{
await ref.getDownloadURL().then((value){
imgRef.add({'url' : value});
j++;
});
});
}
}
@override
void initState() {
// TODO: implement initState
super.initState();
imgRef = FirebaseFirestore.instance.collection('imageURLs');
}
}