mirror of
https://github.com/mdanics/fluttergram.git
synced 2025-08-06 13:19:53 +08:00
added loading indicator + compression before uploading
This commit is contained in:
@ -1,5 +1,5 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:image_picker/image_picker.dart'; // new
|
import 'package:image_picker/image_picker.dart';
|
||||||
import 'package:firebase_storage/firebase_storage.dart';
|
import 'package:firebase_storage/firebase_storage.dart';
|
||||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||||
import 'package:uuid/uuid.dart';
|
import 'package:uuid/uuid.dart';
|
||||||
@ -18,6 +18,8 @@ class _Uploader extends State<Uploader> {
|
|||||||
File file;
|
File file;
|
||||||
TextEditingController descriptionController = new TextEditingController();
|
TextEditingController descriptionController = new TextEditingController();
|
||||||
|
|
||||||
|
bool uploading = false;
|
||||||
|
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return file == null
|
return file == null
|
||||||
? new IconButton(
|
? new IconButton(
|
||||||
@ -50,9 +52,8 @@ class _Uploader extends State<Uploader> {
|
|||||||
new PostForm(
|
new PostForm(
|
||||||
imageFile: file,
|
imageFile: file,
|
||||||
descriptionController: descriptionController,
|
descriptionController: descriptionController,
|
||||||
|
loading: uploading,
|
||||||
),
|
),
|
||||||
new FlatButton(
|
|
||||||
onPressed: compressImage, child: new Text('sketch'))
|
|
||||||
],
|
],
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
@ -75,7 +76,7 @@ class _Uploader extends State<Uploader> {
|
|||||||
|
|
||||||
// image.format = Im.Image.RGBA;
|
// image.format = Im.Image.RGBA;
|
||||||
// Im.Image newim = Im.remapColors(image, alpha: Im.LUMINANCE);
|
// Im.Image newim = Im.remapColors(image, alpha: Im.LUMINANCE);
|
||||||
|
|
||||||
var newim2 = new File('$path/img_$rand.jpg')
|
var newim2 = new File('$path/img_$rand.jpg')
|
||||||
..writeAsBytesSync(Im.encodeJpg(image, quality: 85));
|
..writeAsBytesSync(Im.encodeJpg(image, quality: 85));
|
||||||
|
|
||||||
@ -92,10 +93,17 @@ class _Uploader extends State<Uploader> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void postImage() {
|
void postImage() {
|
||||||
|
setState(() {
|
||||||
|
uploading = true;
|
||||||
|
});
|
||||||
|
compressImage();
|
||||||
Future<String> upload = uploadImage(file).then((String data) {
|
Future<String> upload = uploadImage(file).then((String data) {
|
||||||
postToFireStore(mediaUrl: data, description: descriptionController.text);
|
postToFireStore(mediaUrl: data, description: descriptionController.text);
|
||||||
}).then((_) {
|
}).then((_) {
|
||||||
setState((){file = null;});
|
setState(() {
|
||||||
|
file = null;
|
||||||
|
uploading = false;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -103,18 +111,20 @@ class _Uploader extends State<Uploader> {
|
|||||||
class PostForm extends StatelessWidget {
|
class PostForm extends StatelessWidget {
|
||||||
var imageFile;
|
var imageFile;
|
||||||
TextEditingController descriptionController;
|
TextEditingController descriptionController;
|
||||||
PostForm({this.imageFile, this.descriptionController});
|
bool loading;
|
||||||
|
PostForm({this.imageFile, this.descriptionController, this.loading});
|
||||||
|
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return new Column(
|
return new Column(
|
||||||
children: <Widget>[
|
children: <Widget>[
|
||||||
new Padding(padding: new EdgeInsets.only(top: 10.0)),
|
loading
|
||||||
|
? new LinearProgressIndicator()
|
||||||
|
: new Padding(padding: new EdgeInsets.only(top: 0.0)),
|
||||||
new Container(
|
new Container(
|
||||||
height: 250.0,
|
|
||||||
child: new Image.file(
|
child: new Image.file(
|
||||||
imageFile,
|
imageFile,
|
||||||
fit: BoxFit.fitHeight,
|
fit: BoxFit.fitWidth,
|
||||||
)),
|
)),
|
||||||
new Divider(),
|
new Divider(),
|
||||||
new Row(
|
new Row(
|
||||||
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
||||||
@ -153,7 +163,7 @@ void postToFireStore(
|
|||||||
reference.add({
|
reference.add({
|
||||||
"username": "testeronslice",
|
"username": "testeronslice",
|
||||||
"location": "nice location",
|
"location": "nice location",
|
||||||
"likes": 0,
|
"likes": {},
|
||||||
"mediaUrl": mediaUrl,
|
"mediaUrl": mediaUrl,
|
||||||
"description": description,
|
"description": description,
|
||||||
"ownerId": googleSignIn.currentUser.id
|
"ownerId": googleSignIn.currentUser.id
|
||||||
|
Reference in New Issue
Block a user