Add theme switcher
This commit is contained in:
@@ -1,78 +0,0 @@
|
||||
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();
|
||||
if (googleUser == null) {
|
||||
return null;
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
+45
-34
@@ -1,5 +1,6 @@
|
||||
import 'package:como_gasto/add_page_transition.dart';
|
||||
import 'package:como_gasto/login_state.dart';
|
||||
import 'package:como_gasto/others/add_page_transition.dart';
|
||||
import 'package:como_gasto/pages/settings_page.dart';
|
||||
import 'package:como_gasto/states/login_state.dart';
|
||||
import 'package:como_gasto/pages/add_page.dart';
|
||||
import 'package:como_gasto/pages/details_page_container.dart';
|
||||
import 'package:como_gasto/pages/home_page.dart';
|
||||
@@ -8,6 +9,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import 'expenses_repository.dart';
|
||||
import 'states/theme_state.dart';
|
||||
|
||||
void main() => runApp(MyApp());
|
||||
|
||||
@@ -16,6 +18,9 @@ class MyApp extends StatelessWidget {
|
||||
Widget build(BuildContext context) {
|
||||
return MultiProvider(
|
||||
providers: [
|
||||
ChangeNotifierProvider<ThemeState>(
|
||||
builder: (_) => ThemeState(),
|
||||
),
|
||||
ChangeNotifierProvider<LoginState>(
|
||||
builder: (BuildContext context) => LoginState(),
|
||||
),
|
||||
@@ -30,39 +35,45 @@ class MyApp extends StatelessWidget {
|
||||
},
|
||||
)
|
||||
],
|
||||
child: MaterialApp(
|
||||
title: 'Flutter Demo',
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.blue,
|
||||
),
|
||||
onGenerateRoute: (settings) {
|
||||
if (settings.name == '/details') {
|
||||
DetailsParams params = settings.arguments;
|
||||
return MaterialPageRoute(builder: (BuildContext context) {
|
||||
return DetailsPageContainer(
|
||||
params: params,
|
||||
);
|
||||
});
|
||||
} else if (settings.name == '/add') {
|
||||
Rect buttonRect = settings.arguments;
|
||||
child: Consumer<ThemeState>(
|
||||
builder: (context, state, child) {
|
||||
return MaterialApp(
|
||||
title: 'Flutter Demo',
|
||||
theme: state.currentTheme,
|
||||
onGenerateRoute: (settings) {
|
||||
if (settings.name == '/details') {
|
||||
DetailsParams params = settings.arguments;
|
||||
return MaterialPageRoute(builder: (BuildContext context) {
|
||||
return DetailsPageContainer(
|
||||
params: params,
|
||||
);
|
||||
});
|
||||
} else if (settings.name == '/add') {
|
||||
Rect buttonRect = settings.arguments;
|
||||
|
||||
return AddPageTransition(
|
||||
page: AddPage(
|
||||
buttonRect: buttonRect,
|
||||
),
|
||||
);
|
||||
}
|
||||
return null;
|
||||
},
|
||||
routes: {
|
||||
'/': (BuildContext context) {
|
||||
var state = Provider.of<LoginState>(context);
|
||||
if (state.isLoggedIn()) {
|
||||
return HomePage();
|
||||
} else {
|
||||
return LoginPage();
|
||||
}
|
||||
},
|
||||
return AddPageTransition(
|
||||
page: AddPage(
|
||||
buttonRect: buttonRect,
|
||||
),
|
||||
);
|
||||
} else if (settings.name == '/settings') {
|
||||
return MaterialPageRoute(builder: (BuildContext context) {
|
||||
return SettingsPage();
|
||||
});
|
||||
}
|
||||
return null;
|
||||
},
|
||||
routes: {
|
||||
'/': (BuildContext context) {
|
||||
var state = Provider.of<LoginState>(context);
|
||||
if (state.isLoggedIn()) {
|
||||
return HomePage();
|
||||
} else {
|
||||
return LoginPage();
|
||||
}
|
||||
},
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:como_gasto/graph_widget.dart';
|
||||
import 'package:como_gasto/others/graph_widget.dart';
|
||||
import 'package:como_gasto/pages/details_page_container.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
||||
@@ -1,6 +1,6 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:como_gasto/category_selection_widget.dart';
|
||||
import 'package:como_gasto/others/category_selection_widget.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
@@ -292,7 +292,7 @@ class _AddPageState extends State<AddPage> with SingleTickerProviderStateMixin {
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(
|
||||
buttonWidth * (1 - _buttonAnimation.value)),
|
||||
color: Colors.blueAccent,
|
||||
color: Theme.of(context).floatingActionButtonTheme.backgroundColor ?? Theme.of(context).primaryColor,
|
||||
),
|
||||
child: MaterialButton(
|
||||
onPressed: () {},
|
||||
@@ -314,7 +314,7 @@ class _AddPageState extends State<AddPage> with SingleTickerProviderStateMixin {
|
||||
right: 0,
|
||||
child: Builder(builder: (BuildContext context) {
|
||||
return Container(
|
||||
decoration: BoxDecoration(color: Colors.blueAccent),
|
||||
decoration: BoxDecoration(color: Theme.of(context).floatingActionButtonTheme.backgroundColor ?? Theme.of(context).primaryColor),
|
||||
child: MaterialButton(
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:como_gasto/month_widget.dart';
|
||||
import 'package:como_gasto/others/month_widget.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
@@ -7,7 +7,7 @@ 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 '../states/login_state.dart';
|
||||
import '../utils.dart';
|
||||
|
||||
class HomePage extends StatefulWidget {
|
||||
@@ -73,8 +73,9 @@ class _HomePageState extends State<HomePage> {
|
||||
}),
|
||||
SizedBox(width: 48.0),
|
||||
_bottomAction(FontAwesomeIcons.wallet, () {}),
|
||||
_bottomAction(FontAwesomeIcons.signOutAlt, () {
|
||||
Provider.of<LoginState>(context).logout();
|
||||
_bottomAction(Icons.settings, () {
|
||||
|
||||
Navigator.pushNamed(context, '/settings');
|
||||
}),
|
||||
],
|
||||
),
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import 'package:flare_flutter/flare_actor.dart';
|
||||
import 'package:flutter/gestures.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 '../states/login_state.dart';
|
||||
|
||||
class LoginPage extends StatefulWidget {
|
||||
@override
|
||||
@@ -54,11 +55,34 @@ class _LoginPageState extends State<LoginPage> {
|
||||
return child;
|
||||
}
|
||||
},
|
||||
child: RaisedButton(
|
||||
child: Text("Sign in with Google"),
|
||||
onPressed: () {
|
||||
Provider.of<LoginState>(context).login();
|
||||
},
|
||||
child: Container(
|
||||
width: 200,
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: <Widget>[
|
||||
FloatingActionButton(
|
||||
backgroundColor: Color(0xff4285F4),
|
||||
child: Icon(FontAwesomeIcons.google),
|
||||
onPressed: () {
|
||||
Provider.of<LoginState>(context).login(LoginProvider.GOOGLE);
|
||||
},
|
||||
),
|
||||
FloatingActionButton(
|
||||
backgroundColor: Color(0xff00aced),
|
||||
child: Icon(FontAwesomeIcons.twitter),
|
||||
onPressed: () {
|
||||
Provider.of<LoginState>(context).login(LoginProvider.TWITTER);
|
||||
},
|
||||
),
|
||||
FloatingActionButton(
|
||||
backgroundColor: Color(0xff3b5998),
|
||||
child: Icon(FontAwesomeIcons.facebook),
|
||||
onPressed: () {
|
||||
Provider.of<LoginState>(context).login(LoginProvider.FACEBOOK);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
import 'package:como_gasto/states/theme_state.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class SettingsPage extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text("Settings"),
|
||||
),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(12.0),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.max,
|
||||
children: <Widget>[
|
||||
Consumer<ThemeState>(
|
||||
builder: (context, state, child) {
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.max,
|
||||
children: <Widget>[
|
||||
Expanded(
|
||||
child: Text("Use Dark mode?"),
|
||||
),
|
||||
Switch(
|
||||
value: state.isDarkModeEnabled,
|
||||
onChanged: (_) {
|
||||
state.setDarkMode(!state.isDarkModeEnabled);
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
Expanded(
|
||||
child: Container(),
|
||||
),
|
||||
Container(
|
||||
width: double.infinity,
|
||||
child: RaisedButton(
|
||||
onPressed: () {
|
||||
//Provider.of<LoginState>(context).logout();
|
||||
},
|
||||
child: Text("Sign Out"),
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
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';
|
||||
|
||||
final String TWITTER_API = "...";
|
||||
final String TWITTER_SECRET = "...";
|
||||
|
||||
enum LoginProvider {
|
||||
GOOGLE,
|
||||
TWITTER,
|
||||
FACEBOOK,
|
||||
}
|
||||
|
||||
class LoginState with ChangeNotifier {
|
||||
final GoogleSignIn _googleSignIn = GoogleSignIn();
|
||||
final FirebaseAuth _auth = FirebaseAuth.instance;
|
||||
SharedPreferences _prefs;
|
||||
|
||||
// TODO: Set twitter credentials and uncomment logic
|
||||
// Also uncomment the dependency on the pubspec.yaml file
|
||||
// final TwitterLogin _twitterLogin = TwitterLogin(
|
||||
// consumerKey: TWITTER_API,
|
||||
// consumerSecret: TWITTER_SECRET,
|
||||
// );
|
||||
|
||||
bool _loggedIn = false;
|
||||
bool _loading = true;
|
||||
FirebaseUser _user;
|
||||
|
||||
LoginState() {
|
||||
loginState();
|
||||
}
|
||||
|
||||
bool isLoggedIn() => _loggedIn;
|
||||
|
||||
bool isLoading() => _loading;
|
||||
|
||||
FirebaseUser currentUser() => _user;
|
||||
|
||||
void login(LoginProvider loginProvider) async {
|
||||
_loading = true;
|
||||
notifyListeners();
|
||||
|
||||
switch (loginProvider) {
|
||||
case LoginProvider.GOOGLE:
|
||||
_user = await _handleSignIn();
|
||||
break;
|
||||
case LoginProvider.TWITTER:
|
||||
_user = await _handleTwitterSignIn();
|
||||
break;
|
||||
case LoginProvider.FACEBOOK:
|
||||
_user = await _handleFacebookSignIn();
|
||||
break;
|
||||
}
|
||||
|
||||
_loading = false;
|
||||
_loggedIn = _user != null;
|
||||
_prefs.setBool('isLoggedIn', _loggedIn);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void logout() {
|
||||
_prefs.clear();
|
||||
_googleSignIn.signOut();
|
||||
_loggedIn = false;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
// TODO: Set twitter credentials and uncomment logic
|
||||
// Also uncomment the dependency on the pubspec.yaml file
|
||||
Future<FirebaseUser> _handleFacebookSignIn() async {
|
||||
// final facebookLogin = FacebookLogin();
|
||||
// final result = await facebookLogin.logIn(['email']);
|
||||
// if (result.status != FacebookLoginStatus.loggedIn) {
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// final AuthCredential credential = FacebookAuthProvider.getCredential(
|
||||
// accessToken: result.accessToken.token,
|
||||
// );
|
||||
//
|
||||
// final FirebaseUser user = await _auth.signInWithCredential(credential);
|
||||
// print("signed in " + user.displayName);
|
||||
// return user;
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<FirebaseUser> _handleTwitterSignIn() async {
|
||||
// var twitterResult = await _twitterLogin.authorize();
|
||||
// if (twitterResult.status != TwitterLoginStatus.loggedIn) {
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// var session = twitterResult.session;
|
||||
//
|
||||
// final AuthCredential credential = TwitterAuthProvider.getCredential(
|
||||
// authToken: session.token,
|
||||
// authTokenSecret: session.secret,
|
||||
// );
|
||||
//
|
||||
// final FirebaseUser user = await _auth.signInWithCredential(credential);
|
||||
// print("signed in " + user.displayName);
|
||||
// return user;
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<FirebaseUser> _handleSignIn() async {
|
||||
final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
|
||||
if (googleUser == null) {
|
||||
return null;
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class ThemeState with ChangeNotifier {
|
||||
bool _isDarkModeEnabled = false;
|
||||
|
||||
ThemeData get currentTheme => _isDarkModeEnabled
|
||||
? ThemeData.dark().copyWith(
|
||||
accentColor: Colors.red,
|
||||
buttonTheme: ButtonThemeData(
|
||||
buttonColor: Colors.red,
|
||||
),
|
||||
floatingActionButtonTheme: FloatingActionButtonThemeData(
|
||||
backgroundColor: Colors.red,
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
toggleableActiveColor: Colors.red,
|
||||
)
|
||||
: ThemeData.light();
|
||||
|
||||
bool get isDarkModeEnabled => _isDarkModeEnabled;
|
||||
|
||||
void setDarkMode(bool b) {
|
||||
_isDarkModeEnabled = b;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user