Skip to content

Commit

Permalink
Storing Data to Provider
Browse files Browse the repository at this point in the history
  • Loading branch information
YamamotoDesu committed Apr 12, 2023
1 parent bb2d623 commit ede0757
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 28 deletions.
12 changes: 12 additions & 0 deletions lib/core/common/loader.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:flutter/material.dart';

class Loader extends StatelessWidget {
const Loader({super.key});

@override
Widget build(BuildContext context) {
return const Center(
child: CircularProgressIndicator(),
);
}
}
2 changes: 1 addition & 1 deletion lib/core/common/sign_in_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class SignInButton extends ConsumerWidget {
const SignInButton({super.key});

void signInWithGoogle(BuildContext context, WidgetRef ref) {
ref.read(authControllerProvider).signInWithGoogle(context);
ref.read(authControllerProvider.notifier).signInWithGoogle(context);
}

@override
Expand Down
25 changes: 19 additions & 6 deletions lib/features/auth/controller/auth_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,31 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:reddit_tutorial/core/utils.dart';

import '../../../models/user_model.dart';
import '../repository/auth_repository.dart';

final authControllerProvider = Provider(
(ref) => AuthController(authRepository: ref.read(authRepositoryProvider)));
final userProvider = StateProvider<UserModel?>((ref) => null);

class AuthController {
final authControllerProvider = StateNotifierProvider<AuthController, bool>(
(ref) => AuthController(
authRepository: ref.watch(authRepositoryProvider), ref: ref));

class AuthController extends StateNotifier<bool> {
final AuthRepository _authRepository;
AuthController({required AuthRepository authRepository})
: _authRepository = authRepository;
final Ref _ref;
AuthController({required AuthRepository authRepository, required Ref ref})
: _authRepository = authRepository,
_ref = ref,
super(false); //loading

void signInWithGoogle(BuildContext context) async {
state = true;
final user = await _authRepository.signInWithGoogle();
user.fold((l) => showSnackBar(context, l.message), (r) => null);
state = false;
user.fold(
(l) => showSnackBar(context, l.message),
(userModel) =>
_ref.read(userProvider.notifier).update((state) => userModel),
);
}
}
50 changes: 29 additions & 21 deletions lib/features/auth/screens/login_screen.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:reddit_tutorial/core/common/sign_in_button.dart';
import 'package:reddit_tutorial/features/auth/controller/auth_controller.dart';

import '../../../core/common/loader.dart';
import '../../../core/constants/constants.dart';

class LoginScreen extends StatelessWidget {
class LoginScreen extends ConsumerWidget {
const LoginScreen({super.key});

@override
Widget build(BuildContext context) {
Widget build(BuildContext context, WidgetRef ref) {
final isLoading = ref.watch(authControllerProvider);
return Scaffold(
appBar: AppBar(
title: Image.asset(
Expand All @@ -16,26 +20,30 @@ class LoginScreen extends StatelessWidget {
),
actions: [TextButton(onPressed: () {}, child: const Text('Skip'))],
),
body: Column(
children: [
const SizedBox(height: 30),
const Text(
'Dive into anything',
style: TextStyle(
fontSize: 24, fontWeight: FontWeight.bold, letterSpacing: 0.5),
),
const SizedBox(height: 30),
Padding(
padding: const EdgeInsets.all(8.0),
child: Image.asset(
Constants.logoEmotePath,
height: 250,
body: isLoading
? const Loader()
: Column(
children: [
const SizedBox(height: 30),
const Text(
'Dive into anything',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
letterSpacing: 0.5),
),
const SizedBox(height: 30),
Padding(
padding: const EdgeInsets.all(8.0),
child: Image.asset(
Constants.logoEmotePath,
height: 250,
),
),
const SizedBox(height: 30),
const SignInButton(),
],
),
),
const SizedBox(height: 30),
const SignInButton(),
],
),
);
}
}

0 comments on commit ede0757

Please sign in to comment.