Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```dart
- // Mobble App
- // https://mobble.app
- // https://github.com/nicholaschiang/mobble
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:flutter_facebook_login/flutter_facebook_login.dart';
- import 'package:firebase_auth/firebase_auth.dart';
- import 'package:onesignal/onesignal.dart';
- import 'package:google_sign_in/google_sign_in.dart';
- import 'package:mobble/widgets/login_button.dart';
- import 'package:mobble/model/user.dart';
- class SplashScreen extends StatefulWidget {
- @override
- State<StatefulWidget> createState() => _SplashScreenState();
- }
- class _SplashScreenState extends State<SplashScreen> {
- bool _loggedIn;
- @override
- void initState() {
- super.initState();
- SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark.copyWith(
- statusBarColor: Colors.white, statusBarBrightness: Brightness.dark));
- User.loggedIn().then((val) {
- setState(() {
- _loggedIn = val;
- });
- if (_loggedIn) {
- Navigator.of(context).pushNamed('/home');
- }
- });
- WidgetsBinding.instance
- .addObserver(LifecycleEventHandler(resumeCallBack: () {
- User.isActive = true;
- }, suspendingCallBack: () {
- User.isActive = false;
- }));
- }
- @override
- Widget build(BuildContext context) {
- double imageSize = MediaQuery.of(context).size.width / 3;
- return Scaffold(
- body: Center(
- child: Image.asset(
- "assets/images/logo.png",
- width: imageSize,
- height: imageSize,
- fit: BoxFit.cover,
- ),
- ),
- bottomNavigationBar: Padding(
- padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 16.0),
- child: Column(
- mainAxisSize: MainAxisSize.min,
- children: <Widget>[
- !_loggedIn || _loggedIn == null
- ? _buildGoogleButton()
- : Container(
- width: 0.0,
- height: 0.0,
- ),
- !_loggedIn || _loggedIn == null
- ? _buildFacebookButton()
- : Container(
- width: 0.0,
- height: 0.0,
- ),
- Padding(
- padding: const EdgeInsets.only(top: 20.0),
- child: Text(
- "By continuing, you agree to our Privacy Policy.",
- style: TextStyle(
- color: Colors.black45,
- fontSize: 14.0,
- fontWeight: FontWeight.w500),
- textAlign: TextAlign.center,
- ),
- )
- ],
- ),
- ),
- );
- }
- void _googleLogin() async {
- final GoogleSignInAccount googleUser = await GoogleSignIn().signIn();
- final GoogleSignInAuthentication googleAuth =
- await googleUser.authentication;
- final AuthCredential cred = GoogleAuthProvider.getCredential(
- accessToken: googleAuth.accessToken,
- idToken: googleAuth.idToken,
- );
- FirebaseUser user =
- (await FirebaseAuth.instance.signInWithCredential(cred)).user;
- _continue();
- }
- void _continue() async {
- setState(() {
- _loggedIn = true;
- });
- if (await User.exists) {
- User.isActive = true;
- Navigator.of(context).pushNamed('/home');
- } else {
- User.isActive = true;
- print('[TODO] Show onboarding flow to new users');
- Navigator.of(context).pushNamed('/home');
- }
- }
- void _facebookLogin() async {
- var facebookLogin = FacebookLogin();
- var result = await facebookLogin.logIn(['email']);
- switch (result.status) {
- case FacebookLoginStatus.error:
- print('[ERROR] Something went wrong with the login process.\n'
- 'Here\'s the error Facebook gave us: ${result.errorMessage}');
- break;
- case FacebookLoginStatus.cancelledByUser:
- print('[WARNING] Login cancelled by the user');
- break;
- case FacebookLoginStatus.loggedIn:
- final token = result.accessToken.token;
- final AuthCredential credential =
- FacebookAuthProvider.getCredential(accessToken: token);
- final AuthResult authResult =
- await FirebaseAuth.instance.signInWithCredential(credential);
- _continue();
- break;
- }
- }
- Widget _buildGoogleButton() {
- return Padding(
- padding: const EdgeInsets.only(bottom: 10.0),
- child: LoginButton("Continue with Google",
- color: Color(0xffffffff),
- onPressed: _googleLogin,
- leading: Image.asset(
- "assets/images/google.png",
- width: 24.0,
- height: 24.0,
- )));
- }
- Widget _buildFacebookButton() {
- return LoginButton("Continue with Facebook",
- color: Color(0xff3b5998),
- onPressed: _facebookLogin,
- leading: Image.network(
- 'http://www.logospng.com/images/16/social-facebook-1489-1488-1494-' +
- '1500-1497-1491-1505-1493-1499-1504-1493-1514-1508-1512-1505-' +
- '1493-1501-1491-1497-1490-1497-1496-1500-1497-buzzlead-16424.png',
- color: Colors.white,
- width: 24.0,
- height: 24.0,
- ));
- }
- }
- class LifecycleEventHandler extends WidgetsBindingObserver {
- LifecycleEventHandler({this.resumeCallBack, this.suspendingCallBack});
- final Function resumeCallBack;
- final Function suspendingCallBack;
- @override
- Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
- switch (state) {
- case AppLifecycleState.inactive:
- case AppLifecycleState.paused:
- case AppLifecycleState.suspending:
- await suspendingCallBack();
- break;
- case AppLifecycleState.resumed:
- await resumeCallBack();
- break;
- }
- }
- }
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement