172 lines
4.7 KiB
Dart
172 lines
4.7 KiB
Dart
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';
|
|
import 'package:provider/provider.dart';
|
|
|
|
import '../login_state.dart';
|
|
import '../utils.dart';
|
|
|
|
class HomePage extends StatefulWidget {
|
|
@override
|
|
_HomePageState createState() => _HomePageState();
|
|
}
|
|
|
|
class _HomePageState extends State<HomePage> {
|
|
PageController _controller;
|
|
int currentPage = DateTime.now().month - 1;
|
|
Stream<QuerySnapshot> _query;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
|
|
_controller = PageController(
|
|
initialPage: currentPage,
|
|
viewportFraction: 0.4,
|
|
);
|
|
}
|
|
|
|
Widget _bottomAction(IconData icon, Function callback) {
|
|
return InkWell(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: Icon(icon),
|
|
),
|
|
onTap: callback,
|
|
);
|
|
}
|
|
|
|
@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 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, () {
|
|
Provider.of<LoginState>(context).logout();
|
|
}),
|
|
],
|
|
),
|
|
),
|
|
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
|
|
floatingActionButton: FloatingActionButton(
|
|
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(
|
|
days: daysInMonth(currentPage + 1),
|
|
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(() {
|
|
var user = Provider.of<LoginState>(context).currentUser();
|
|
currentPage = newPage;
|
|
_query = Firestore.instance
|
|
.collection('users')
|
|
.document(user.uid)
|
|
.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),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|