Refactor and date picker
This commit is contained in:
+101
-80
@@ -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();
|
||||
},
|
||||
),
|
||||
],
|
||||
)
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
+23
-98
@@ -1,110 +1,35 @@
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:como_gasto/pages/ui/day_expense_list_tile.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../login_state.dart';
|
||||
|
||||
class DetailsParams {
|
||||
class DetailsPage extends StatelessWidget {
|
||||
final String categoryName;
|
||||
final int month;
|
||||
final List<DocumentSnapshot> documents;
|
||||
final Function(String) onDelete;
|
||||
|
||||
DetailsParams(this.categoryName, this.month);
|
||||
}
|
||||
const DetailsPage({Key key, this.categoryName, this.documents, this.onDelete})
|
||||
: super(key: key);
|
||||
|
||||
class DetailsPage extends StatefulWidget {
|
||||
final DetailsParams params;
|
||||
|
||||
const DetailsPage({Key key, this.params}) : super(key: key);
|
||||
|
||||
@override
|
||||
_DetailsPageState createState() => _DetailsPageState();
|
||||
}
|
||||
|
||||
class _DetailsPageState extends State<DetailsPage> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Consumer<LoginState>(
|
||||
builder: (BuildContext context, LoginState state, Widget child) {
|
||||
var user = Provider.of<LoginState>(context).currentUser();
|
||||
var _query = Firestore.instance
|
||||
.collection('users')
|
||||
.document(user.uid)
|
||||
.collection('expenses')
|
||||
.where("month", isEqualTo: widget.params.month + 1)
|
||||
.where("category", isEqualTo: widget.params.categoryName)
|
||||
.snapshots();
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(categoryName),
|
||||
),
|
||||
body: ListView.builder(
|
||||
itemCount: documents.length,
|
||||
itemBuilder: (BuildContext context, int index) {
|
||||
var document = documents[index];
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(widget.params.categoryName),
|
||||
),
|
||||
body: StreamBuilder<QuerySnapshot>(
|
||||
stream: _query,
|
||||
builder:
|
||||
(BuildContext context, AsyncSnapshot<QuerySnapshot> data) {
|
||||
if (data.hasData) {
|
||||
return ListView.builder(
|
||||
itemBuilder: (BuildContext context, int index) {
|
||||
var document = data.data.documents[index];
|
||||
|
||||
return Dismissible(
|
||||
key: Key(document.documentID),
|
||||
onDismissed: (direction) {
|
||||
Firestore.instance
|
||||
.collection('users')
|
||||
.document(user.uid)
|
||||
.collection('expenses')
|
||||
.document(document.documentID)
|
||||
.delete();
|
||||
},
|
||||
child: ListTile(
|
||||
leading: Stack(
|
||||
children: <Widget>[
|
||||
Icon(
|
||||
Icons.calendar_today,
|
||||
size: 40,
|
||||
),
|
||||
Positioned(
|
||||
left: 0,
|
||||
right: 0,
|
||||
bottom: 8,
|
||||
child: Text(
|
||||
document["day"].toString(),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
title: Container(
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blueAccent.withOpacity(0.2),
|
||||
borderRadius: BorderRadius.circular(5.0),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Text(
|
||||
"\$${document["value"]}",
|
||||
style: TextStyle(
|
||||
color: Colors.blueAccent,
|
||||
fontWeight: FontWeight.w500,
|
||||
fontSize: 16.0,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
itemCount: data.data.documents.length,
|
||||
);
|
||||
}
|
||||
|
||||
return Center(
|
||||
child: CircularProgressIndicator(),
|
||||
);
|
||||
},
|
||||
));
|
||||
},
|
||||
return Dismissible(
|
||||
key: Key(document.documentID),
|
||||
onDismissed: (direction) {
|
||||
onDelete(document.documentID);
|
||||
},
|
||||
child: new DayExpenseListTile(document: document),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../expenses_repository.dart';
|
||||
import 'details_page.dart';
|
||||
|
||||
class DetailsParams {
|
||||
final String categoryName;
|
||||
final int month;
|
||||
|
||||
DetailsParams(this.categoryName, this.month);
|
||||
}
|
||||
|
||||
class DetailsPageContainer extends StatefulWidget {
|
||||
final DetailsParams params;
|
||||
|
||||
const DetailsPageContainer({Key key, this.params}) : super(key: key);
|
||||
|
||||
@override
|
||||
_DetailsPageContainerState createState() => _DetailsPageContainerState();
|
||||
}
|
||||
|
||||
class _DetailsPageContainerState extends State<DetailsPageContainer> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Consumer<ExpensesRepository>(
|
||||
builder: (BuildContext context, ExpensesRepository db, Widget child) {
|
||||
var _query = db.queryByCategory(
|
||||
widget.params.month + 1, widget.params.categoryName);
|
||||
|
||||
return StreamBuilder<QuerySnapshot>(
|
||||
stream: _query,
|
||||
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> data) {
|
||||
if (data.hasData) {
|
||||
return DetailsPage(
|
||||
categoryName: widget.params.categoryName,
|
||||
documents: data.data.documents,
|
||||
onDelete: (documentId) {
|
||||
db.delete(documentId);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
return Center(
|
||||
child: CircularProgressIndicator(),
|
||||
);
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
+13
-24
@@ -1,13 +1,12 @@
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:como_gasto/add_page_transition.dart';
|
||||
import 'package:como_gasto/month_widget.dart';
|
||||
import 'package:como_gasto/pages/add_page.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:rect_getter/rect_getter.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
|
||||
import '../expenses_repository.dart';
|
||||
import '../login_state.dart';
|
||||
import '../utils.dart';
|
||||
|
||||
@@ -50,15 +49,9 @@ class _HomePageState extends State<HomePage> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Consumer<LoginState>(
|
||||
builder: (BuildContext context, LoginState state, Widget child) {
|
||||
var user = Provider.of<LoginState>(context).currentUser();
|
||||
_query = Firestore.instance
|
||||
.collection('users')
|
||||
.document(user.uid)
|
||||
.collection('expenses')
|
||||
.where("month", isEqualTo: currentPage + 1)
|
||||
.snapshots();
|
||||
return Consumer<ExpensesRepository>(
|
||||
builder: (BuildContext context, ExpensesRepository db, Widget child) {
|
||||
_query = db.queryByMonth(currentPage + 1);
|
||||
|
||||
return Scaffold(
|
||||
bottomNavigationBar: BottomAppBar(
|
||||
@@ -183,16 +176,11 @@ class _HomePageState extends State<HomePage> {
|
||||
size: Size.fromHeight(70.0),
|
||||
child: PageView(
|
||||
onPageChanged: (newPage) {
|
||||
var user = Provider.of<LoginState>(context).currentUser();
|
||||
var db = Provider.of<ExpensesRepository>(context);
|
||||
|
||||
setState(() {
|
||||
currentPage = newPage;
|
||||
_query = Firestore.instance
|
||||
.collection('users')
|
||||
.document(user.uid)
|
||||
.collection('expenses')
|
||||
.where("month", isEqualTo: currentPage + 1)
|
||||
.snapshots();
|
||||
_query = db.queryByMonth(currentPage + 1);
|
||||
});
|
||||
},
|
||||
controller: _controller,
|
||||
@@ -218,7 +206,8 @@ class _HomePageState extends State<HomePage> {
|
||||
flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
|
||||
|
||||
// initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
|
||||
var initializationSettingsAndroid = new AndroidInitializationSettings('app_icon');
|
||||
var initializationSettingsAndroid =
|
||||
new AndroidInitializationSettings('app_icon');
|
||||
var initializationSettingsIOS = new IOSInitializationSettings(
|
||||
onDidReceiveLocalNotification: onDidReceiveLocalNotification,
|
||||
);
|
||||
@@ -227,10 +216,12 @@ class _HomePageState extends State<HomePage> {
|
||||
initializationSettingsIOS,
|
||||
);
|
||||
|
||||
flutterLocalNotificationsPlugin.initialize(
|
||||
flutterLocalNotificationsPlugin
|
||||
.initialize(
|
||||
initializationSettings,
|
||||
onSelectNotification: onSelectNotification,
|
||||
).then((init) {
|
||||
)
|
||||
.then((init) {
|
||||
setupNotification();
|
||||
});
|
||||
}
|
||||
@@ -266,9 +257,7 @@ class _HomePageState extends State<HomePage> {
|
||||
void setupNotification() async {
|
||||
var time = new Time(16, 11, 0);
|
||||
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
|
||||
'daily-notifications',
|
||||
'Daily Notifications',
|
||||
'Daily Notifications');
|
||||
'daily-notifications', 'Daily Notifications', 'Daily Notifications');
|
||||
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
|
||||
var platformChannelSpecifics = new NotificationDetails(
|
||||
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class DayExpenseListTile extends StatelessWidget {
|
||||
const DayExpenseListTile({
|
||||
Key key,
|
||||
@required this.document,
|
||||
}) : super(key: key);
|
||||
|
||||
final DocumentSnapshot document;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListTile(
|
||||
leading: Stack(
|
||||
children: <Widget>[
|
||||
Icon(
|
||||
Icons.calendar_today,
|
||||
size: 40,
|
||||
),
|
||||
Positioned(
|
||||
left: 0,
|
||||
right: 0,
|
||||
bottom: 8,
|
||||
child: Text(
|
||||
document["day"].toString(),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
title: Container(
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blueAccent.withOpacity(0.2),
|
||||
borderRadius: BorderRadius.circular(5.0),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Text(
|
||||
"\$${document["value"]}",
|
||||
style: TextStyle(
|
||||
color: Colors.blueAccent,
|
||||
fontWeight: FontWeight.w500,
|
||||
fontSize: 16.0,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user