2022-09-27 16:38:12 +02:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_login/flutter_login.dart';
|
|
|
|
|
|
|
|
class EmailPasswordLoginForm extends StatefulWidget {
|
2024-02-19 16:03:18 +01:00
|
|
|
/// Constructs an [EmailPasswordLoginForm] widget.
|
|
|
|
///
|
|
|
|
/// [onLogin]: Callback function for user login.
|
|
|
|
/// [onForgotPassword]: Callback function for when the user
|
|
|
|
/// forgets their password.
|
|
|
|
/// [onRegister]: Callback function for user registration.
|
|
|
|
/// [options]: The options for configuring the login form.
|
2022-09-27 16:38:12 +02:00
|
|
|
const EmailPasswordLoginForm({
|
2024-02-06 16:05:46 +01:00
|
|
|
required this.onLogin,
|
2022-09-27 16:38:12 +02:00
|
|
|
super.key,
|
|
|
|
this.onForgotPassword,
|
|
|
|
this.onRegister,
|
|
|
|
this.options = const LoginOptions(),
|
|
|
|
});
|
|
|
|
|
|
|
|
final LoginOptions options;
|
2024-02-15 14:40:56 +01:00
|
|
|
final void Function(String email, BuildContext ctx)? onForgotPassword;
|
|
|
|
final FutureOr<void> Function(
|
|
|
|
String email,
|
|
|
|
String password,
|
|
|
|
BuildContext context,
|
|
|
|
)? onRegister;
|
2022-09-27 16:38:12 +02:00
|
|
|
final FutureOr<void> Function(String email, String password) onLogin;
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<EmailPasswordLoginForm> createState() => _EmailPasswordLoginFormState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _EmailPasswordLoginFormState extends State<EmailPasswordLoginForm> {
|
|
|
|
final _formKey = GlobalKey<FormState>();
|
|
|
|
final ValueNotifier<bool> _formValid = ValueNotifier(false);
|
|
|
|
bool _obscurePassword = true;
|
|
|
|
|
|
|
|
String _currentEmail = '';
|
|
|
|
String _currentPassword = '';
|
|
|
|
|
|
|
|
void _updateCurrentEmail(String email) {
|
|
|
|
_currentEmail = email;
|
|
|
|
_validate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _updateCurrentPassword(String password) {
|
|
|
|
_currentPassword = password;
|
|
|
|
_validate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _validate() {
|
2024-02-06 16:05:46 +01:00
|
|
|
late var isValid =
|
2022-09-29 17:22:26 +02:00
|
|
|
widget.options.validations.validateEmail(_currentEmail) == null &&
|
|
|
|
widget.options.validations.validatePassword(_currentPassword) ==
|
|
|
|
null;
|
2022-09-27 16:38:12 +02:00
|
|
|
if (isValid != _formValid.value) {
|
|
|
|
_formValid.value = isValid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _handleLogin() async {
|
|
|
|
if (mounted) {
|
|
|
|
var form = _formKey.currentState!;
|
|
|
|
if (form.validate()) {
|
|
|
|
await widget.onLogin(
|
|
|
|
_currentEmail,
|
|
|
|
_currentPassword,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-15 15:46:08 +01:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_currentEmail = widget.options.initialEmail;
|
|
|
|
_currentPassword = widget.options.initialPassword;
|
|
|
|
_validate();
|
|
|
|
}
|
|
|
|
|
2022-09-27 16:38:12 +02:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
var theme = Theme.of(context);
|
|
|
|
var options = widget.options;
|
2022-11-25 11:41:44 +01:00
|
|
|
return CustomScrollView(
|
2024-02-15 11:17:30 +01:00
|
|
|
physics: const ScrollPhysics(),
|
2022-11-25 11:41:44 +01:00
|
|
|
slivers: [
|
|
|
|
SliverFillRemaining(
|
|
|
|
hasScrollBody: false,
|
2024-02-15 11:17:30 +01:00
|
|
|
fillOverscroll: true,
|
2022-11-25 11:41:44 +01:00
|
|
|
child: Column(
|
|
|
|
children: [
|
2024-02-15 11:17:30 +01:00
|
|
|
Expanded(
|
|
|
|
flex: options.spacers.titleSpacer,
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
if (options.spacers.spacerBeforeTitle != null) ...[
|
|
|
|
Spacer(flex: options.spacers.spacerBeforeTitle!),
|
|
|
|
],
|
|
|
|
if (options.title != null) ...[
|
|
|
|
Align(
|
|
|
|
alignment: Alignment.topCenter,
|
2024-03-06 10:40:53 +01:00
|
|
|
child: wrapWithDefaultStyle(
|
2024-02-15 11:17:30 +01:00
|
|
|
options.title,
|
|
|
|
theme.textTheme.headlineSmall,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
if (options.spacers.spacerAfterTitle != null) ...[
|
|
|
|
Spacer(flex: options.spacers.spacerAfterTitle!),
|
|
|
|
],
|
|
|
|
if (options.subtitle != null) ...[
|
|
|
|
Align(
|
|
|
|
alignment: Alignment.topCenter,
|
2024-03-06 10:40:53 +01:00
|
|
|
child: wrapWithDefaultStyle(
|
2024-02-15 11:17:30 +01:00
|
|
|
options.subtitle,
|
|
|
|
theme.textTheme.titleSmall,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
if (options.spacers.spacerAfterSubtitle != null) ...[
|
|
|
|
Spacer(flex: options.spacers.spacerAfterSubtitle!),
|
|
|
|
],
|
|
|
|
if (options.image != null) ...[
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(16),
|
|
|
|
child: options.image,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
if (options.spacers.spacerAfterImage != null) ...[
|
|
|
|
Spacer(flex: options.spacers.spacerAfterImage!),
|
|
|
|
],
|
|
|
|
],
|
2022-11-25 11:41:44 +01:00
|
|
|
),
|
2024-02-15 11:17:30 +01:00
|
|
|
),
|
2022-11-25 11:41:44 +01:00
|
|
|
Expanded(
|
2023-05-08 18:09:19 +02:00
|
|
|
flex: options.spacers.formFlexValue,
|
2022-11-25 11:41:44 +01:00
|
|
|
child: ConstrainedBox(
|
2023-05-08 18:09:19 +02:00
|
|
|
constraints: BoxConstraints(
|
|
|
|
maxWidth: options.maxFormWidth ?? 300,
|
2022-11-25 11:41:44 +01:00
|
|
|
),
|
|
|
|
child: Form(
|
|
|
|
key: _formKey,
|
2024-02-15 11:17:30 +01:00
|
|
|
child: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
options.emailInputContainerBuilder(
|
|
|
|
TextFormField(
|
2024-03-07 13:46:13 +01:00
|
|
|
textAlign:
|
|
|
|
options.emailTextAlign ?? TextAlign.start,
|
2024-02-15 11:17:30 +01:00
|
|
|
onChanged: _updateCurrentEmail,
|
|
|
|
validator: widget.options.validations.validateEmail,
|
|
|
|
initialValue: options.initialEmail,
|
|
|
|
keyboardType: TextInputType.emailAddress,
|
|
|
|
textInputAction: TextInputAction.next,
|
|
|
|
style: options.emailTextStyle,
|
|
|
|
decoration: options.emailDecoration,
|
2022-09-27 16:38:12 +02:00
|
|
|
),
|
2024-02-15 11:17:30 +01:00
|
|
|
),
|
|
|
|
options.passwordInputContainerBuilder(
|
|
|
|
TextFormField(
|
2024-03-07 13:46:13 +01:00
|
|
|
textAlign:
|
|
|
|
options.passwordTextAlign ?? TextAlign.start,
|
2024-02-15 11:17:30 +01:00
|
|
|
obscureText: _obscurePassword,
|
|
|
|
onChanged: _updateCurrentPassword,
|
|
|
|
validator:
|
|
|
|
widget.options.validations.validatePassword,
|
|
|
|
initialValue: options.initialPassword,
|
|
|
|
keyboardType: TextInputType.visiblePassword,
|
|
|
|
textInputAction: TextInputAction.done,
|
|
|
|
style: options.passwordTextStyle,
|
|
|
|
onFieldSubmitted: (_) async => _handleLogin(),
|
|
|
|
decoration: options.passwordDecoration.copyWith(
|
|
|
|
suffixIcon: options.showObscurePassword
|
|
|
|
? IconButton(
|
|
|
|
onPressed: () {
|
|
|
|
setState(() {
|
|
|
|
_obscurePassword = !_obscurePassword;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
icon: Icon(
|
|
|
|
_obscurePassword
|
|
|
|
? Icons.visibility
|
|
|
|
: Icons.visibility_off,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
: null,
|
2022-11-25 11:41:44 +01:00
|
|
|
),
|
|
|
|
),
|
2024-02-15 11:17:30 +01:00
|
|
|
),
|
|
|
|
if (widget.onForgotPassword != null) ...[
|
|
|
|
Align(
|
|
|
|
alignment: Alignment.topRight,
|
|
|
|
child: options.forgotPasswordButtonBuilder(
|
2024-02-06 16:05:46 +01:00
|
|
|
context,
|
|
|
|
() {
|
2024-02-15 14:40:56 +01:00
|
|
|
widget.onForgotPassword
|
|
|
|
?.call(_currentEmail, context);
|
2022-11-25 11:41:44 +01:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
() {},
|
|
|
|
options,
|
|
|
|
),
|
2024-02-15 11:17:30 +01:00
|
|
|
),
|
2022-11-25 11:41:44 +01:00
|
|
|
],
|
2024-02-15 11:17:30 +01:00
|
|
|
if (options.spacers.spacerAfterForm != null) ...[
|
|
|
|
Spacer(flex: options.spacers.spacerAfterForm!),
|
|
|
|
],
|
|
|
|
AnimatedBuilder(
|
|
|
|
animation: _formValid,
|
|
|
|
builder: (context, _) => options.loginButtonBuilder(
|
|
|
|
context,
|
|
|
|
_handleLogin,
|
|
|
|
!_formValid.value,
|
|
|
|
() {
|
|
|
|
_formKey.currentState?.validate();
|
|
|
|
},
|
|
|
|
options,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
if (widget.onRegister != null) ...[
|
|
|
|
options.registrationButtonBuilder(
|
|
|
|
context,
|
|
|
|
() async {
|
|
|
|
widget.onRegister?.call(
|
|
|
|
_currentEmail,
|
|
|
|
_currentPassword,
|
2024-02-15 14:40:56 +01:00
|
|
|
context,
|
2024-02-15 11:17:30 +01:00
|
|
|
);
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
() {},
|
|
|
|
options,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
if (options.spacers.spacerAfterButton != null) ...[
|
|
|
|
Spacer(flex: options.spacers.spacerAfterButton!),
|
|
|
|
],
|
|
|
|
],
|
2022-09-27 16:38:12 +02:00
|
|
|
),
|
2022-11-25 11:41:44 +01:00
|
|
|
),
|
2022-09-27 16:38:12 +02:00
|
|
|
),
|
|
|
|
),
|
2022-11-25 11:41:44 +01:00
|
|
|
],
|
2022-09-27 16:38:12 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
2024-03-06 10:40:53 +01:00
|
|
|
}
|
2022-09-27 16:38:12 +02:00
|
|
|
|
2024-03-06 10:40:53 +01:00
|
|
|
Widget? wrapWithDefaultStyle(Widget? widget, TextStyle? style) {
|
|
|
|
if (style == null || widget == null) {
|
|
|
|
return widget;
|
|
|
|
} else {
|
|
|
|
return DefaultTextStyle(style: style, child: widget);
|
2022-09-27 16:38:12 +02:00
|
|
|
}
|
|
|
|
}
|