Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. ```dart
  2. // Mobble App
  3. // https://mobble.app
  4. // https://github.com/nicholaschiang/mobble
  5.  
  6. import 'package:flutter/material.dart';
  7. import 'package:flutter/services.dart';
  8. import 'package:flutter_facebook_login/flutter_facebook_login.dart';
  9. import 'package:firebase_auth/firebase_auth.dart';
  10. import 'package:onesignal/onesignal.dart';
  11. import 'package:google_sign_in/google_sign_in.dart';
  12.  
  13. import 'package:mobble/widgets/login_button.dart';
  14. import 'package:mobble/model/user.dart';
  15.  
  16. class SplashScreen extends StatefulWidget {
  17. @override
  18. State<StatefulWidget> createState() => _SplashScreenState();
  19. }
  20.  
  21. class _SplashScreenState extends State<SplashScreen> {
  22. bool _loggedIn;
  23.  
  24. @override
  25. void initState() {
  26. super.initState();
  27. SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark.copyWith(
  28. statusBarColor: Colors.white, statusBarBrightness: Brightness.dark));
  29. User.loggedIn().then((val) {
  30. setState(() {
  31. _loggedIn = val;
  32. });
  33. if (_loggedIn) {
  34. Navigator.of(context).pushNamed('/home');
  35. }
  36. });
  37. WidgetsBinding.instance
  38. .addObserver(LifecycleEventHandler(resumeCallBack: () {
  39. User.isActive = true;
  40. }, suspendingCallBack: () {
  41. User.isActive = false;
  42. }));
  43. }
  44.  
  45. @override
  46. Widget build(BuildContext context) {
  47. double imageSize = MediaQuery.of(context).size.width / 3;
  48. return Scaffold(
  49. body: Center(
  50. child: Image.asset(
  51. "assets/images/logo.png",
  52. width: imageSize,
  53. height: imageSize,
  54. fit: BoxFit.cover,
  55. ),
  56. ),
  57. bottomNavigationBar: Padding(
  58. padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 16.0),
  59. child: Column(
  60. mainAxisSize: MainAxisSize.min,
  61. children: <Widget>[
  62. !_loggedIn || _loggedIn == null
  63. ? _buildGoogleButton()
  64. : Container(
  65. width: 0.0,
  66. height: 0.0,
  67. ),
  68. !_loggedIn || _loggedIn == null
  69. ? _buildFacebookButton()
  70. : Container(
  71. width: 0.0,
  72. height: 0.0,
  73. ),
  74. Padding(
  75. padding: const EdgeInsets.only(top: 20.0),
  76. child: Text(
  77. "By continuing, you agree to our Privacy Policy.",
  78. style: TextStyle(
  79. color: Colors.black45,
  80. fontSize: 14.0,
  81. fontWeight: FontWeight.w500),
  82. textAlign: TextAlign.center,
  83. ),
  84. )
  85. ],
  86. ),
  87. ),
  88. );
  89. }
  90.  
  91. void _googleLogin() async {
  92. final GoogleSignInAccount googleUser = await GoogleSignIn().signIn();
  93. final GoogleSignInAuthentication googleAuth =
  94. await googleUser.authentication;
  95. final AuthCredential cred = GoogleAuthProvider.getCredential(
  96. accessToken: googleAuth.accessToken,
  97. idToken: googleAuth.idToken,
  98. );
  99. FirebaseUser user =
  100. (await FirebaseAuth.instance.signInWithCredential(cred)).user;
  101. _continue();
  102. }
  103.  
  104. void _continue() async {
  105. setState(() {
  106. _loggedIn = true;
  107. });
  108. if (await User.exists) {
  109. User.isActive = true;
  110. Navigator.of(context).pushNamed('/home');
  111. } else {
  112. User.isActive = true;
  113. print('[TODO] Show onboarding flow to new users');
  114. Navigator.of(context).pushNamed('/home');
  115. }
  116. }
  117.  
  118. void _facebookLogin() async {
  119. var facebookLogin = FacebookLogin();
  120. var result = await facebookLogin.logIn(['email']);
  121. switch (result.status) {
  122. case FacebookLoginStatus.error:
  123. print('[ERROR] Something went wrong with the login process.\n'
  124. 'Here\'s the error Facebook gave us: ${result.errorMessage}');
  125. break;
  126. case FacebookLoginStatus.cancelledByUser:
  127. print('[WARNING] Login cancelled by the user');
  128. break;
  129. case FacebookLoginStatus.loggedIn:
  130. final token = result.accessToken.token;
  131. final AuthCredential credential =
  132. FacebookAuthProvider.getCredential(accessToken: token);
  133. final AuthResult authResult =
  134. await FirebaseAuth.instance.signInWithCredential(credential);
  135. _continue();
  136. break;
  137. }
  138. }
  139.  
  140. Widget _buildGoogleButton() {
  141. return Padding(
  142. padding: const EdgeInsets.only(bottom: 10.0),
  143. child: LoginButton("Continue with Google",
  144. color: Color(0xffffffff),
  145. onPressed: _googleLogin,
  146. leading: Image.asset(
  147. "assets/images/google.png",
  148. width: 24.0,
  149. height: 24.0,
  150. )));
  151. }
  152.  
  153. Widget _buildFacebookButton() {
  154. return LoginButton("Continue with Facebook",
  155. color: Color(0xff3b5998),
  156. onPressed: _facebookLogin,
  157. leading: Image.network(
  158. 'http://www.logospng.com/images/16/social-facebook-1489-1488-1494-' +
  159. '1500-1497-1491-1505-1493-1499-1504-1493-1514-1508-1512-1505-' +
  160. '1493-1501-1491-1497-1490-1497-1496-1500-1497-buzzlead-16424.png',
  161. color: Colors.white,
  162. width: 24.0,
  163. height: 24.0,
  164. ));
  165. }
  166. }
  167.  
  168. class LifecycleEventHandler extends WidgetsBindingObserver {
  169. LifecycleEventHandler({this.resumeCallBack, this.suspendingCallBack});
  170.  
  171. final Function resumeCallBack;
  172. final Function suspendingCallBack;
  173.  
  174. @override
  175. Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
  176. switch (state) {
  177. case AppLifecycleState.inactive:
  178. case AppLifecycleState.paused:
  179. case AppLifecycleState.suspending:
  180. await suspendingCallBack();
  181. break;
  182. case AppLifecycleState.resumed:
  183. await resumeCallBack();
  184. break;
  185. }
  186. }
  187. }
  188. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement