4. ubah login_screen jadi hook consumer widget

This commit is contained in:
sindhu
2024-01-09 10:40:32 +07:00
parent e686c6f7a1
commit 7bbce2f3fd
3 changed files with 50 additions and 39 deletions

View File

@@ -1,9 +1,11 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../app/route.dart'; import '../app/route.dart';
import '../test_map.dart'; // import '../test_map.dart';
void main() { void main() async {
runApp(const MyApp()); WidgetsFlutterBinding.ensureInitialized();
runApp(const ProviderScope(child: MyApp()));
} }
class MyApp extends StatelessWidget { class MyApp extends StatelessWidget {

View 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);

View File

@@ -1,48 +1,44 @@
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:google_sign_in/google_sign_in.dart'; import 'package:google_sign_in/google_sign_in.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import '../../app/constant.dart'; import '../../app/constant.dart';
import '../../provider/google_login_provider.dart';
class LoginScreen extends StatefulWidget { class LoginScreen extends HookConsumerWidget {
const LoginScreen({super.key}); const LoginScreen({super.key});
@override @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> { useEffect(() {
GoogleSignInAccount? _currentUser; googleSignIn.onCurrentUserChanged.listen((account) {
final GoogleSignIn _googleSignIn = GoogleSignIn( // untuk update value ke provider google_login_provider yaitu currentUserProvider
scopes: [ ref.read(currentUserProvider.notifier).update((state) => account);
'email',
'https://www.googleapis.com/auth/contacts.readonly',
],
);
@override
void initState() {
_googleSignIn.onCurrentUserChanged.listen((account) {
setState(() {
_currentUser = account;
}); });
}); googleSignIn.signInSilently();
_googleSignIn.signInSilently(); return (){};
super.initState(); }, const []);
}
Future<void> _handleSignIn() async { // fungsi untuk sync ke google mail
try { Future<void> handleSignIn() async {
await _googleSignIn.signIn(); try {
} catch (error) { await googleSignIn.signIn();
if (kDebugMode) { } catch (error) {
print(error); if (kDebugMode) {
print(error);
}
} }
} }
}
@override
Widget build(BuildContext context) {
return Scaffold( return Scaffold(
body: SafeArea( body: SafeArea(
child: SingleChildScrollView( child: SingleChildScrollView(
@@ -51,22 +47,22 @@ class _LoginScreenState extends State<LoginScreen> {
SizedBox( SizedBox(
height: Constant.getActualYPhone(context: context, y: 100), height: Constant.getActualYPhone(context: context, y: 100),
), ),
_currentUser != null (currentUser != null)
? Container( ? Container(
child: ListTile( child: ListTile(
leading: GoogleUserCircleAvatar( leading: GoogleUserCircleAvatar(
identity: _currentUser!, identity: currentUser,
), ),
title: Text( title: Text(
_currentUser!.displayName ?? "", currentUser.displayName ?? "",
), ),
subtitle: Text( subtitle: Text(
_currentUser!.email, currentUser.email,
), ),
trailing: IconButton( trailing: IconButton(
icon: Icon(Icons.logout_outlined), icon: Icon(Icons.logout_outlined),
onPressed: () async { onPressed: () async {
await _googleSignIn.disconnect(); await googleSignIn.disconnect();
}, },
), ),
), ),
@@ -157,7 +153,7 @@ class _LoginScreenState extends State<LoginScreen> {
height: Constant.getActualYPhone(context: context, y: 48), height: Constant.getActualYPhone(context: context, y: 48),
child: ElevatedButton( child: ElevatedButton(
onPressed: () async { onPressed: () async {
await _handleSignIn(); await handleSignIn();
}, },
child: Row( child: Row(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,