/
forgot_password_view.dart
135 lines (118 loc) · 3.96 KB
/
forgot_password_view.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2022, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:firebase_ui_shared/firebase_ui_shared.dart';
import 'package:flutter/material.dart' hide Title;
import 'package:firebase_auth/firebase_auth.dart' as fba;
import 'package:firebase_ui_auth/firebase_ui_auth.dart';
import 'package:firebase_ui_localizations/firebase_ui_localizations.dart';
import '../widgets/internal/title.dart';
/// {@template ui.auth.views.forgot_password_view}
/// A view that could be used to build a custom [ForgotPasswordScreen].
/// {@endtemplate}
class ForgotPasswordView extends StatefulWidget {
/// {@macro ui.auth.auth_controller.auth}
final fba.FirebaseAuth? auth;
/// A configuration object that is used to construct a dynamic link.
final fba.ActionCodeSettings? actionCodeSettings;
/// Returned widget would be placed under the title.
final WidgetBuilder? subtitleBuilder;
/// Returned widget would be placed at the bottom of the view.
final WidgetBuilder? footerBuilder;
/// An email that [EmailInput] should be pre-filled with.
final String? email;
/// {@macro ui.auth.views.forgot_password_view}
const ForgotPasswordView({
super.key,
this.auth,
this.email,
this.actionCodeSettings,
this.subtitleBuilder,
this.footerBuilder,
});
@override
// ignore: library_private_types_in_public_api
_ForgotPasswordViewState createState() => _ForgotPasswordViewState();
}
class _ForgotPasswordViewState extends State<ForgotPasswordView> {
late final emailCtrl = TextEditingController(text: widget.email ?? '');
final formKey = GlobalKey<FormState>();
bool emailSent = false;
fba.FirebaseAuth get auth => widget.auth ?? fba.FirebaseAuth.instance;
bool isLoading = false;
fba.FirebaseAuthException? exception;
Future<void> _submit(String email) async {
if (isLoading) {
return;
}
setState(() {
exception = null;
isLoading = true;
});
try {
await auth.sendPasswordResetEmail(
email: email,
actionCodeSettings: widget.actionCodeSettings,
);
emailSent = true;
} on fba.FirebaseAuthException catch (e) {
exception = e;
} finally {
setState(() => isLoading = false);
}
}
@override
Widget build(BuildContext context) {
final l = FirebaseUILocalizations.labelsOf(context);
const spacer = SizedBox(height: 32);
return Form(
key: formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Title(text: l.forgotPasswordViewTitle),
if (!emailSent) ...[
spacer,
widget.subtitleBuilder?.call(context) ??
Text(l.forgotPasswordHintText),
],
spacer,
if (!emailSent) ...[
EmailInput(
autofocus: false,
controller: emailCtrl,
onSubmitted: _submit,
),
spacer,
] else ...[
Text(l.passwordResetEmailSentText),
spacer,
],
if (exception != null) ...[
const SizedBox(height: 16),
ErrorText(exception: exception!),
const SizedBox(height: 16),
],
if (!emailSent)
LoadingButton(
isLoading: isLoading,
label: l.resetPasswordButtonLabel,
onTap: () {
if (formKey.currentState!.validate()) {
_submit(emailCtrl.text);
}
},
),
const SizedBox(height: 8),
UniversalButton(
variant: ButtonVariant.text,
text: l.goBackButtonLabel,
onPressed: () => Navigator.pop(context),
),
if (widget.footerBuilder != null) widget.footerBuilder!(context),
],
),
);
}
}