Add Firestore support

This commit is contained in:
Ricardo Markiewicz
2019-04-21 01:51:43 -03:00
parent 30324991f5
commit ced7aa91d7
13 changed files with 558 additions and 96 deletions
+5 -12
View File
@@ -4,22 +4,15 @@ import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart';
class GraphWidget extends StatefulWidget {
final List<double> data;
const GraphWidget({Key key, this.data}) : super(key: key);
@override
_GraphWidgetState createState() => _GraphWidgetState();
}
class _GraphWidgetState extends State<GraphWidget> {
var data;
@override
void initState() {
super.initState();
var r = Random();
data = List<double>.generate(30, (i) => r.nextDouble() * 1500);
}
_onSelectionChanged(SelectionModel model) {
final selectedDatum = model.selectedDatum;
@@ -56,7 +49,7 @@ class _GraphWidgetState extends State<GraphWidget> {
colorFn: (_, __) => MaterialPalette.blue.shadeDefault,
domainFn: (value, index) => index,
measureFn: (value, _) => value,
data: data,
data: widget.data,
strokeWidthPxFn: (_, __) => 4,
)
];
+25 -83
View File
@@ -1,6 +1,8 @@
import 'package:como_gasto/graph_widget.dart';
import 'package:como_gasto/month_widget.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
void main() => runApp(MyApp());
@@ -25,12 +27,18 @@ class HomePage extends StatefulWidget {
class _HomePageState extends State<HomePage> {
PageController _controller;
int currentPage = 9;
Stream<QuerySnapshot> _query;
@override
void initState() {
super.initState();
_query = Firestore.instance
.collection('expenses')
.where("month", isEqualTo: currentPage + 1)
.snapshots();
_controller = PageController(
initialPage: currentPage,
viewportFraction: 0.4,
@@ -79,13 +87,20 @@ class _HomePageState extends State<HomePage> {
child: Column(
children: <Widget>[
_selector(),
_expenses(),
_graph(),
Container(
color: Colors.blueAccent.withOpacity(0.15),
height: 24.0,
StreamBuilder<QuerySnapshot>(
stream: _query,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> data) {
if (data.hasData) {
return MonthWidget(
documents: data.data.documents,
);
}
return Center(
child: CircularProgressIndicator(),
);
},
),
_list(),
],
),
);
@@ -127,6 +142,10 @@ class _HomePageState extends State<HomePage> {
onPageChanged: (newPage) {
setState(() {
currentPage = newPage;
_query = Firestore.instance
.collection('expenses')
.where("month", isEqualTo: currentPage + 1)
.snapshots();
});
},
controller: _controller,
@@ -147,81 +166,4 @@ class _HomePageState extends State<HomePage> {
),
);
}
Widget _expenses() {
return Column(
children: <Widget>[
Text("\$2361,41",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 40.0,
),
),
Text("Total expenses",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16.0,
color: Colors.blueGrey,
),
),
],
);
}
Widget _graph() {
return Container(
height: 250.0,
child: GraphWidget(),
);
}
Widget _item(IconData icon, String name, int percent, double value) {
return ListTile(
leading: Icon(icon, size: 32.0,),
title: Text(name,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0
),
),
subtitle: Text("$percent% of expenses",
style: TextStyle(
fontSize: 16.0,
color: Colors.blueGrey,
),
),
trailing: Container(
decoration: BoxDecoration(
color: Colors.blueAccent.withOpacity(0.2),
borderRadius: BorderRadius.circular(5.0),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text("\$$value",
style: TextStyle(
color: Colors.blueAccent,
fontWeight: FontWeight.w500,
fontSize: 16.0,
),
),
),
),
);
}
Widget _list() {
return Expanded(
child: ListView.separated(
itemCount: 15,
itemBuilder: (BuildContext context, int index) =>
_item(FontAwesomeIcons.shoppingCart, "Shopping", 14, 145.12),
separatorBuilder: (BuildContext context, int index) {
return Container(
color: Colors.blueAccent.withOpacity(0.15),
height: 8.0,
);
},
),
);
}
}
+135
View File
@@ -0,0 +1,135 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:como_gasto/graph_widget.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
class MonthWidget extends StatefulWidget {
final List<DocumentSnapshot> documents;
final double total;
final List<double> perDay;
final Map<String, double> categories;
MonthWidget({Key key, this.documents})
:
total = documents.map((doc) => doc['value'])
.fold(0.0, (a, b) => a + b),
perDay = List.generate(30, (int index) {
return documents.where((doc) => doc['day'] == (index + 1))
.map((doc) => doc['value'])
.fold(0.0, (a, b) => a + b);
}),
categories = documents.fold({}, (Map<String, double> map, document) {
if (!map.containsKey(document['category'])) {
map[document['category']] = 0.0;
}
map[document['category']] += document['value'];
return map;
}),
super(key: key);
@override
_MonthWidgetState createState() => _MonthWidgetState();
}
class _MonthWidgetState extends State<MonthWidget> {
@override
Widget build(BuildContext context) {
print(widget.categories);
return Expanded(
child: Column(
children: <Widget>[
_expenses(),
_graph(),
Container(
color: Colors.blueAccent.withOpacity(0.15),
height: 24.0,
),
_list(),
],
),
);
}
Widget _expenses() {
return Column(
children: <Widget>[
Text("\$${widget.total.toStringAsFixed(2)}",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 40.0,
),
),
Text("Total expenses",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16.0,
color: Colors.blueGrey,
),
),
],
);
}
Widget _graph() {
return Container(
height: 250.0,
child: GraphWidget(
data: widget.perDay,
),
);
}
Widget _item(IconData icon, String name, int percent, double value) {
return ListTile(
leading: Icon(icon, size: 32.0,),
title: Text(name,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0
),
),
subtitle: Text("$percent% of expenses",
style: TextStyle(
fontSize: 16.0,
color: Colors.blueGrey,
),
),
trailing: Container(
decoration: BoxDecoration(
color: Colors.blueAccent.withOpacity(0.2),
borderRadius: BorderRadius.circular(5.0),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text("\$$value",
style: TextStyle(
color: Colors.blueAccent,
fontWeight: FontWeight.w500,
fontSize: 16.0,
),
),
),
),
);
}
Widget _list() {
return Expanded(
child: ListView.separated(
itemCount: widget.categories.keys.length,
itemBuilder: (BuildContext context, int index) {
var key = widget.categories.keys.elementAt(index);
var data = widget.categories[key];
return _item(FontAwesomeIcons.shoppingCart, key, 100 * data ~/ widget.total, data);
},
separatorBuilder: (BuildContext context, int index) {
return Container(
color: Colors.blueAccent.withOpacity(0.15),
height: 8.0,
);
},
),
);
}
}