diff --git a/lib/add_page_transition.dart b/lib/add_page_transition.dart new file mode 100644 index 0000000..a0b880f --- /dev/null +++ b/lib/add_page_transition.dart @@ -0,0 +1,15 @@ +import 'package:flutter/material.dart'; + +class AddPageTransition extends PageRouteBuilder { + final Widget page; + final Widget background; + + AddPageTransition({this.page, this.background}) + : super( + transitionDuration: Duration(microseconds: 1), + pageBuilder: (context, animation1, animation2) => page, + transitionsBuilder: (context, animation1, animation, child) => + Stack( + children: [background, child], + )); +} diff --git a/lib/graph_widget.dart b/lib/graph_widget.dart index 252c1dc..391f7aa 100644 --- a/lib/graph_widget.dart +++ b/lib/graph_widget.dart @@ -3,16 +3,42 @@ import 'dart:math'; import 'package:flutter/material.dart'; import 'package:charts_flutter/flutter.dart'; -class GraphWidget extends StatefulWidget { +class PieGraphWidget extends StatefulWidget { final List data; - const GraphWidget({Key key, this.data}) : super(key: key); + const PieGraphWidget({Key key, this.data}) : super(key: key); @override - _GraphWidgetState createState() => _GraphWidgetState(); + _PieGraphWidgetState createState() => _PieGraphWidgetState(); } -class _GraphWidgetState extends State { +class _PieGraphWidgetState extends State { + @override + Widget build(BuildContext context) { + List> series = [ + Series( + id: 'Gasto', + domainFn: (value, index) => index, + measureFn: (value, _) => value, + data: widget.data, + strokeWidthPxFn: (_, __) => 4, + ) + ]; + + return PieChart(series); + } +} + +class LinesGraphWidget extends StatefulWidget { + final List data; + + const LinesGraphWidget({Key key, this.data}) : super(key: key); + + @override + _LinesGraphWidgetState createState() => _LinesGraphWidgetState(); +} + +class _LinesGraphWidgetState extends State { _onSelectionChanged(SelectionModel model) { final selectedDatum = model.selectedDatum; diff --git a/lib/main.dart b/lib/main.dart index 30adb4d..8cd6ebf 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,7 @@ +import 'package:como_gasto/add_page_transition.dart'; import 'package:como_gasto/login_state.dart'; import 'package:como_gasto/pages/add_page.dart'; +import 'package:como_gasto/pages/details_page.dart'; import 'package:como_gasto/pages/home_page.dart'; import 'package:como_gasto/pages/login_page.dart'; import 'package:flutter/material.dart'; @@ -17,6 +19,18 @@ class MyApp extends StatelessWidget { theme: ThemeData( primarySwatch: Colors.blue, ), + onGenerateRoute: (settings) { + if (settings.name == '/details') { + DetailsParams params = settings.arguments; + return MaterialPageRoute( + builder: (BuildContext context) { + return DetailsPage( + params: params , + ); + } + ); + } + }, routes: { '/': (BuildContext context) { var state = Provider.of(context); @@ -26,7 +40,6 @@ class MyApp extends StatelessWidget { return LoginPage(); } }, - '/add': (BuildContext contex) => AddPage(), }, ), ); diff --git a/lib/month_widget.dart b/lib/month_widget.dart index 616f04b..5a97e30 100644 --- a/lib/month_widget.dart +++ b/lib/month_widget.dart @@ -1,15 +1,23 @@ import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:como_gasto/graph_widget.dart'; +import 'package:como_gasto/pages/details_page.dart'; import 'package:flutter/material.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; +enum GraphType { + LINES, + PIE, +} + class MonthWidget extends StatefulWidget { final List documents; final double total; final List perDay; final Map categories; + final GraphType graphType; + final int month; - MonthWidget({Key key, this.documents, days}) + MonthWidget({Key key, @required this.month, this.graphType, this.documents, days}) : total = documents.map((doc) => doc['value']) .fold(0.0, (a, b) => a + b), @@ -72,16 +80,30 @@ class _MonthWidgetState extends State { } Widget _graph() { - return Container( - height: 250.0, - child: GraphWidget( - data: widget.perDay, - ), - ); + if (widget.graphType == GraphType.LINES) { + return Container( + height: 250.0, + child: LinesGraphWidget( + data: widget.perDay, + ), + ); + } else { + var perCategory = widget.categories.keys.map((name) => widget.categories[name] / widget.total).toList(); + return Container( + height: 250.0, + child: PieGraphWidget( + data: perCategory, + ), + ); + } } Widget _item(IconData icon, String name, int percent, double value) { return ListTile( + onTap: () { + Navigator.of(context).pushNamed("/details", + arguments: DetailsParams(name, widget.month)); + }, leading: Icon(icon, size: 32.0,), title: Text(name, style: TextStyle( diff --git a/lib/pages/add_page.dart b/lib/pages/add_page.dart index deb9e81..51e0d5f 100644 --- a/lib/pages/add_page.dart +++ b/lib/pages/add_page.dart @@ -7,51 +7,101 @@ import 'package:provider/provider.dart'; import '../login_state.dart'; class AddPage extends StatefulWidget { + final Rect buttonRect; + + const AddPage({Key key, this.buttonRect}) : super(key: key); + @override _AddPageState createState() => _AddPageState(); } -class _AddPageState extends State { +class _AddPageState extends State with SingleTickerProviderStateMixin { + AnimationController _controller; + Animation _buttonAnimation; + Animation _pageAnimation; + String category; int value = 0; + @override + void initState() { + super.initState(); + + _controller = AnimationController( + vsync: this, + duration: Duration(milliseconds: 750), + ); + + _buttonAnimation = Tween(begin: 0, end: 1.0).animate( + CurvedAnimation(parent: _controller, curve: Curves.fastOutSlowIn) + ); + + _pageAnimation = Tween(begin: -1, end: 1.0).animate( + CurvedAnimation(parent: _controller, curve: Curves.fastOutSlowIn) + ); + + _controller.addListener(() { + print(_controller.value); + setState(() {}); + }); + _controller.addStatusListener((status) { + if (status == AnimationStatus.dismissed) { + Navigator.of(context).pop(); + } + }); + + _controller.forward(); + } + @override Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar( - automaticallyImplyLeading: false, - backgroundColor: Colors.transparent, - elevation: 0.0, - title: Text( - "Category", - style: TextStyle( - color: Colors.grey, + var h = MediaQuery.of(context).size.height; + return Stack( + children: [ + Transform.translate( + offset: Offset(0, h * (1 - _pageAnimation.value)), + child: Scaffold( + appBar: AppBar( + automaticallyImplyLeading: false, + backgroundColor: Colors.transparent, + elevation: 0.0, + title: Text( + "Category", + style: TextStyle( + color: Colors.grey, + ), + ), + centerTitle: false, + actions: [ + IconButton( + icon: Icon( + Icons.close, + color: Colors.grey, + ), + onPressed: () { + _controller.reverse(); + }, + ) + ], + ), + body: _body(), ), ), - centerTitle: false, - actions: [ - IconButton( - icon: Icon( - Icons.close, - color: Colors.grey, - ), - onPressed: () { - Navigator.of(context).pop(); - }, - ) - ], - ), - body: _body(), + _submit(), + ], ); } Widget _body() { + var h = MediaQuery.of(context).size.height; return Column( children: [ _categorySelector(), _currentValue(), _numpad(), - _submit(), + SizedBox( + height: h - widget.buttonRect.top, + ) ], ); } @@ -169,45 +219,81 @@ class _AddPageState extends State { } Widget _submit() { - return Builder(builder: (BuildContext context) { - return Hero( - tag: "add_button", - child: Container( - height: 50.0, - width: double.infinity, - decoration: BoxDecoration(color: Colors.blueAccent), - child: MaterialButton( - child: Text( - "Add expense", - style: TextStyle( - color: Colors.white, - fontSize: 20.0, + if (_controller.value < 1) { + var buttonWidth = widget.buttonRect.right - widget.buttonRect.left; + var w = MediaQuery.of(context).size.width; + + return Positioned( + left: widget.buttonRect.left * (1 - _buttonAnimation.value), + //<-- Margin from left + right: (w - widget.buttonRect.right) * (1 - _buttonAnimation.value), + //<-- Margin from right + top: widget.buttonRect.top, + //<-- Margin from top + bottom: (MediaQuery.of(context).size.height - widget.buttonRect.bottom) * + (1 - _buttonAnimation.value), + //<-- Margin from bottom + child: Container( + width: double.infinity, + //<-- Blue cirle + decoration: BoxDecoration( + borderRadius: BorderRadius.circular( + buttonWidth * (1 - _buttonAnimation.value)), + color: Colors.blueAccent, + ), + child: MaterialButton( + child: Text( + "Add expense", + style: TextStyle( + color: Colors.white, + fontSize: 20.0, + ), ), ), - onPressed: () { - var user = Provider.of(context).currentUser(); - if (value > 0 && category != "") { - Firestore.instance - .collection('users') - .document(user.uid) - .collection('expenses') - .document() - .setData({ - "category": category, - "value": value / 100.0, - "month": DateTime.now().month, - "day": DateTime.now().day, - }); - - Navigator.of(context).pop(); - } else { - Scaffold.of(context).showSnackBar( - SnackBar(content: Text("Select a value and a cagtegory"))); - } - }, ), - ), + ); + } else { + return Positioned( + top: widget.buttonRect.top, + bottom: 0, + left: 0, + right: 0, + child: Builder(builder: (BuildContext context) { + return Container( + decoration: BoxDecoration(color: Colors.blueAccent), + child: MaterialButton( + child: Text( + "Add expense", + style: TextStyle( + color: Colors.white, + fontSize: 20.0, + ), + ), + onPressed: () { + var user = Provider.of(context).currentUser(); + if (value > 0 && category != "") { + Firestore.instance + .collection('users') + .document(user.uid) + .collection('expenses') + .document() + .setData({ + "category": category, + "value": value / 100.0, + "month": DateTime.now().month, + "day": DateTime.now().day, + }); + + Navigator.of(context).pop(); + } else { + Scaffold.of(context).showSnackBar(SnackBar( + content: Text("Select a value and a cagtegory"))); + } + }, + ), + ); + }), ); - }); + } } } diff --git a/lib/pages/details_page.dart b/lib/pages/details_page.dart new file mode 100644 index 0000000..ec0df0e --- /dev/null +++ b/lib/pages/details_page.dart @@ -0,0 +1,110 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; + +import '../login_state.dart'; + +class DetailsParams { + final String categoryName; + final int month; + + DetailsParams(this.categoryName, this.month); +} + +class DetailsPage extends StatefulWidget { + final DetailsParams params; + + const DetailsPage({Key key, this.params}) : super(key: key); + + @override + _DetailsPageState createState() => _DetailsPageState(); +} + +class _DetailsPageState extends State { + @override + Widget build(BuildContext context) { + return Consumer( + builder: (BuildContext context, LoginState state, Widget child) { + var user = Provider.of(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(widget.params.categoryName), + ), + body: StreamBuilder( + stream: _query, + builder: + (BuildContext context, AsyncSnapshot 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: [ + 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(), + ); + }, + )); + }, + ); + } +} diff --git a/lib/pages/home_page.dart b/lib/pages/home_page.dart index 924ae2e..0c1e058 100644 --- a/lib/pages/home_page.dart +++ b/lib/pages/home_page.dart @@ -1,8 +1,11 @@ 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 '../login_state.dart'; import '../utils.dart'; @@ -13,9 +16,13 @@ class HomePage extends StatefulWidget { } class _HomePageState extends State { + var globalKey = RectGetter.createGlobalKey(); + Rect buttonRect; + PageController _controller; int currentPage = DateTime.now().month - 1; Stream _query; + GraphType currentType = GraphType.LINES; @override void initState() { @@ -57,22 +64,44 @@ class _HomePageState extends State { mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ - _bottomAction(FontAwesomeIcons.history, () {}), - _bottomAction(FontAwesomeIcons.chartPie, () {}), + _bottomAction(FontAwesomeIcons.chartLine, () { + setState(() { + currentType = GraphType.LINES; + }); + }), + _bottomAction(FontAwesomeIcons.chartPie, () { + setState(() { + currentType = GraphType.PIE; + }); + }), SizedBox(width: 48.0), _bottomAction(FontAwesomeIcons.wallet, () {}), - _bottomAction(Icons.settings, () { + _bottomAction(FontAwesomeIcons.signOutAlt, () { Provider.of(context).logout(); }), ], ), ), floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked, - floatingActionButton: FloatingActionButton( - child: Icon(Icons.add), - onPressed: () { - Navigator.of(context).pushNamed('/add'); - }, + floatingActionButton: RectGetter( + key: globalKey, + child: FloatingActionButton( + child: Icon(Icons.add), + onPressed: () { + buttonRect = RectGetter.getRectFromKey(globalKey); + + print(buttonRect); + + var page = AddPageTransition( + background: widget, + page: AddPage( + buttonRect: buttonRect, + ), + ); + + Navigator.of(context).push(page); + }, + ), ), body: _body(), ); @@ -91,6 +120,8 @@ class _HomePageState extends State { return MonthWidget( days: daysInMonth(currentPage + 1), documents: data.data.documents, + graphType: currentType, + month: currentPage, ); } diff --git a/pubspec.lock b/pubspec.lock index 0a8cd4c..ca5c3d5 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -151,6 +151,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.2" + rect_getter: + dependency: "direct main" + description: + name: rect_getter + url: "https://pub.dartlang.org" + source: hosted + version: "0.0.1" shared_preferences: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index c50d3b9..5aea7a1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -30,6 +30,7 @@ dependencies: google_sign_in: ^4.0.1+3 firebase_auth: ^0.11.1 shared_preferences: ^0.5.2 + rect_getter: ^0.0.1 dev_dependencies: flutter_test: