Add screen to add expenses
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class CategorySelectionWidget extends StatefulWidget {
|
||||
final Map<String, IconData> categories;
|
||||
final Function(String) onValueChanged;
|
||||
|
||||
const CategorySelectionWidget({Key key, this.categories, this.onValueChanged}) : super(key: key);
|
||||
|
||||
@override
|
||||
_CategorySelectionWidgetState createState() =>
|
||||
_CategorySelectionWidgetState();
|
||||
}
|
||||
|
||||
class CategoryWidget extends StatelessWidget {
|
||||
final String name;
|
||||
final IconData icon;
|
||||
final bool selected;
|
||||
|
||||
const CategoryWidget({Key key, this.name, this.icon, this.selected})
|
||||
: super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16.0),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
Container(
|
||||
width: 50,
|
||||
height: 50,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(25.0),
|
||||
border: Border.all(
|
||||
color: selected ? Colors.blueAccent : Colors.grey,
|
||||
width: selected ? 3.0 : 1.0,
|
||||
),
|
||||
),
|
||||
child: Icon(icon),
|
||||
),
|
||||
Text(name),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _CategorySelectionWidgetState extends State<CategorySelectionWidget> {
|
||||
String currentItem = "Shopping";
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var widgets = <Widget>[];
|
||||
|
||||
widget.categories.forEach((name, icon) {
|
||||
widgets.add(GestureDetector(
|
||||
onTap: () {
|
||||
setState(() {
|
||||
currentItem = name;
|
||||
});
|
||||
widget.onValueChanged(name);
|
||||
},
|
||||
child: CategoryWidget(
|
||||
name: name,
|
||||
icon: icon,
|
||||
selected: name == currentItem,
|
||||
),
|
||||
));
|
||||
});
|
||||
return ListView(
|
||||
scrollDirection: Axis.horizontal,
|
||||
children: widgets,
|
||||
);
|
||||
}
|
||||
}
|
||||
+6
-152
@@ -1,8 +1,6 @@
|
||||
import 'package:como_gasto/graph_widget.dart';
|
||||
import 'package:como_gasto/month_widget.dart';
|
||||
import 'package:como_gasto/pages/add_page.dart';
|
||||
import 'package:como_gasto/pages/home_page.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());
|
||||
|
||||
@@ -14,156 +12,12 @@ class MyApp extends StatelessWidget {
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.blue,
|
||||
),
|
||||
home: HomePage(),
|
||||
routes: {
|
||||
'/': (BuildContext contex) => HomePage(),
|
||||
'/add': (BuildContext contex) => AddPage(),
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class HomePage extends StatefulWidget {
|
||||
@override
|
||||
_HomePageState createState() => _HomePageState();
|
||||
}
|
||||
|
||||
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,
|
||||
);
|
||||
}
|
||||
|
||||
Widget _bottomAction(IconData icon) {
|
||||
return InkWell(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Icon(icon),
|
||||
),
|
||||
onTap: () {},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
bottomNavigationBar: BottomAppBar(
|
||||
notchMargin: 8.0,
|
||||
shape: CircularNotchedRectangle(),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.max,
|
||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||
children: <Widget>[
|
||||
_bottomAction(FontAwesomeIcons.history),
|
||||
_bottomAction(FontAwesomeIcons.chartPie),
|
||||
SizedBox(width: 48.0),
|
||||
_bottomAction(FontAwesomeIcons.wallet),
|
||||
_bottomAction(Icons.settings),
|
||||
],
|
||||
),
|
||||
),
|
||||
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
|
||||
floatingActionButton: FloatingActionButton(
|
||||
child: Icon(Icons.add),
|
||||
onPressed: () {},
|
||||
),
|
||||
body: _body(),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _body() {
|
||||
return SafeArea(
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
_selector(),
|
||||
StreamBuilder<QuerySnapshot>(
|
||||
stream: _query,
|
||||
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> data) {
|
||||
if (data.hasData) {
|
||||
return MonthWidget(
|
||||
documents: data.data.documents,
|
||||
);
|
||||
}
|
||||
|
||||
return Center(
|
||||
child: CircularProgressIndicator(),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _pageItem(String name, int position) {
|
||||
var _alignment;
|
||||
final selected = TextStyle(
|
||||
fontSize: 20.0,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.blueGrey,
|
||||
);
|
||||
final unselected = TextStyle(
|
||||
fontSize: 20.0,
|
||||
fontWeight: FontWeight.normal,
|
||||
color: Colors.blueGrey.withOpacity(0.4),
|
||||
);
|
||||
|
||||
if (position == currentPage) {
|
||||
_alignment = Alignment.center;
|
||||
} else if (position > currentPage) {
|
||||
_alignment = Alignment.centerRight;
|
||||
} else {
|
||||
_alignment = Alignment.centerLeft;
|
||||
}
|
||||
|
||||
return Align(
|
||||
alignment: _alignment,
|
||||
child: Text(name,
|
||||
style: position == currentPage ? selected : unselected,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _selector() {
|
||||
return SizedBox.fromSize(
|
||||
size: Size.fromHeight(70.0),
|
||||
child: PageView(
|
||||
onPageChanged: (newPage) {
|
||||
setState(() {
|
||||
currentPage = newPage;
|
||||
_query = Firestore.instance
|
||||
.collection('expenses')
|
||||
.where("month", isEqualTo: currentPage + 1)
|
||||
.snapshots();
|
||||
});
|
||||
},
|
||||
controller: _controller,
|
||||
children: <Widget>[
|
||||
_pageItem("Enero", 0),
|
||||
_pageItem("Febrero", 1),
|
||||
_pageItem("Marzo", 2),
|
||||
_pageItem("Abril", 3),
|
||||
_pageItem("Mayo", 4),
|
||||
_pageItem("Junio", 5),
|
||||
_pageItem("Julio", 6),
|
||||
_pageItem("Agosto", 7),
|
||||
_pageItem("Septiembre", 8),
|
||||
_pageItem("Octubre", 9),
|
||||
_pageItem("Noviembre", 10),
|
||||
_pageItem("Diciembre", 11),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,207 @@
|
||||
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';
|
||||
|
||||
class AddPage extends StatefulWidget {
|
||||
@override
|
||||
_AddPageState createState() => _AddPageState();
|
||||
}
|
||||
|
||||
class _AddPageState extends State<AddPage> {
|
||||
String category;
|
||||
int value = 0;
|
||||
|
||||
@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,
|
||||
),
|
||||
),
|
||||
centerTitle: false,
|
||||
actions: <Widget>[
|
||||
IconButton(
|
||||
icon: Icon(
|
||||
Icons.close,
|
||||
color: Colors.grey,
|
||||
),
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
)
|
||||
],
|
||||
),
|
||||
body: _body(),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _body() {
|
||||
return Column(
|
||||
children: <Widget>[
|
||||
_categorySelector(),
|
||||
_currentValue(),
|
||||
_numpad(),
|
||||
_submit(),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _categorySelector() {
|
||||
return Container(
|
||||
height: 80.0,
|
||||
child: CategorySelectionWidget(
|
||||
categories: {
|
||||
"Shopping": Icons.shopping_cart,
|
||||
"Alcohol": FontAwesomeIcons.beer,
|
||||
"Fast food": FontAwesomeIcons.hamburger,
|
||||
"Bills": FontAwesomeIcons.wallet,
|
||||
},
|
||||
onValueChanged: (newCategory) => category = newCategory,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _currentValue() {
|
||||
var realValue = value / 100.0;
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 32.0),
|
||||
child: Text(
|
||||
"\$${realValue.toStringAsFixed(2)}",
|
||||
style: TextStyle(
|
||||
fontSize: 50.0,
|
||||
color: Colors.blueAccent,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _num(String text, double height) {
|
||||
return GestureDetector(
|
||||
behavior: HitTestBehavior.opaque,
|
||||
onTap: () {
|
||||
setState(() {
|
||||
if (text == ",") {
|
||||
value = value * 100;
|
||||
} else {
|
||||
value = value * 10 + int.parse(text);
|
||||
}
|
||||
});
|
||||
},
|
||||
child: Container(
|
||||
height: height,
|
||||
child: Center(
|
||||
child: Text(
|
||||
text,
|
||||
style: TextStyle(
|
||||
fontSize: 40,
|
||||
color: Colors.grey,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _numpad() {
|
||||
return Expanded(
|
||||
child: LayoutBuilder(
|
||||
builder: (BuildContext context, BoxConstraints constraints) {
|
||||
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,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
]),
|
||||
],
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
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,
|
||||
),
|
||||
),
|
||||
onPressed: () {
|
||||
if (value > 0 && category != "") {
|
||||
Firestore.instance
|
||||
.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")));
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:como_gasto/month_widget.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
||||
|
||||
class HomePage extends StatefulWidget {
|
||||
@override
|
||||
_HomePageState createState() => _HomePageState();
|
||||
}
|
||||
|
||||
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,
|
||||
);
|
||||
}
|
||||
|
||||
Widget _bottomAction(IconData icon) {
|
||||
return InkWell(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Icon(icon),
|
||||
),
|
||||
onTap: () {},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
bottomNavigationBar: BottomAppBar(
|
||||
notchMargin: 8.0,
|
||||
shape: CircularNotchedRectangle(),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.max,
|
||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||
children: <Widget>[
|
||||
_bottomAction(FontAwesomeIcons.history),
|
||||
_bottomAction(FontAwesomeIcons.chartPie),
|
||||
SizedBox(width: 48.0),
|
||||
_bottomAction(FontAwesomeIcons.wallet),
|
||||
_bottomAction(Icons.settings),
|
||||
],
|
||||
),
|
||||
),
|
||||
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
|
||||
floatingActionButton: FloatingActionButton(
|
||||
child: Hero(
|
||||
tag: "add_button",
|
||||
child: Icon(Icons.add),
|
||||
),
|
||||
onPressed: () {
|
||||
Navigator.of(context).pushNamed('/add');
|
||||
},
|
||||
),
|
||||
body: _body(),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _body() {
|
||||
return SafeArea(
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
_selector(),
|
||||
StreamBuilder<QuerySnapshot>(
|
||||
stream: _query,
|
||||
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> data) {
|
||||
if (data.hasData) {
|
||||
return MonthWidget(
|
||||
documents: data.data.documents,
|
||||
);
|
||||
}
|
||||
|
||||
return Center(
|
||||
child: CircularProgressIndicator(),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _pageItem(String name, int position) {
|
||||
var _alignment;
|
||||
final selected = TextStyle(
|
||||
fontSize: 20.0,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.blueGrey,
|
||||
);
|
||||
final unselected = TextStyle(
|
||||
fontSize: 20.0,
|
||||
fontWeight: FontWeight.normal,
|
||||
color: Colors.blueGrey.withOpacity(0.4),
|
||||
);
|
||||
|
||||
if (position == currentPage) {
|
||||
_alignment = Alignment.center;
|
||||
} else if (position > currentPage) {
|
||||
_alignment = Alignment.centerRight;
|
||||
} else {
|
||||
_alignment = Alignment.centerLeft;
|
||||
}
|
||||
|
||||
return Align(
|
||||
alignment: _alignment,
|
||||
child: Text(
|
||||
name,
|
||||
style: position == currentPage ? selected : unselected,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _selector() {
|
||||
return SizedBox.fromSize(
|
||||
size: Size.fromHeight(70.0),
|
||||
child: PageView(
|
||||
onPageChanged: (newPage) {
|
||||
setState(() {
|
||||
currentPage = newPage;
|
||||
_query = Firestore.instance
|
||||
.collection('expenses')
|
||||
.where("month", isEqualTo: currentPage + 1)
|
||||
.snapshots();
|
||||
});
|
||||
},
|
||||
controller: _controller,
|
||||
children: <Widget>[
|
||||
_pageItem("Enero", 0),
|
||||
_pageItem("Febrero", 1),
|
||||
_pageItem("Marzo", 2),
|
||||
_pageItem("Abril", 3),
|
||||
_pageItem("Mayo", 4),
|
||||
_pageItem("Junio", 5),
|
||||
_pageItem("Julio", 6),
|
||||
_pageItem("Agosto", 7),
|
||||
_pageItem("Septiembre", 8),
|
||||
_pageItem("Octubre", 9),
|
||||
_pageItem("Noviembre", 10),
|
||||
_pageItem("Diciembre", 11),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user