verification_page.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // ignore_for_file: use_build_context_synchronously
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_riverpod/flutter_riverpod.dart';
  4. import 'package:go_router/go_router.dart';
  5. import 'package:tp5/core/core.dart';
  6. import 'package:tp5/onboarding/onboarding.dart';
  7. class VerificationPageParams {
  8. const VerificationPageParams({
  9. required this.email,
  10. required this.password,
  11. required this.username,
  12. });
  13. final String email;
  14. final String password;
  15. final String username;
  16. }
  17. class VerificationPage extends ConsumerStatefulWidget {
  18. const VerificationPage({required this.params, super.key});
  19. final VerificationPageParams params;
  20. @override
  21. ConsumerState<VerificationPage> createState() => _VerificationPageState();
  22. }
  23. class _VerificationPageState extends ConsumerState<VerificationPage> {
  24. final _formKey = GlobalKey<FormState>();
  25. AutovalidateMode? _autovalidateMode;
  26. bool _isSubmitting = false;
  27. final _codeCtrl = TextEditingController();
  28. Future<void> _resendCode() async {
  29. try {
  30. setState(() {
  31. _isSubmitting = true;
  32. });
  33. await ref.read(onboardingRepositoryProvider).signUp(
  34. email: widget.params.email,
  35. password: widget.params.password,
  36. username: widget.params.username,
  37. );
  38. if (mounted) {
  39. context.showAlert('Code resent');
  40. }
  41. } catch (e) {
  42. context.showAlert(e.toString());
  43. }
  44. setState(() {
  45. _isSubmitting = false;
  46. });
  47. }
  48. Future<void> _verify() async {
  49. try {
  50. setState(() {
  51. _isSubmitting = true;
  52. });
  53. await ref.read(onboardingRepositoryProvider).verifyCode(
  54. email: widget.params.email,
  55. code: _codeCtrl.text,
  56. );
  57. if (mounted) {
  58. context.showAlert('Successfully signed up');
  59. context.go('/');
  60. }
  61. } catch (e) {
  62. setState(() {
  63. _isSubmitting = false;
  64. });
  65. context.showAlert(e.toString());
  66. }
  67. }
  68. @override
  69. Widget build(BuildContext context) {
  70. return Scaffold(
  71. appBar: AppBar(
  72. title: const Text('Verify Account'),
  73. ),
  74. body: Padding(
  75. padding: const EdgeInsets.symmetric(
  76. horizontal: 20,
  77. vertical: 30,
  78. ),
  79. child: Form(
  80. key: _formKey,
  81. autovalidateMode: _autovalidateMode,
  82. child: Column(
  83. children: [
  84. Text(
  85. 'Enter the verification code sent '
  86. 'to your email address ${widget.params.email}',
  87. ),
  88. const SizedBox(height: 30),
  89. TextFormField(
  90. controller: _codeCtrl,
  91. readOnly: _isSubmitting,
  92. decoration: const InputDecoration(
  93. labelText: 'Verification code',
  94. border: OutlineInputBorder(),
  95. ),
  96. validator: (value) {
  97. if (value == null || value.isEmpty) {
  98. return 'Missing the verification code';
  99. }
  100. return null;
  101. },
  102. ),
  103. const SizedBox(height: 20),
  104. SizedBox(
  105. width: double.infinity,
  106. child: OutlinedButton(
  107. onPressed: _isSubmitting ? null : _resendCode,
  108. child: const Text('Resend Code'),
  109. ),
  110. ),
  111. const SizedBox(height: 10),
  112. SizedBox(
  113. width: double.infinity,
  114. child: FilledButton(
  115. onPressed: _isSubmitting
  116. ? null
  117. : () {
  118. if (_formKey.currentState!.validate()) {
  119. _verify();
  120. } else {
  121. setState(() {
  122. _autovalidateMode = AutovalidateMode.always;
  123. });
  124. }
  125. },
  126. child: const Text('Submit'),
  127. ),
  128. ),
  129. ],
  130. ),
  131. ),
  132. ),
  133. );
  134. }
  135. }