|
| 1 | +import 'package:easy_localization/easy_localization.dart'; |
| 2 | +import 'package:firebase_auth/firebase_auth.dart'; |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter_news_app/core/util/images.dart'; |
| 5 | +import 'package:flutter_news_app/core/util/shared_preferences_manager.dart'; |
| 6 | +import 'package:flutter_news_app/feature/presentation/page/dashboard/dashboard_page.dart'; |
| 7 | +import 'package:flutter_news_app/injection_container.dart'; |
| 8 | +import 'package:font_awesome_flutter/font_awesome_flutter.dart'; |
| 9 | +import 'package:google_sign_in/google_sign_in.dart'; |
| 10 | + |
| 11 | +class LoginPage extends StatefulWidget { |
| 12 | + static const routeName = '/login_page'; |
| 13 | + |
| 14 | + const LoginPage({Key? key}) : super(key: key); |
| 15 | + |
| 16 | + @override |
| 17 | + State<LoginPage> createState() => _LoginPageState(); |
| 18 | +} |
| 19 | + |
| 20 | +class _LoginPageState extends State<LoginPage> { |
| 21 | + final firebaseAuth = FirebaseAuth.instance; |
| 22 | + final sharedPreferencesManager = sl<SharedPreferencesManager>(); |
| 23 | + |
| 24 | + var heightScreen = 0.0; |
| 25 | + var paddingBottom = 0.0; |
| 26 | + |
| 27 | + @override |
| 28 | + Widget build(BuildContext context) { |
| 29 | + final mediaQueryData = MediaQuery.of(context); |
| 30 | + heightScreen = mediaQueryData.size.height; |
| 31 | + paddingBottom = mediaQueryData.padding.bottom; |
| 32 | + |
| 33 | + return Scaffold( |
| 34 | + backgroundColor: Colors.white, |
| 35 | + body: Stack( |
| 36 | + children: [ |
| 37 | + buildWidgetBackgroundImage(), |
| 38 | + buildWidgetOverlayBackgroundImage(), |
| 39 | + buildWidgetForm(), |
| 40 | + ], |
| 41 | + ), |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + Widget buildWidgetForm() { |
| 46 | + return Column( |
| 47 | + children: [ |
| 48 | + Container( |
| 49 | + height: heightScreen / 1.8, |
| 50 | + ), |
| 51 | + Expanded( |
| 52 | + child: Container( |
| 53 | + width: double.infinity, |
| 54 | + padding: EdgeInsets.only( |
| 55 | + left: 16, |
| 56 | + right: 16, |
| 57 | + bottom: paddingBottom + 16, |
| 58 | + ), |
| 59 | + child: Column( |
| 60 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 61 | + mainAxisAlignment: MainAxisAlignment.end, |
| 62 | + children: [ |
| 63 | + Text( |
| 64 | + 'smart_news_for_smart_people'.tr(), |
| 65 | + style: Theme.of(context).textTheme.headline6?.copyWith( |
| 66 | + fontSize: 28, |
| 67 | + ), |
| 68 | + maxLines: 3, |
| 69 | + ), |
| 70 | + const SizedBox(height: 8), |
| 71 | + Text( |
| 72 | + 'get_started_by_sign_in_now'.tr(), |
| 73 | + style: Theme.of(context).textTheme.bodyText2?.copyWith( |
| 74 | + color: Colors.grey, |
| 75 | + ), |
| 76 | + ), |
| 77 | + const SizedBox(height: 16), |
| 78 | + buildWidgetButtonSignIn( |
| 79 | + 'sign_in_with_google'.tr(), |
| 80 | + const Color(0xFFFC2F1E), |
| 81 | + FontAwesomeIcons.google, |
| 82 | + onPressed: () async { |
| 83 | + final userCredential = await signInWithGoogle(); |
| 84 | + sharedPreferencesManager.putBool(SharedPreferencesManager.keyIsLogin, true); |
| 85 | + sharedPreferencesManager.putString( |
| 86 | + SharedPreferencesManager.keyProfilePicture, |
| 87 | + userCredential.user?.photoURL ?? '-', |
| 88 | + ); |
| 89 | + sharedPreferencesManager.putString( |
| 90 | + SharedPreferencesManager.keyDisplayName, |
| 91 | + userCredential.user?.displayName ?? '-', |
| 92 | + ); |
| 93 | + sharedPreferencesManager.putString( |
| 94 | + SharedPreferencesManager.keyEmail, |
| 95 | + userCredential.user?.email ?? '-', |
| 96 | + ); |
| 97 | + await firebaseAuth.signOut(); |
| 98 | + if (mounted) { |
| 99 | + Navigator.pushReplacementNamed(context, DashboardPage.routeName); |
| 100 | + } |
| 101 | + }, |
| 102 | + ), |
| 103 | + buildWidgetButtonSignIn( |
| 104 | + 'sign_in_with_twitter'.tr(), |
| 105 | + const Color(0xFF6BAAE8), |
| 106 | + FontAwesomeIcons.twitter, |
| 107 | + onPressed: () { |
| 108 | + // TODO: Buat fitur sign in with twitter |
| 109 | + }, |
| 110 | + ), |
| 111 | + buildWidgetButtonSignIn( |
| 112 | + 'sign_in_with_github'.tr(), |
| 113 | + const Color(0xFF191717), |
| 114 | + FontAwesomeIcons.github, |
| 115 | + onPressed: () { |
| 116 | + // TODO: Buat fitur sign in with Github |
| 117 | + }, |
| 118 | + ), |
| 119 | + ], |
| 120 | + ), |
| 121 | + ), |
| 122 | + ), |
| 123 | + ], |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + Widget buildWidgetButtonSignIn( |
| 128 | + String text, |
| 129 | + Color backgroundColor, |
| 130 | + IconData icon, { |
| 131 | + required Function() onPressed, |
| 132 | + }) { |
| 133 | + return SizedBox( |
| 134 | + width: double.infinity, |
| 135 | + child: ElevatedButton( |
| 136 | + onPressed: onPressed, |
| 137 | + style: ElevatedButton.styleFrom( |
| 138 | + primary: backgroundColor, |
| 139 | + ), |
| 140 | + child: Stack( |
| 141 | + children: [ |
| 142 | + Center( |
| 143 | + child: Text( |
| 144 | + text, |
| 145 | + style: const TextStyle( |
| 146 | + color: Colors.white, |
| 147 | + ), |
| 148 | + ), |
| 149 | + ), |
| 150 | + Align( |
| 151 | + alignment: Alignment.centerLeft, |
| 152 | + child: Icon( |
| 153 | + icon, |
| 154 | + color: Colors.white, |
| 155 | + size: 18, |
| 156 | + ), |
| 157 | + ), |
| 158 | + ], |
| 159 | + ), |
| 160 | + ), |
| 161 | + ); |
| 162 | + } |
| 163 | + |
| 164 | + Widget buildWidgetOverlayBackgroundImage() { |
| 165 | + return Positioned( |
| 166 | + left: 0, |
| 167 | + top: 0, |
| 168 | + right: 0, |
| 169 | + child: Container( |
| 170 | + width: double.infinity, |
| 171 | + height: heightScreen / 1.8, |
| 172 | + decoration: BoxDecoration( |
| 173 | + gradient: LinearGradient( |
| 174 | + begin: Alignment.topCenter, |
| 175 | + end: Alignment.bottomCenter, |
| 176 | + colors: [ |
| 177 | + Colors.transparent, |
| 178 | + Colors.white.withOpacity(0.8), |
| 179 | + Colors.white, |
| 180 | + ], |
| 181 | + stops: const [ |
| 182 | + 0.0, |
| 183 | + 0.8, |
| 184 | + 1.0, |
| 185 | + ], |
| 186 | + ), |
| 187 | + ), |
| 188 | + ), |
| 189 | + ); |
| 190 | + } |
| 191 | + |
| 192 | + Widget buildWidgetBackgroundImage() { |
| 193 | + return Positioned( |
| 194 | + left: 0, |
| 195 | + top: 0, |
| 196 | + right: 0, |
| 197 | + child: Container( |
| 198 | + height: heightScreen / 1.8, |
| 199 | + decoration: BoxDecoration( |
| 200 | + gradient: LinearGradient( |
| 201 | + begin: Alignment.topCenter, |
| 202 | + end: Alignment.bottomCenter, |
| 203 | + colors: [ |
| 204 | + Colors.transparent, |
| 205 | + Colors.white.withOpacity(.5), |
| 206 | + Colors.white, |
| 207 | + ], |
| 208 | + stops: const [ |
| 209 | + 0.0, |
| 210 | + 0.5, |
| 211 | + 1, |
| 212 | + ], |
| 213 | + ), |
| 214 | + ), |
| 215 | + child: Image.asset( |
| 216 | + AssetsImage.newspaper, |
| 217 | + width: double.infinity, |
| 218 | + fit: BoxFit.cover, |
| 219 | + ), |
| 220 | + ), |
| 221 | + ); |
| 222 | + } |
| 223 | + |
| 224 | + Future<UserCredential> signInWithGoogle() async { |
| 225 | + final googleUser = await GoogleSignIn().signIn(); |
| 226 | + final googleAuth = await googleUser?.authentication; |
| 227 | + final credential = GoogleAuthProvider.credential( |
| 228 | + accessToken: googleAuth?.accessToken, |
| 229 | + idToken: googleAuth?.idToken, |
| 230 | + ); |
| 231 | + return await firebaseAuth.signInWithCredential(credential); |
| 232 | + } |
| 233 | +} |
0 commit comments