feat(auth): add reactive repo + extra data + router examples + tests
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
// Author: Hugo Pointcheval
|
||||
// Email: git@pcl.ovh
|
||||
// -----
|
||||
// File: bootstrap.dart
|
||||
// Created Date: 19/08/2022 15:05:17
|
||||
// Last Modified: 19/08/2022 15:21:47
|
||||
// -----
|
||||
// Copyright (c) 2022
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:example_router/core/utils/app_bloc_observer.dart';
|
||||
import 'package:example_router/firebase_options.dart';
|
||||
import 'package:firebase_core/firebase_core.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
Future<void> bootstrap(FutureOr<Widget> Function() builder) async {
|
||||
await runZonedGuarded(
|
||||
() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
FlutterError.onError = (details) {
|
||||
debugPrint(details.toString());
|
||||
};
|
||||
|
||||
await Firebase.initializeApp(
|
||||
options: DefaultFirebaseOptions.currentPlatform,
|
||||
);
|
||||
|
||||
GoRouter.setUrlPathStrategy(UrlPathStrategy.path);
|
||||
|
||||
Bloc.observer = AppBlocObserver();
|
||||
|
||||
runApp(await builder());
|
||||
},
|
||||
(error, stackTrace) => debugPrint(error.toString()),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// Author: Hugo Pointcheval
|
||||
// Email: git@pcl.ovh
|
||||
// -----
|
||||
// File: form_field.dart
|
||||
// Created Date: 19/08/2022 11:52:33
|
||||
// Last Modified: 19/08/2022 16:35:39
|
||||
// -----
|
||||
// Copyright (c) 2022
|
||||
|
||||
abstract class AppFormField {
|
||||
static const confirmedPassword = 'confirmedPassword';
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
// Author: Hugo Pointcheval
|
||||
// Email: git@pcl.ovh
|
||||
// -----
|
||||
// File: router.dart
|
||||
// Created Date: 19/08/2022 11:52:22
|
||||
// Last Modified: 19/08/2022 16:39:07
|
||||
// -----
|
||||
// Copyright (c) 2022
|
||||
|
||||
import 'package:example_router/presentation/features/home/home_page.dart';
|
||||
import 'package:example_router/presentation/features/sign_in/sign_in_page.dart';
|
||||
import 'package:example_router/presentation/features/sign_up/sign_up_page.dart';
|
||||
import 'package:example_router/presentation/features/sub/sub_page.dart';
|
||||
import 'package:example_router/presentation/features/welcome/welcome_page.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
class AppRouter {
|
||||
/// Default transition for all pages
|
||||
static Page<void> defaultTransition(
|
||||
BuildContext context,
|
||||
GoRouterState state,
|
||||
Widget child,
|
||||
) =>
|
||||
CupertinoPage<void>(
|
||||
key: state.pageKey,
|
||||
child: child,
|
||||
);
|
||||
|
||||
static final publicRoutes = ['/', '/sign_in', '/sign_up'];
|
||||
|
||||
static final List<GoRoute> routes = [
|
||||
GoRoute(
|
||||
path: '/',
|
||||
name: WelcomePage.pageName,
|
||||
pageBuilder: (context, state) => defaultTransition(
|
||||
context,
|
||||
state,
|
||||
const WelcomePage(),
|
||||
),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/sign_in',
|
||||
name: SignInPage.pageName,
|
||||
pageBuilder: (context, state) => defaultTransition(
|
||||
context,
|
||||
state,
|
||||
const SignInPage(),
|
||||
),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/sign_up',
|
||||
name: SignUpPage.pageName,
|
||||
pageBuilder: (context, state) => defaultTransition(
|
||||
context,
|
||||
state,
|
||||
const SignUpPage(),
|
||||
),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/home',
|
||||
name: HomePage.pageName,
|
||||
pageBuilder: (context, state) => defaultTransition(
|
||||
context,
|
||||
state,
|
||||
const HomePage(),
|
||||
),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/home/sub',
|
||||
name: SubPage.pageName,
|
||||
pageBuilder: (context, state) => defaultTransition(
|
||||
context,
|
||||
state,
|
||||
const SubPage(),
|
||||
),
|
||||
),
|
||||
];
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
// Author: Hugo Pointcheval
|
||||
// Email: git@pcl.ovh
|
||||
// -----
|
||||
// File: app_bloc_observer.dart
|
||||
// Created Date: 19/08/2022 12:02:23
|
||||
// Last Modified: 19/08/2022 12:02:45
|
||||
// -----
|
||||
// Copyright (c) 2022
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
class AppBlocObserver extends BlocObserver {
|
||||
@override
|
||||
void onEvent(Bloc bloc, Object? event) {
|
||||
super.onEvent(bloc, event);
|
||||
debugPrint(event.toString());
|
||||
}
|
||||
|
||||
@override
|
||||
void onError(BlocBase bloc, Object error, StackTrace stackTrace) {
|
||||
debugPrint(error.toString());
|
||||
super.onError(bloc, error, stackTrace);
|
||||
}
|
||||
|
||||
@override
|
||||
void onChange(BlocBase bloc, Change change) {
|
||||
super.onChange(bloc, change);
|
||||
debugPrint('curr:\t${change.currentState}\nnext:\t${change.nextState}');
|
||||
}
|
||||
|
||||
@override
|
||||
void onTransition(Bloc bloc, Transition transition) {
|
||||
super.onTransition(bloc, transition);
|
||||
debugPrint(transition.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// Author: Hugo Pointcheval
|
||||
// Email: git@pcl.ovh
|
||||
// -----
|
||||
// File: forms.dart
|
||||
// Created Date: 19/08/2022 12:00:31
|
||||
// Last Modified: 19/08/2022 16:35:52
|
||||
// -----
|
||||
// Copyright (c) 2022
|
||||
|
||||
import 'package:example_router/core/constants/form_field.dart';
|
||||
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
|
||||
|
||||
class Forms {
|
||||
static FormData getNormalData() => const FormData([
|
||||
FormInput(
|
||||
AppFormField.confirmedPassword,
|
||||
ConfirmedPassword.pure(),
|
||||
metadata: FormInputMetadata<void>(export: false),
|
||||
),
|
||||
]);
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
// File generated by FlutterFire CLI.
|
||||
// ignore_for_file: lines_longer_than_80_chars, avoid_classes_with_only_static_members
|
||||
import 'package:firebase_core/firebase_core.dart' show FirebaseOptions;
|
||||
import 'package:flutter/foundation.dart'
|
||||
show defaultTargetPlatform, kIsWeb, TargetPlatform;
|
||||
|
||||
/// Default [FirebaseOptions] for use with your Firebase apps.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// import 'firebase_options.dart';
|
||||
/// // ...
|
||||
/// await Firebase.initializeApp(
|
||||
/// options: DefaultFirebaseOptions.currentPlatform,
|
||||
/// );
|
||||
/// ```
|
||||
class DefaultFirebaseOptions {
|
||||
static FirebaseOptions get currentPlatform {
|
||||
if (kIsWeb) {
|
||||
throw UnsupportedError(
|
||||
'DefaultFirebaseOptions have not been configured for web - '
|
||||
'you can reconfigure this by running the FlutterFire CLI again.',
|
||||
);
|
||||
}
|
||||
switch (defaultTargetPlatform) {
|
||||
case TargetPlatform.android:
|
||||
return android;
|
||||
case TargetPlatform.iOS:
|
||||
throw UnsupportedError(
|
||||
'DefaultFirebaseOptions have not been configured for ios - '
|
||||
'you can reconfigure this by running the FlutterFire CLI again.',
|
||||
);
|
||||
case TargetPlatform.macOS:
|
||||
throw UnsupportedError(
|
||||
'DefaultFirebaseOptions have not been configured for macos - '
|
||||
'you can reconfigure this by running the FlutterFire CLI again.',
|
||||
);
|
||||
case TargetPlatform.windows:
|
||||
throw UnsupportedError(
|
||||
'DefaultFirebaseOptions have not been configured for windows - '
|
||||
'you can reconfigure this by running the FlutterFire CLI again.',
|
||||
);
|
||||
case TargetPlatform.linux:
|
||||
throw UnsupportedError(
|
||||
'DefaultFirebaseOptions have not been configured for linux - '
|
||||
'you can reconfigure this by running the FlutterFire CLI again.',
|
||||
);
|
||||
default:
|
||||
throw UnsupportedError(
|
||||
'DefaultFirebaseOptions are not supported for this platform.',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
static const FirebaseOptions android = FirebaseOptions(
|
||||
apiKey: 'AIzaSyAYS14uXupkS158Q5QAFP1864UrUN_yDSk',
|
||||
appId: '1:136771801992:android:ac3cfeb99fb0763e97203d',
|
||||
messagingSenderId: '136771801992',
|
||||
projectId: 'tchat-beta',
|
||||
databaseURL: 'https://tchat-beta.firebaseio.com',
|
||||
storageBucket: 'tchat-beta.appspot.com',
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import 'package:example_router/bootstrap.dart';
|
||||
import 'package:example_router/presentation/features/app/app.dart';
|
||||
|
||||
void main() {
|
||||
bootstrap(App.new);
|
||||
}
|
||||
+115
@@ -0,0 +1,115 @@
|
||||
// Author: Hugo Pointcheval
|
||||
// Email: git@pcl.ovh
|
||||
// -----
|
||||
// File: app.dart
|
||||
// Created Date: 19/08/2022 12:05:38
|
||||
// Last Modified: Fri Aug 26 2022
|
||||
// -----
|
||||
// Copyright (c) 2022
|
||||
|
||||
import 'package:example_router/core/routes/router.dart';
|
||||
import 'package:example_router/core/utils/forms.dart';
|
||||
import 'package:example_router/presentation/features/home/home_page.dart';
|
||||
import 'package:example_router/presentation/features/welcome/welcome_page.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
|
||||
|
||||
class App extends StatelessWidget {
|
||||
final AuthenticationRepository authenticationRepository =
|
||||
AuthenticationRepositoryFirebase();
|
||||
|
||||
App({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
AuthenticationState? previous;
|
||||
|
||||
final AuthenticationCubit authenticationCubit = AuthenticationCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
onAuthSuccess: (user) async {
|
||||
debugPrint(user.toString());
|
||||
return {};
|
||||
},
|
||||
);
|
||||
|
||||
final GoRouter router = GoRouter(
|
||||
initialLocation: '/',
|
||||
routes: AppRouter.routes,
|
||||
debugLogDiagnostics: true,
|
||||
errorBuilder: (_, __) => const ColoredBox(
|
||||
color: Colors.red,
|
||||
),
|
||||
refreshListenable: GoRouterRefreshStream(authenticationCubit.stream),
|
||||
redirect: (state) {
|
||||
final authState = authenticationCubit.state;
|
||||
|
||||
if (authState != previous) {
|
||||
previous = authState;
|
||||
// Check if current user is logged in
|
||||
final loggedIn =
|
||||
authState.status == AuthenticationStatus.authenticated;
|
||||
|
||||
// Checking if current path is onboarding or not
|
||||
final isOnboarding = AppRouter.publicRoutes.contains(state.subloc);
|
||||
|
||||
if (!loggedIn) {
|
||||
debugPrint('Not logged');
|
||||
if (isOnboarding) {
|
||||
return null;
|
||||
} else {
|
||||
return state.namedLocation(WelcomePage.pageName);
|
||||
}
|
||||
} else {
|
||||
final email = authState.user?.email;
|
||||
debugPrint('Logged as: $email');
|
||||
if (isOnboarding) {
|
||||
return state.namedLocation(HomePage.pageName);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
},
|
||||
);
|
||||
|
||||
return MultiRepositoryProvider(
|
||||
providers: [
|
||||
RepositoryProvider<AuthenticationRepository>.value(
|
||||
value: authenticationRepository,
|
||||
),
|
||||
],
|
||||
child: MultiBlocProvider(
|
||||
providers: [
|
||||
BlocProvider<AuthenticationCubit>.value(
|
||||
value: authenticationCubit..init(),
|
||||
),
|
||||
BlocProvider<SignUpCubit>(
|
||||
create: (_) => SignUpCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
formData: Forms.getNormalData(),
|
||||
onSignUpSuccess: (state, uid) async {
|
||||
debugPrint(state.toString());
|
||||
debugPrint(uid);
|
||||
},
|
||||
),
|
||||
),
|
||||
BlocProvider<SignInCubit>(
|
||||
create: (_) => SignInCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
),
|
||||
),
|
||||
],
|
||||
child: MaterialApp.router(
|
||||
title: 'Demo Authentication',
|
||||
debugShowCheckedModeBanner: false,
|
||||
routerDelegate: router.routerDelegate,
|
||||
routeInformationParser: router.routeInformationParser,
|
||||
routeInformationProvider: router.routeInformationProvider,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
// Author: Hugo Pointcheval
|
||||
// Email: git@pcl.ovh
|
||||
// -----
|
||||
// File: home_page.dart
|
||||
// Created Date: 19/08/2022 14:38:24
|
||||
// Last Modified: 19/08/2022 16:12:22
|
||||
// -----
|
||||
// Copyright (c) 2022
|
||||
|
||||
import 'package:example_router/presentation/features/sub/sub_page.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
|
||||
|
||||
class HomePage extends StatelessWidget {
|
||||
const HomePage({Key? key}) : super(key: key);
|
||||
|
||||
static String pageName = 'Home';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Home'),
|
||||
actions: [
|
||||
IconButton(
|
||||
onPressed: () => context.read<AuthenticationCubit>().logOut(),
|
||||
icon: const Icon(Icons.logout_rounded))
|
||||
],
|
||||
),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: [
|
||||
BlocBuilder<AuthenticationCubit, AuthenticationState>(
|
||||
builder: (context, state) {
|
||||
final email = state.user?.email;
|
||||
return Text('Logged as $email');
|
||||
},
|
||||
),
|
||||
const SizedBox(
|
||||
height: 8,
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () => context.pushNamed(SubPage.pageName),
|
||||
child: const Text('Go to sub page')),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
// Author: Hugo Pointcheval
|
||||
// Email: git@pcl.ovh
|
||||
// -----
|
||||
// File: sign_in_page.dart
|
||||
// Created Date: 19/08/2022 12:41:42
|
||||
// Last Modified: 19/08/2022 15:26:36
|
||||
// -----
|
||||
// Copyright (c) 2022
|
||||
|
||||
import 'package:example_router/presentation/features/sign_in/widgets/sign_in_form.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class SignInPage extends StatelessWidget {
|
||||
const SignInPage({Key? key}) : super(key: key);
|
||||
|
||||
static String pageName = 'SignIn';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Sign In')),
|
||||
body: const Padding(
|
||||
padding: EdgeInsets.all(8),
|
||||
child: SingleChildScrollView(
|
||||
child: SignInForm(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
// Author: Hugo Pointcheval
|
||||
// Email: git@pcl.ovh
|
||||
// -----
|
||||
// File: sign_in_form.dart
|
||||
// Created Date: 19/08/2022 15:24:37
|
||||
// Last Modified: 19/08/2022 16:35:01
|
||||
// -----
|
||||
// Copyright (c) 2022
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
|
||||
|
||||
class _EmailInput extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<SignInCubit, SignInState>(
|
||||
buildWhen: (previous, current) => previous.email != current.email,
|
||||
builder: (context, state) {
|
||||
return TextField(
|
||||
onChanged: (email) => context.read<SignInCubit>().emailChanged(email),
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Email',
|
||||
helperText: '',
|
||||
errorText: state.email.invalid ? 'Invalid email' : null,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _PasswordInput extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<SignInCubit, SignInState>(
|
||||
buildWhen: (previous, current) => previous.password != current.password,
|
||||
builder: (context, state) {
|
||||
return TextField(
|
||||
onChanged: (password) {
|
||||
context.read<SignInCubit>().passwordChanged(password);
|
||||
},
|
||||
obscureText: true,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Password',
|
||||
helperText: '',
|
||||
errorText: state.password.invalid ? 'Invalid password' : null,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _SignInButton extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<SignInCubit, SignInState>(
|
||||
builder: (context, state) {
|
||||
return state.status.isSubmissionInProgress
|
||||
? const CircularProgressIndicator()
|
||||
: ElevatedButton(
|
||||
onPressed: state.status.isValidated
|
||||
? () =>
|
||||
context.read<SignInCubit>().signInWithEmailAndPassword()
|
||||
: null,
|
||||
child: const Text('Sign in'),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class SignInForm extends StatelessWidget {
|
||||
const SignInForm({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocListener<SignInCubit, SignInState>(
|
||||
listener: (context, state) {
|
||||
if (state.status.isSubmissionSuccess) {
|
||||
Navigator.of(context).pop();
|
||||
} else if (state.status.isSubmissionFailure) {
|
||||
ScaffoldMessenger.of(context)
|
||||
..hideCurrentSnackBar()
|
||||
..showSnackBar(
|
||||
SnackBar(content: Text(state.errorMessage ?? 'Sign In Failure')),
|
||||
);
|
||||
}
|
||||
},
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: [
|
||||
_EmailInput(),
|
||||
const SizedBox(height: 8),
|
||||
_PasswordInput(),
|
||||
const SizedBox(height: 16),
|
||||
_SignInButton(),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
// Author: Hugo Pointcheval
|
||||
// Email: git@pcl.ovh
|
||||
// -----
|
||||
// File: sign_up_page.dart
|
||||
// Created Date: 19/08/2022 12:41:27
|
||||
// Last Modified: 19/08/2022 14:58:51
|
||||
// -----
|
||||
// Copyright (c) 2022
|
||||
|
||||
import 'package:example_router/presentation/features/sign_up/widgets/sign_up_form.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class SignUpPage extends StatelessWidget {
|
||||
const SignUpPage({Key? key}) : super(key: key);
|
||||
|
||||
static String pageName = 'SignUp';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Sign Up')),
|
||||
body: const Padding(
|
||||
padding: EdgeInsets.all(8),
|
||||
child: SingleChildScrollView(
|
||||
child: SignUpForm(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
+149
@@ -0,0 +1,149 @@
|
||||
// Author: Hugo Pointcheval
|
||||
// Email: git@pcl.ovh
|
||||
// -----
|
||||
// File: sign_up_form.dart
|
||||
// Created Date: 19/08/2022 14:41:08
|
||||
// Last Modified: Fri Aug 26 2022
|
||||
// -----
|
||||
// Copyright (c) 2022
|
||||
|
||||
import 'package:example_router/core/constants/form_field.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
|
||||
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
|
||||
|
||||
class _EmailInput extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<SignUpCubit, SignUpState>(
|
||||
buildWhen: (previous, current) => previous.email != current.email,
|
||||
builder: (context, state) {
|
||||
return TextField(
|
||||
onChanged: (email) => context.read<SignUpCubit>().emailChanged(email),
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Email',
|
||||
helperText: '',
|
||||
errorText: state.email.invalid ? 'Invalid email' : null,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _PasswordInput extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<SignUpCubit, SignUpState>(
|
||||
buildWhen: (previous, current) => previous.password != current.password,
|
||||
builder: (context, state) {
|
||||
return TextField(
|
||||
onChanged: (password) {
|
||||
context.read<SignUpCubit>().passwordChanged(password);
|
||||
context.read<SignUpCubit>().dataChanged(
|
||||
AppFormField.confirmedPassword,
|
||||
ConfirmedPassword.dirty(
|
||||
password: password,
|
||||
value: context
|
||||
.read<SignUpCubit>()
|
||||
.state
|
||||
.data
|
||||
.valueOf<String>(
|
||||
AppFormField.confirmedPassword),
|
||||
),
|
||||
);
|
||||
},
|
||||
obscureText: true,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Password',
|
||||
helperText: '',
|
||||
errorText: state.password.invalid ? 'Invalid password' : null,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ConfirmPasswordInput extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<SignUpCubit, SignUpState>(
|
||||
builder: (context, state) {
|
||||
return TextField(
|
||||
onChanged: (confirmPassword) => context
|
||||
.read<SignUpCubit>()
|
||||
.dataChanged(
|
||||
AppFormField.confirmedPassword,
|
||||
ConfirmedPassword.dirty(
|
||||
password: context.read<SignUpCubit>().state.password.value,
|
||||
value: confirmPassword,
|
||||
),
|
||||
),
|
||||
obscureText: true,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Confirm password',
|
||||
helperText: '',
|
||||
errorText: state.data.isNotValid(AppFormField.confirmedPassword)
|
||||
? 'Passwords do not match'
|
||||
: null,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _SignUpButton extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<SignUpCubit, SignUpState>(
|
||||
builder: (context, state) {
|
||||
return state.status.isSubmissionInProgress
|
||||
? const CircularProgressIndicator()
|
||||
: ElevatedButton(
|
||||
onPressed: state.status.isValidated
|
||||
? () => context.read<SignUpCubit>().signUpFormSubmitted()
|
||||
: null,
|
||||
child: const Text('Sign up'),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class SignUpForm extends StatelessWidget {
|
||||
const SignUpForm({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocListener<SignUpCubit, SignUpState>(
|
||||
listener: (context, state) {
|
||||
if (state.status.isSubmissionSuccess) {
|
||||
Navigator.of(context).pop();
|
||||
} else if (state.status.isSubmissionFailure) {
|
||||
ScaffoldMessenger.of(context)
|
||||
..hideCurrentSnackBar()
|
||||
..showSnackBar(
|
||||
SnackBar(content: Text(state.errorMessage ?? 'Sign Up Failure')),
|
||||
);
|
||||
}
|
||||
},
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: [
|
||||
_EmailInput(),
|
||||
const SizedBox(height: 8),
|
||||
_PasswordInput(),
|
||||
const SizedBox(height: 8),
|
||||
_ConfirmPasswordInput(),
|
||||
const SizedBox(height: 16),
|
||||
_SignUpButton(),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
// Author: Hugo Pointcheval
|
||||
// Email: git@pcl.ovh
|
||||
// -----
|
||||
// File: sub_page.dart
|
||||
// Created Date: 19/08/2022 16:10:05
|
||||
// Last Modified: 19/08/2022 16:10:44
|
||||
// -----
|
||||
// Copyright (c) 2022
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
|
||||
|
||||
class SubPage extends StatelessWidget {
|
||||
const SubPage({Key? key}) : super(key: key);
|
||||
|
||||
static String pageName = 'Sub';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Sub'),
|
||||
actions: [
|
||||
IconButton(
|
||||
onPressed: () => context.read<AuthenticationCubit>().logOut(),
|
||||
icon: const Icon(Icons.logout_rounded))
|
||||
],
|
||||
),
|
||||
body: const Padding(
|
||||
padding: EdgeInsets.all(8),
|
||||
child: SingleChildScrollView(
|
||||
child: Text('Another page'),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
// Author: Hugo Pointcheval
|
||||
// Email: git@pcl.ovh
|
||||
// -----
|
||||
// File: welcome_page.dart
|
||||
// Created Date: 19/08/2022 12:33:21
|
||||
// Last Modified: 19/08/2022 15:56:05
|
||||
// -----
|
||||
// Copyright (c) 2022
|
||||
|
||||
import 'package:example_router/presentation/features/sign_in/sign_in_page.dart';
|
||||
import 'package:example_router/presentation/features/sign_up/sign_up_page.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
|
||||
|
||||
class WelcomePage extends StatelessWidget {
|
||||
const WelcomePage({Key? key}) : super(key: key);
|
||||
|
||||
static String pageName = 'Welcome';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Welcome'),
|
||||
actions: [
|
||||
IconButton(
|
||||
onPressed: () => context.read<AuthenticationCubit>().changeStatus(
|
||||
context.read<AuthenticationCubit>().state.user!),
|
||||
icon: const Icon(Icons.refresh_rounded))
|
||||
],
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: [
|
||||
ElevatedButton(
|
||||
onPressed: () => context.pushNamed(SignUpPage.pageName),
|
||||
child: const Text('Sign Up')),
|
||||
ElevatedButton(
|
||||
onPressed: () => context.pushNamed(SignInPage.pageName),
|
||||
style: ButtonStyle(
|
||||
backgroundColor:
|
||||
MaterialStateProperty.all<Color>(Colors.white),
|
||||
foregroundColor:
|
||||
MaterialStateProperty.all<Color>(Colors.blue)),
|
||||
child: const Text('Sign In'))
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user