Add login
This commit is contained in:
@@ -46,7 +46,7 @@ class CategoryWidget extends StatelessWidget {
|
||||
}
|
||||
|
||||
class _CategorySelectionWidgetState extends State<CategorySelectionWidget> {
|
||||
String currentItem = "Shopping";
|
||||
String currentItem = "";
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:google_sign_in/google_sign_in.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class LoginState with ChangeNotifier {
|
||||
final GoogleSignIn _googleSignIn = GoogleSignIn();
|
||||
final FirebaseAuth _auth = FirebaseAuth.instance;
|
||||
SharedPreferences _prefs;
|
||||
|
||||
bool _loggedIn = false;
|
||||
bool _loading = true;
|
||||
FirebaseUser _user;
|
||||
|
||||
LoginState() {
|
||||
loginState();
|
||||
}
|
||||
|
||||
bool isLoggedIn() => _loggedIn;
|
||||
|
||||
bool isLoading() => _loading;
|
||||
|
||||
FirebaseUser currentUser() => _user;
|
||||
|
||||
void login() async {
|
||||
_loading = true;
|
||||
notifyListeners();
|
||||
|
||||
_user = await _handleSignIn();
|
||||
|
||||
_loading = false;
|
||||
if (_user != null) {
|
||||
_prefs.setBool('isLoggedIn', true);
|
||||
_loggedIn = true;
|
||||
notifyListeners();
|
||||
} else {
|
||||
_loggedIn = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
void logout() {
|
||||
_prefs.clear();
|
||||
_googleSignIn.signOut();
|
||||
_loggedIn = false;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<FirebaseUser> _handleSignIn() async {
|
||||
final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
|
||||
final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
|
||||
|
||||
final AuthCredential credential = GoogleAuthProvider.getCredential(
|
||||
accessToken: googleAuth.accessToken,
|
||||
idToken: googleAuth.idToken,
|
||||
);
|
||||
|
||||
final FirebaseUser user = await _auth.signInWithCredential(credential);
|
||||
print("signed in " + user.displayName);
|
||||
return user;
|
||||
}
|
||||
|
||||
void loginState() async {
|
||||
_prefs = await SharedPreferences.getInstance();
|
||||
if (_prefs.containsKey('isLoggedIn')) {
|
||||
_user = await _auth.currentUser();
|
||||
_loggedIn = _user != null;
|
||||
_loading = false;
|
||||
notifyListeners();
|
||||
} else {
|
||||
_loading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
}
|
||||
+21
-8
@@ -1,21 +1,34 @@
|
||||
import 'package:como_gasto/login_state.dart';
|
||||
import 'package:como_gasto/pages/add_page.dart';
|
||||
import 'package:como_gasto/pages/home_page.dart';
|
||||
import 'package:como_gasto/pages/login_page.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
void main() => runApp(MyApp());
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'Flutter Demo',
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.blue,
|
||||
return ChangeNotifierProvider<LoginState>(
|
||||
builder: (BuildContext context) => LoginState(),
|
||||
child: MaterialApp(
|
||||
title: 'Flutter Demo',
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.blue,
|
||||
),
|
||||
routes: {
|
||||
'/': (BuildContext context) {
|
||||
var state = Provider.of<LoginState>(context);
|
||||
if (state.isLoggedIn()) {
|
||||
return HomePage();
|
||||
} else {
|
||||
return LoginPage();
|
||||
}
|
||||
},
|
||||
'/add': (BuildContext contex) => AddPage(),
|
||||
},
|
||||
),
|
||||
routes: {
|
||||
'/': (BuildContext contex) => HomePage(),
|
||||
'/add': (BuildContext contex) => AddPage(),
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,11 +9,11 @@ class MonthWidget extends StatefulWidget {
|
||||
final List<double> perDay;
|
||||
final Map<String, double> categories;
|
||||
|
||||
MonthWidget({Key key, this.documents})
|
||||
MonthWidget({Key key, this.documents, days})
|
||||
:
|
||||
total = documents.map((doc) => doc['value'])
|
||||
.fold(0.0, (a, b) => a + b),
|
||||
perDay = List.generate(30, (int index) {
|
||||
perDay = List.generate(days, (int index) {
|
||||
return documents.where((doc) => doc['day'] == (index + 1))
|
||||
.map((doc) => doc['value'])
|
||||
.fold(0.0, (a, b) => a + b);
|
||||
|
||||
@@ -2,6 +2,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';
|
||||
|
||||
class AddPage extends StatefulWidget {
|
||||
@override
|
||||
@@ -182,8 +185,11 @@ class _AddPageState extends State<AddPage> {
|
||||
),
|
||||
),
|
||||
onPressed: () {
|
||||
var user = Provider.of<LoginState>(context).currentUser();
|
||||
if (value > 0 && category != "") {
|
||||
Firestore.instance
|
||||
.collection('users')
|
||||
.document(user.uid)
|
||||
.collection('expenses')
|
||||
.document()
|
||||
.setData({
|
||||
|
||||
+48
-32
@@ -2,6 +2,10 @@ 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
|
||||
@@ -10,61 +14,69 @@ class HomePage extends StatefulWidget {
|
||||
|
||||
class _HomePageState extends State<HomePage> {
|
||||
PageController _controller;
|
||||
int currentPage = 9;
|
||||
int currentPage = DateTime.now().month - 1;
|
||||
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) {
|
||||
Widget _bottomAction(IconData icon, Function callback) {
|
||||
return InkWell(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Icon(icon),
|
||||
),
|
||||
onTap: () {},
|
||||
onTap: callback,
|
||||
);
|
||||
}
|
||||
|
||||
@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),
|
||||
],
|
||||
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(),
|
||||
);
|
||||
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
|
||||
floatingActionButton: FloatingActionButton(
|
||||
child: Icon(Icons.add),
|
||||
onPressed: () {
|
||||
Navigator.of(context).pushNamed('/add');
|
||||
},
|
||||
),
|
||||
body: _body(),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Widget _body() {
|
||||
@@ -77,6 +89,7 @@ class _HomePageState extends State<HomePage> {
|
||||
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> data) {
|
||||
if (data.hasData) {
|
||||
return MonthWidget(
|
||||
days: daysInMonth(currentPage + 1),
|
||||
documents: data.data.documents,
|
||||
);
|
||||
}
|
||||
@@ -127,8 +140,11 @@ class _HomePageState extends State<HomePage> {
|
||||
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();
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../login_state.dart';
|
||||
|
||||
class LoginPage extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: Center(
|
||||
child: Consumer<LoginState>(
|
||||
builder: (BuildContext context, LoginState value, Widget child) {
|
||||
if (value.isLoading()) {
|
||||
return CircularProgressIndicator();
|
||||
} else {
|
||||
return child;
|
||||
}
|
||||
},
|
||||
child: RaisedButton(
|
||||
child: Text("Sign In"),
|
||||
onPressed: () {
|
||||
Provider.of<LoginState>(context).login();
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class Demo extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return
|
||||
Provider<String>.value(
|
||||
value: 'Hello World',
|
||||
child: Consumer<String>(
|
||||
builder: (context, value, child) => Text(value),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
|
||||
int daysInMonth(int month) {
|
||||
var now = DateTime.now();
|
||||
|
||||
var lastDayDateTime = (month < 12) ?
|
||||
new DateTime(now.year, month + 1, 0) : new DateTime(now.year + 1, 1, 0);
|
||||
|
||||
return lastDayDateTime.day;
|
||||
}
|
||||
Reference in New Issue
Block a user