Add the ability to take a photo and save that image into Firebase Store
This commit is contained in:
@@ -1,4 +1,9 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:firebase_storage/firebase_storage.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
class ExpensesRepository {
|
||||
final String userId;
|
||||
@@ -33,18 +38,45 @@ class ExpensesRepository {
|
||||
.delete();
|
||||
}
|
||||
|
||||
add(String categoryName, double value, DateTime date) {
|
||||
Firestore.instance
|
||||
add(String categoryName, double value, DateTime date, File selectedImage) async {
|
||||
var document = Firestore.instance
|
||||
.collection('users')
|
||||
.document(userId)
|
||||
.collection('expenses')
|
||||
.document()
|
||||
.setData({
|
||||
.document();
|
||||
|
||||
await document.setData({
|
||||
"category": categoryName,
|
||||
"value": value,
|
||||
"month": date.month,
|
||||
"day": date.day,
|
||||
"year": date.year,
|
||||
});
|
||||
|
||||
if (selectedImage != null) {
|
||||
var imageName = Uuid().v1();
|
||||
var imagePath = "/users/$userId/$imageName.jpg";
|
||||
final StorageReference storageReference = FirebaseStorage().ref().child(imagePath);
|
||||
|
||||
final StorageUploadTask uploadTask = storageReference.putFile(selectedImage);
|
||||
|
||||
final StreamSubscription<StorageTaskEvent> streamSubscription = uploadTask.events.listen((event) {
|
||||
// You can use this to notify yourself or your user in any kind of way.
|
||||
// For example: you could use the uploadTask.events stream in a StreamBuilder instead
|
||||
// to show your user what the current status is. In that case, you would not need to cancel any
|
||||
// subscription as StreamBuilder handles this automatically.
|
||||
|
||||
// Here, every StorageTaskEvent concerning the upload is printed to the logs.
|
||||
print('EVENT ${event.type}');
|
||||
});
|
||||
|
||||
// Cancel your subscription when done.
|
||||
await uploadTask.onComplete;
|
||||
streamSubscription.cancel();
|
||||
|
||||
document.setData({
|
||||
"imageUrl": (await storageReference.getDownloadURL()).toString(),
|
||||
}, merge: true,);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+47
-28
@@ -1,6 +1,9 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:como_gasto/category_selection_widget.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../expenses_repository.dart';
|
||||
@@ -25,6 +28,8 @@ class _AddPageState extends State<AddPage> with SingleTickerProviderStateMixin {
|
||||
String dateStr = "hoy";
|
||||
DateTime date = DateTime.now();
|
||||
|
||||
File _selectedPicture;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@@ -64,41 +69,50 @@ class _AddPageState extends State<AddPage> with SingleTickerProviderStateMixin {
|
||||
offset: Offset(0, h * (1 - _pageAnimation.value)),
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
automaticallyImplyLeading: false,
|
||||
leading: BackButton(
|
||||
color: Colors.grey,
|
||||
onPressed: () {
|
||||
_controller.reverse();
|
||||
},
|
||||
),
|
||||
backgroundColor: Colors.transparent,
|
||||
elevation: 0.0,
|
||||
title: GestureDetector(
|
||||
onTap: () {
|
||||
showDatePicker(
|
||||
context: context,
|
||||
initialDate: DateTime.now(),
|
||||
firstDate: DateTime.now().subtract(Duration(hours: 24*30)),
|
||||
lastDate: DateTime.now(),
|
||||
).then((newDate) {
|
||||
if (newDate != null) {
|
||||
setState(() {
|
||||
date = newDate;
|
||||
dateStr = "${date.year.toString()}-${date.month.toString().padLeft(2,'0')}-${date.day.toString().padLeft(2,'0')}";
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
child: Text(
|
||||
"Category ($dateStr)",
|
||||
style: TextStyle(
|
||||
color: Colors.grey,
|
||||
),
|
||||
title: Text(
|
||||
"Category ($dateStr)",
|
||||
style: TextStyle(
|
||||
color: Colors.grey,
|
||||
),
|
||||
),
|
||||
centerTitle: false,
|
||||
actions: <Widget>[
|
||||
IconButton(
|
||||
icon: Icon(
|
||||
Icons.close,
|
||||
color: Colors.grey,
|
||||
),
|
||||
icon: Icon(Icons.calendar_today),
|
||||
color: Colors.grey,
|
||||
onPressed: () {
|
||||
_controller.reverse();
|
||||
showDatePicker(
|
||||
context: context,
|
||||
initialDate: DateTime.now(),
|
||||
firstDate: DateTime.now().subtract(Duration(hours: 24*30)),
|
||||
lastDate: DateTime.now(),
|
||||
).then((newDate) {
|
||||
if (newDate != null) {
|
||||
setState(() {
|
||||
date = newDate;
|
||||
dateStr = "${date.year.toString()}-${date.month.toString().padLeft(2,'0')}-${date.day.toString().padLeft(2,'0')}";
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
),
|
||||
IconButton(
|
||||
icon: Icon(Icons.camera_alt),
|
||||
color: Colors.grey,
|
||||
onPressed: () async {
|
||||
var image = await ImagePicker.pickImage(source: ImageSource.camera);
|
||||
|
||||
setState(() {
|
||||
_selectedPicture = image;
|
||||
});
|
||||
},
|
||||
)
|
||||
],
|
||||
@@ -119,6 +133,11 @@ class _AddPageState extends State<AddPage> with SingleTickerProviderStateMixin {
|
||||
return Column(
|
||||
children: <Widget>[
|
||||
_categorySelector(),
|
||||
if (_selectedPicture != null)
|
||||
SizedBox(
|
||||
height: 100,
|
||||
child: Image.file(_selectedPicture),
|
||||
),
|
||||
_currentValue(),
|
||||
_numpad(),
|
||||
SizedBox(
|
||||
@@ -303,7 +322,7 @@ class _AddPageState extends State<AddPage> with SingleTickerProviderStateMixin {
|
||||
onPressed: () {
|
||||
var db = Provider.of<ExpensesRepository>(context);
|
||||
if (value > 0 && category != "") {
|
||||
db.add(category, value / 100.0, date);
|
||||
db.add(category, value / 100.0, date, _selectedPicture);
|
||||
|
||||
_controller.reverse();
|
||||
} else {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:firebase_storage/firebase_storage.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class DayExpenseListTile extends StatelessWidget {
|
||||
@@ -46,6 +47,9 @@ class DayExpenseListTile extends StatelessWidget {
|
||||
),
|
||||
),
|
||||
),
|
||||
subtitle: document['imageUrl'] == null
|
||||
? null
|
||||
: Image.network(document['imageUrl']),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user