|
1 | 1 | import 'package:flutter/material.dart';
|
2 | 2 | import 'package:flutter_svg/flutter_svg.dart';
|
| 3 | +import 'package:form_builder_validators/form_builder_validators.dart'; |
3 | 4 | import 'package:go_router/go_router.dart';
|
4 | 5 | import 'package:graduation_project/core/res/assets_manager.dart';
|
5 | 6 | import 'package:graduation_project/core/res/strings_manager.dart';
|
| 7 | +import 'package:graduation_project/core/res/ui_constants.dart'; |
6 | 8 | import 'package:graduation_project/core/res/values_manager.dart';
|
| 9 | +import 'package:graduation_project/core/utils/logger.dart'; |
7 | 10 | import 'package:graduation_project/core/widgets/custom_elevated_button.dart';
|
8 | 11 | import 'package:graduation_project/core/widgets/custom_form_field.dart';
|
9 | 12 | import 'package:graduation_project/features/auth/presentation/widgets/auth_action_text_widget.dart';
|
10 | 13 | import 'package:graduation_project/features/auth/presentation/widgets/auth_logo_widget.dart';
|
11 | 14 | import 'package:graduation_project/features/auth/presentation/widgets/auth_text_widget.dart';
|
12 | 15 | import 'package:graduation_project/features/auth/presentation/widgets/password_field_widget.dart';
|
13 | 16 |
|
14 |
| -class RegisterViewBody extends StatelessWidget { |
| 17 | +class RegisterViewBody extends StatefulWidget { |
15 | 18 | const RegisterViewBody({super.key});
|
16 | 19 |
|
| 20 | + @override |
| 21 | + State<RegisterViewBody> createState() => _RegisterViewBodyState(); |
| 22 | +} |
| 23 | + |
| 24 | +class _RegisterViewBodyState extends State<RegisterViewBody> { |
| 25 | + late final GlobalKey<FormState> _formKey; |
| 26 | + late final TextEditingController _usernameController; |
| 27 | + late final TextEditingController _emailController; |
| 28 | + late final TextEditingController _passwordController; |
| 29 | + |
| 30 | + @override |
| 31 | + void initState() { |
| 32 | + super.initState(); |
| 33 | + _formKey = GlobalKey<FormState>(); |
| 34 | + _usernameController = TextEditingController(); |
| 35 | + _emailController = TextEditingController(); |
| 36 | + _passwordController = TextEditingController(); |
| 37 | + } |
| 38 | + |
17 | 39 | @override
|
18 | 40 | Widget build(BuildContext context) {
|
19 |
| - return Column( |
20 |
| - crossAxisAlignment: CrossAxisAlignment.start, |
21 |
| - children: [ |
22 |
| - const AuthHeaderWidget(), |
23 |
| - const AuthTextWidget( |
24 |
| - title: AppStrings.registerTitle, |
25 |
| - subtitle: AppStrings.registerSubtitle, |
26 |
| - ), |
27 |
| - const SizedBox(height: AppSize.s30), |
28 |
| - CustomTextFormField( |
29 |
| - prefixIcon: SvgPicture.asset(AssetsManager.iconsUser), |
30 |
| - hintText: AppStrings.username, |
31 |
| - ), |
32 |
| - const SizedBox(height: AppSize.s20), |
33 |
| - CustomTextFormField( |
34 |
| - prefixIcon: SvgPicture.asset(AssetsManager.iconsMail), |
35 |
| - hintText: AppStrings.email, |
36 |
| - ), |
37 |
| - const SizedBox(height: AppSize.s20), |
38 |
| - const PasswordFieldWidget(), |
39 |
| - const SizedBox(height: AppSize.s20), |
40 |
| - const PasswordFieldWidget(isConfirmPassword: true), |
41 |
| - const SizedBox(height: AppSize.s40), |
42 |
| - CustomElevatedButton(text: AppStrings.signUp, onPressed: () {}), |
43 |
| - const SizedBox(height: AppSize.s18), |
44 |
| - AuthActionTextWidget( |
45 |
| - textBeforeAction: AppStrings.alreadyHaveAnAccount, |
46 |
| - actionText: AppStrings.login, |
47 |
| - onActionPressed: () { |
48 |
| - context.pop(); |
49 |
| - }, |
50 |
| - ), |
51 |
| - ], |
| 41 | + return Form( |
| 42 | + key: _formKey, |
| 43 | + child: Column( |
| 44 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 45 | + children: [ |
| 46 | + const AuthHeaderWidget(), |
| 47 | + const AuthTextWidget( |
| 48 | + title: AppStrings.registerTitle, |
| 49 | + subtitle: AppStrings.registerSubtitle, |
| 50 | + ), |
| 51 | + const SizedBox(height: AppSize.s30), |
| 52 | + CustomTextFormField( |
| 53 | + controller: _usernameController, |
| 54 | + prefixIcon: SvgPicture.asset(AssetsManager.iconsUser), |
| 55 | + validator: FormBuilderValidators.compose([ |
| 56 | + FormBuilderValidators.required(), |
| 57 | + FormBuilderValidators.username(), |
| 58 | + ]), |
| 59 | + hintText: AppStrings.username, |
| 60 | + ), |
| 61 | + const SizedBox(height: AppSize.s20), |
| 62 | + CustomTextFormField( |
| 63 | + controller: _emailController, |
| 64 | + prefixIcon: SvgPicture.asset(AssetsManager.iconsMail), |
| 65 | + validator: FormBuilderValidators.compose([ |
| 66 | + FormBuilderValidators.required(), |
| 67 | + FormBuilderValidators.email(), |
| 68 | + ]), |
| 69 | + hintText: AppStrings.email, |
| 70 | + ), |
| 71 | + const SizedBox(height: AppSize.s20), |
| 72 | + PasswordFieldWidget( |
| 73 | + controller: _passwordController, |
| 74 | + validator: FormBuilderValidators.match( |
| 75 | + UiConstants.passwordRegex, |
| 76 | + errorText: AppStrings.invalidPassword, |
| 77 | + ), |
| 78 | + ), |
| 79 | + const SizedBox(height: AppSize.s20), |
| 80 | + PasswordFieldWidget( |
| 81 | + isConfirmPassword: true, |
| 82 | + validator: FormBuilderValidators.compose([ |
| 83 | + FormBuilderValidators.required(), |
| 84 | + FormBuilderValidators.equal( |
| 85 | + _passwordController.text, |
| 86 | + errorText: AppStrings.invalidMatchePassword, |
| 87 | + ), |
| 88 | + ]), |
| 89 | + ), |
| 90 | + const SizedBox(height: AppSize.s40), |
| 91 | + CustomElevatedButton(text: AppStrings.signUp, onPressed: _submit), |
| 92 | + const SizedBox(height: AppSize.s18), |
| 93 | + AuthActionTextWidget( |
| 94 | + textBeforeAction: AppStrings.alreadyHaveAnAccount, |
| 95 | + actionText: AppStrings.login, |
| 96 | + onActionPressed: () { |
| 97 | + context.pop(); |
| 98 | + }, |
| 99 | + ), |
| 100 | + ], |
| 101 | + ), |
52 | 102 | );
|
53 | 103 | }
|
| 104 | + |
| 105 | + void _submit() { |
| 106 | + if (_formKey.currentState!.validate()) { |
| 107 | + Logger.success("Registration form is valid"); |
| 108 | + } else { |
| 109 | + Logger.error("Registration form is invalid"); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @override |
| 114 | + void dispose() { |
| 115 | + _usernameController.dispose(); |
| 116 | + _emailController.dispose(); |
| 117 | + _passwordController.dispose(); |
| 118 | + super.dispose(); |
| 119 | + } |
54 | 120 | }
|
0 commit comments