Skip to content

Commit

Permalink
Merge pull request #157 from PDG-NUTRI/155-chargement-durant-login-et…
Browse files Browse the repository at this point in the history
…-register

LoadingOverlay done
  • Loading branch information
chloefont authored Sep 4, 2022
2 parents 0df85af + 13e3989 commit 2461d43
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 30 deletions.
4 changes: 4 additions & 0 deletions lib/provider/register_provider.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:pdg_app/widgets/loading_overlay.dart';

import '../model/user.dart';

Expand All @@ -13,6 +14,8 @@ class RegisterProvider extends ChangeNotifier {
TextEditingController();
final TextEditingController _phoneController = TextEditingController();
final TextEditingController _insuranceController = TextEditingController();
final LoadingOverlayController _loadingController =
LoadingOverlayController();

DateTime _birthDay = DateTime.now();

Expand All @@ -24,6 +27,7 @@ class RegisterProvider extends ChangeNotifier {
_confirmPasswordController;
TextEditingController get phoneController => _phoneController;
TextEditingController get insuranceController => _insuranceController;
LoadingOverlayController get loadingController => _loadingController;
DateTime get birthDay => _birthDay;

final _formPage1 = GlobalKey<FormState>();
Expand Down
51 changes: 30 additions & 21 deletions lib/screens/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:pdg_app/provider/auth_provider.dart';
import 'package:pdg_app/widgets/forms/main_text_field.dart';
import 'package:pdg_app/widgets/loading_overlay.dart';

import '../router/router.gr.dart';
import '../widgets/buttons/right_arrow_button.dart';
Expand All @@ -19,7 +20,8 @@ class LoginScreen extends StatefulWidget {
class _LoginScreenState extends State<LoginScreen> {
final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();

final LoadingOverlayController _loadingController =
LoadingOverlayController();
@override
void initState() {
super.initState();
Expand All @@ -29,28 +31,35 @@ class _LoginScreenState extends State<LoginScreen> {

@override
Widget build(BuildContext context) {
final router = AutoRouter.of(context);
final scaffold = ScaffoldMessenger.of(context);
return Scaffold(
resizeToAvoidBottomInset: false,
body: Login(
emailController: _emailController,
passwordController: _passwordController,
onLoginPress: () async {
final auth = GetIt.I.get<AuthProvider>();
await auth.signIn(_emailController.text, _passwordController.text);
if (auth.isConnected()) {
// ignore: use_build_context_synchronously
AutoRouter.of(context).replaceAll([const HomeScreenRoute()]);
} else {
// ignore: use_build_context_synchronously
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Login failed'),
),
);
}
},
onRegisterPress: () =>
AutoRouter.of(context).navigate(const RegisterScreenRoute()),
body: LoadingOverlay(
controller: _loadingController,
child: Login(
emailController: _emailController,
passwordController: _passwordController,
onLoginPress: () async {
_loadingController.showLoadingOverlay();
final auth = GetIt.I.get<AuthProvider>();
await auth.signIn(_emailController.text, _passwordController.text);
if (auth.isConnected()) {
_loadingController.hideLoadingOverlay();
router.replaceAll([const HomeScreenRoute()]);
} else {
// ignore: use_build_context_synchronously
_loadingController.hideLoadingOverlay();
scaffold.showSnackBar(
const SnackBar(
content: Text('Login failed'),
),
);
}
},
onRegisterPress: () =>
AutoRouter.of(context).navigate(const RegisterScreenRoute()),
),
),
);
}
Expand Down
23 changes: 15 additions & 8 deletions lib/screens/register.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@ import 'package:pdg_app/widgets/register/top_shape.dart';
import 'package:pdg_app/widgets/register/bottom_shape.dart';
import 'package:provider/provider.dart';

import '../widgets/loading_overlay.dart';

class RegisterScreen extends StatelessWidget {
const RegisterScreen({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return const Scaffold(
resizeToAvoidBottomInset: false,
body: Register(),
return ChangeNotifierProvider(
create: (context) => RegisterProvider(),
builder: (context, child) {
return Scaffold(
resizeToAvoidBottomInset: false,
body: LoadingOverlay(
controller: context.read<RegisterProvider>().loadingController,
child: const Register(),
),
);
},
);

/// Navigating to the RegisterScreenRoute.
Expand Down Expand Up @@ -56,11 +66,8 @@ class Register extends StatelessWidget {
),
),
const SizedBox(height: 20),
Expanded(
child: ChangeNotifierProvider(
create: (context) => RegisterProvider(),
child: const AutoRouter(),
),
const Expanded(
child: AutoRouter(),
),
CustomPaint(
size: Size(
Expand Down
62 changes: 62 additions & 0 deletions lib/widgets/loading_overlay.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import 'package:flutter/material.dart';

class LoadingOverlay extends StatefulWidget {
final Widget _child;
final LoadingOverlayController _controller;
LoadingOverlay(
{required Widget child, LoadingOverlayController? controller, Key? key})
: _child = child,
_controller = controller ?? LoadingOverlayController(),
super(
key: key,
);

@override
State<LoadingOverlay> createState() => _LoadingOverlayState();
}

class LoadingOverlayController extends ChangeNotifier {
bool _isLoading = false;

bool get isLoading => _isLoading;

LoadingOverlayController();

void showLoadingOverlay() {
_isLoading = true;
notifyListeners();
}

void hideLoadingOverlay() {
_isLoading = false;
notifyListeners();
}
}

class _LoadingOverlayState extends State<LoadingOverlay> {
@override
void initState() {
widget._controller.addListener(() {
setState(() {});
});
super.initState();
}

@override
Widget build(BuildContext context) {
return Stack(
children: [
widget._child,
if (widget._controller.isLoading)
Container(
color: const Color.fromARGB(166, 0, 0, 0),
child: Center(
child: CircularProgressIndicator(
color: Theme.of(context).colorScheme.primary,
),
),
),
],
);
}
}
3 changes: 2 additions & 1 deletion lib/widgets/register/register_third_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ class _RegisterThirdPageState extends State<RegisterThirdPage> {
child: RightArrowButton(
text: 'Register',
onPressed: () async {
registerProvider.loadingController.showLoadingOverlay();
await GetIt.I.get<AuthProvider>().register(
registerProvider.emailController.text,
registerProvider.passwordController.text,
registerProvider.createUser(),
);

registerProvider.loadingController.hideLoadingOverlay();
router.replaceAll([const HomeScreenRoute()]);
}),
),
Expand Down

0 comments on commit 2461d43

Please sign in to comment.