Refactor and date picker

This commit is contained in:
Ricardo Markiewicz
2019-08-03 00:17:14 -03:00
parent 776648b470
commit f5f109ab32
11 changed files with 337 additions and 218 deletions
+101 -80
View File
@@ -1,10 +1,9 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:como_gasto/category_selection_widget.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:provider/provider.dart';
import '../login_state.dart';
import '../expenses_repository.dart';
class AddPage extends StatefulWidget {
final Rect buttonRect;
@@ -23,6 +22,9 @@ class _AddPageState extends State<AddPage> with SingleTickerProviderStateMixin {
String category;
int value = 0;
String dateStr = "hoy";
DateTime date = DateTime.now();
@override
void initState() {
super.initState();
@@ -52,7 +54,10 @@ class _AddPageState extends State<AddPage> with SingleTickerProviderStateMixin {
@override
Widget build(BuildContext context) {
var h = MediaQuery.of(context).size.height;
var h = MediaQuery
.of(context)
.size
.height;
return Stack(
children: [
Transform.translate(
@@ -62,10 +67,27 @@ class _AddPageState extends State<AddPage> with SingleTickerProviderStateMixin {
automaticallyImplyLeading: false,
backgroundColor: Colors.transparent,
elevation: 0.0,
title: Text(
"Category",
style: TextStyle(
color: Colors.grey,
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,
),
),
),
centerTitle: false,
@@ -90,7 +112,10 @@ class _AddPageState extends State<AddPage> with SingleTickerProviderStateMixin {
}
Widget _body() {
var h = MediaQuery.of(context).size.height;
var h = MediaQuery
.of(context)
.size
.height;
return Column(
children: <Widget>[
_categorySelector(),
@@ -167,60 +192,63 @@ class _AddPageState extends State<AddPage> with SingleTickerProviderStateMixin {
return Expanded(
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
var height = constraints.biggest.height / 4;
var height = constraints.biggest.height / 4;
return Table(
border: TableBorder.all(
color: Colors.grey,
width: 1.0,
),
children: [
TableRow(children: [
_num("1", height),
_num("2", height),
_num("3", height),
]),
TableRow(children: [
_num("4", height),
_num("5", height),
_num("6", height),
]),
TableRow(children: [
_num("7", height),
_num("8", height),
_num("9", height),
]),
TableRow(children: [
_num(",", height),
_num("0", height),
GestureDetector(
onTap: () {
setState(() {
value = value ~/ 10;
});
},
child: Container(
height: height,
child: Center(
child: Icon(
Icons.backspace,
color: Colors.grey,
size: 40,
return Table(
border: TableBorder.all(
color: Colors.grey,
width: 1.0,
),
children: [
TableRow(children: [
_num("1", height),
_num("2", height),
_num("3", height),
]),
TableRow(children: [
_num("4", height),
_num("5", height),
_num("6", height),
]),
TableRow(children: [
_num("7", height),
_num("8", height),
_num("9", height),
]),
TableRow(children: [
_num(",", height),
_num("0", height),
GestureDetector(
onTap: () {
setState(() {
value = value ~/ 10;
});
},
child: Container(
height: height,
child: Center(
child: Icon(
Icons.backspace,
color: Colors.grey,
size: 40,
),
),
),
),
),
),
]),
],
);
}),
]),
],
);
}),
);
}
Widget _submit() {
if (_controller.value < 1) {
var buttonWidth = widget.buttonRect.right - widget.buttonRect.left;
var w = MediaQuery.of(context).size.width;
var w = MediaQuery
.of(context)
.size
.width;
return Positioned(
left: widget.buttonRect.left * (1 - _buttonAnimation.value),
@@ -230,8 +258,11 @@ class _AddPageState extends State<AddPage> with SingleTickerProviderStateMixin {
top: widget.buttonRect.top,
//<-- Margin from top
bottom:
(MediaQuery.of(context).size.height - widget.buttonRect.bottom) *
(1 - _buttonAnimation.value),
(MediaQuery
.of(context)
.size
.height - widget.buttonRect.bottom) *
(1 - _buttonAnimation.value),
//<-- Margin from bottom
child: Container(
width: double.infinity,
@@ -270,37 +301,27 @@ class _AddPageState extends State<AddPage> with SingleTickerProviderStateMixin {
),
),
onPressed: () {
var today = DateTime.now();
var user = Provider.of<LoginState>(context).currentUser();
var db = Provider.of<ExpensesRepository>(context);
if (value > 0 && category != "") {
Firestore.instance
.collection('users')
.document(user.uid)
.collection('expenses')
.document()
.setData({
"category": category,
"value": value / 100.0,
"month": today.month,
"day": today.day,
"year": today.year,
});
db.add(category, value / 100.0, date);
_controller.reverse();
} else {
showDialog(
context: context,
builder: (context) => AlertDialog(
content: Text("You need to select a category and a value greater than zero."),
actions: <Widget>[
FlatButton(
child: Text('Ok'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
)
builder: (context) =>
AlertDialog(
content: Text(
"You need to select a category and a value greater than zero."),
actions: <Widget>[
FlatButton(
child: Text('Ok'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
)
);
}
},