8000 Create feature login with Google · CoderJava/Flutter-News-App@4f430b2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4f430b2

Browse files
committed
Create feature login with Google
1 parent 2eaf4fa commit 4f430b2

File tree

3 files changed

+293
-4
lines changed

3 files changed

+293
-4
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import 'package:easy_localization/easy_localization.dart';
2+
import 'package:flutter/material.dart';
3+
import 'package:flutter_news_app/core/util/shared_preferences_manager.dart';
4+
import 'package:flutter_news_app/feature/presentation/page/login/login_page.dart';
5+
import 'package:flutter_news_app/injection_container.dart';
6+
7+
class DashboardPage extends StatefulWidget {
8+
static const routeName = '/dashboard_page';
9+
10+
const DashboardPage({Key? key}) : super(key: key);
11+
12+
@override
13+
State<DashboardPage> createState() => _DashboardPageState();
14+
}
15+
16+
class _DashboardPageState extends State<DashboardPage> {
17+
final sharedPreferencesManager = sl<SharedPreferencesManager>();
18+
19+
@override
20+
Widget build(BuildContext context) {
21+
return Scaffold(
22+
body: Center(
23+
child: ElevatedButton(
24+
onPressed: () async {
25+
await sharedPreferencesManager.clearAll();
26+
if (mounted) {
27+
Navigator.pushReplacementNamed(context, LoginPage.routeName);
28+
}
29+
},
30+
child: Text('sign_out'.tr()),
31+
),
32+
),
33+
);
34+
}
35+
}
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
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+
}

lib/feature/presentation/page/splash/splash_page.dart

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter_news_app/core/util/constant_value.dart';
33
import 'package:flutter_news_app/core/util/images.dart';
4+
import 'package:flutter_news_app/core/util/shared_preferences_manager.dart';
5+
import 'package:flutter_news_app/feature/presentation/page/dashboard/dashboard_page.dart';
6+
import 'package:flutter_news_app/feature/presentation/page/login/login_page.dart';
7+
import 'package:flutter_news_app/injection_container.dart';
48
import 'package:flutter_svg/flutter_svg.dart';
59

6-
class SplashPage extends StatelessWidget {
7-
SplashPage({Key? key}) : super(key: key);
10+
class SplashPage extends StatefulWidget {
11+
static const routeName = '/splash_page';
812

13+
const SplashPage({Key? key}) : super(key: key);
14+
15+
@override
16+
State<SplashPage> createState() => _SplashPageState();
17+
}
18+
19+
class _SplashPageState extends State<SplashPage> {
920
final constantColor = ConstantColor();
21+
final sharedPreferencesManager = sl<SharedPreferencesManager>();
22+
23+
@override
24+
void initState() {
25+
Future.delayed(const Duration(seconds: 1)).then((_) {
26+
final isLogin = sharedPreferencesManager.getBool(SharedPreferencesManager.keyIsLogin) ?? false;
27+
Navigator.pushReplacementNamed(context, isLogin ? DashboardPage.routeName : LoginPage.routeName);
28+
});
29+
super.initState();
30+
}
1031

1132
@override
1233
Widget build(BuildContext context) {
@@ -16,8 +37,8 @@ class SplashPage extends StatelessWidget {
1637
backgroundColor: constantColor.primaryColor500,
1738
body: Center(
1839
child: SvgPicture.asset(
19-
AssetSvg.logoFlutterNewsRectangle,
20-
width: widthScreen / 2,
40+
AssetsSvg.logoFlutterNewsRectangle,
41+
width: widthScreen / 3,
2142
),
2243
),
2344
);

0 commit comments

Comments
 (0)
0