4. ubah login_screen jadi hook consumer widget
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../app/route.dart';
|
||||
import '../test_map.dart';
|
||||
// import '../test_map.dart';
|
||||
|
||||
void main() {
|
||||
runApp(const MyApp());
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
runApp(const ProviderScope(child: MyApp()));
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
|
||||
13
lib/provider/google_login_provider.dart
Normal file
13
lib/provider/google_login_provider.dart
Normal file
@@ -0,0 +1,13 @@
|
||||
import 'package:google_sign_in/google_sign_in.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
final googleSignInProvider = StateProvider<GoogleSignIn>((ref) {
|
||||
return GoogleSignIn(
|
||||
scopes: [
|
||||
'email',
|
||||
'https://www.googleapis.com/auth/contacts.readonly',
|
||||
],
|
||||
);
|
||||
});
|
||||
|
||||
final currentUserProvider = StateProvider<GoogleSignInAccount?>((ref) => null);
|
||||
@@ -1,48 +1,44 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:google_sign_in/google_sign_in.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
|
||||
import '../../app/constant.dart';
|
||||
import '../../provider/google_login_provider.dart';
|
||||
|
||||
class LoginScreen extends StatefulWidget {
|
||||
class LoginScreen extends HookConsumerWidget {
|
||||
const LoginScreen({super.key});
|
||||
|
||||
@override
|
||||
State<LoginScreen> createState() => _LoginScreenState();
|
||||
}
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
|
||||
// inisialisasi
|
||||
// ref.watch itu sama spt memantau state terus menerus
|
||||
// ref.read itu memantau namun hny 1x saja
|
||||
GoogleSignInAccount? currentUser = ref.watch(currentUserProvider);
|
||||
final googleSignIn = ref.watch(googleSignInProvider);
|
||||
|
||||
class _LoginScreenState extends State<LoginScreen> {
|
||||
GoogleSignInAccount? _currentUser;
|
||||
final GoogleSignIn _googleSignIn = GoogleSignIn(
|
||||
scopes: [
|
||||
'email',
|
||||
'https://www.googleapis.com/auth/contacts.readonly',
|
||||
],
|
||||
);
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
_googleSignIn.onCurrentUserChanged.listen((account) {
|
||||
setState(() {
|
||||
_currentUser = account;
|
||||
useEffect(() {
|
||||
googleSignIn.onCurrentUserChanged.listen((account) {
|
||||
// untuk update value ke provider google_login_provider yaitu currentUserProvider
|
||||
ref.read(currentUserProvider.notifier).update((state) => account);
|
||||
});
|
||||
});
|
||||
_googleSignIn.signInSilently();
|
||||
super.initState();
|
||||
}
|
||||
googleSignIn.signInSilently();
|
||||
return (){};
|
||||
}, const []);
|
||||
|
||||
Future<void> _handleSignIn() async {
|
||||
try {
|
||||
await _googleSignIn.signIn();
|
||||
} catch (error) {
|
||||
if (kDebugMode) {
|
||||
print(error);
|
||||
// fungsi untuk sync ke google mail
|
||||
Future<void> handleSignIn() async {
|
||||
try {
|
||||
await googleSignIn.signIn();
|
||||
} catch (error) {
|
||||
if (kDebugMode) {
|
||||
print(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: SafeArea(
|
||||
child: SingleChildScrollView(
|
||||
@@ -51,22 +47,22 @@ class _LoginScreenState extends State<LoginScreen> {
|
||||
SizedBox(
|
||||
height: Constant.getActualYPhone(context: context, y: 100),
|
||||
),
|
||||
_currentUser != null
|
||||
(currentUser != null)
|
||||
? Container(
|
||||
child: ListTile(
|
||||
leading: GoogleUserCircleAvatar(
|
||||
identity: _currentUser!,
|
||||
identity: currentUser,
|
||||
),
|
||||
title: Text(
|
||||
_currentUser!.displayName ?? "",
|
||||
currentUser.displayName ?? "",
|
||||
),
|
||||
subtitle: Text(
|
||||
_currentUser!.email,
|
||||
currentUser.email,
|
||||
),
|
||||
trailing: IconButton(
|
||||
icon: Icon(Icons.logout_outlined),
|
||||
onPressed: () async {
|
||||
await _googleSignIn.disconnect();
|
||||
await googleSignIn.disconnect();
|
||||
},
|
||||
),
|
||||
),
|
||||
@@ -157,7 +153,7 @@ class _LoginScreenState extends State<LoginScreen> {
|
||||
height: Constant.getActualYPhone(context: context, y: 48),
|
||||
child: ElevatedButton(
|
||||
onPressed: () async {
|
||||
await _handleSignIn();
|
||||
await handleSignIn();
|
||||
},
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
|
||||
Reference in New Issue
Block a user