diff --git a/lib/provider/register_provider.dart b/lib/provider/register_provider.dart index f90689f..5b38fb5 100644 --- a/lib/provider/register_provider.dart +++ b/lib/provider/register_provider.dart @@ -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'; @@ -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(); @@ -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(); diff --git a/lib/screens/login.dart b/lib/screens/login.dart index 2aa1096..25bb4a2 100644 --- a/lib/screens/login.dart +++ b/lib/screens/login.dart @@ -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'; @@ -19,7 +20,8 @@ class LoginScreen extends StatefulWidget { class _LoginScreenState extends State { final TextEditingController _emailController = TextEditingController(); final TextEditingController _passwordController = TextEditingController(); - + final LoadingOverlayController _loadingController = + LoadingOverlayController(); @override void initState() { super.initState(); @@ -29,28 +31,35 @@ class _LoginScreenState extends State { @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(); - 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(); + 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()), + ), ), ); } diff --git a/lib/screens/register.dart b/lib/screens/register.dart index 71619f7..d69d510 100644 --- a/lib/screens/register.dart +++ b/lib/screens/register.dart @@ -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().loadingController, + child: const Register(), + ), + ); + }, ); /// Navigating to the RegisterScreenRoute. @@ -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( diff --git a/lib/widgets/loading_overlay.dart b/lib/widgets/loading_overlay.dart new file mode 100644 index 0000000..6b59d33 --- /dev/null +++ b/lib/widgets/loading_overlay.dart @@ -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 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 { + @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, + ), + ), + ), + ], + ); + } +} diff --git a/lib/widgets/register/register_third_page.dart b/lib/widgets/register/register_third_page.dart index 0264ed2..e42f4f2 100644 --- a/lib/widgets/register/register_third_page.dart +++ b/lib/widgets/register/register_third_page.dart @@ -41,12 +41,13 @@ class _RegisterThirdPageState extends State { child: RightArrowButton( text: 'Register', onPressed: () async { + registerProvider.loadingController.showLoadingOverlay(); await GetIt.I.get().register( registerProvider.emailController.text, registerProvider.passwordController.text, registerProvider.createUser(), ); - + registerProvider.loadingController.hideLoadingOverlay(); router.replaceAll([const HomeScreenRoute()]); }), ),