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 {
|
|
|
|
const EmailPasswordLoginForm({
|
|
|
|
super.key,
|
|
|
|
this.onForgotPassword,
|
|
|
|
required this.onLogin,
|
|
|
|
this.onRegister,
|
|
|
|
this.options = const LoginOptions(),
|
|
|
|
});
|
|
|
|
|
|
|
|
final LoginOptions options;
|
2022-09-29 17:22:26 +02:00
|
|
|
final void Function(String email)? onForgotPassword;
|
2022-09-27 16:38:12 +02:00
|
|
|
final FutureOr<void> Function(String email, String password)? onRegister;
|
|
|
|
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() {
|
2022-09-29 17:22:26 +02:00
|
|
|
late bool isValid =
|
|
|
|
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(
|
|
|
|
physics: MediaQuery.of(context).viewInsets.bottom == 0
|
|
|
|
? const NeverScrollableScrollPhysics()
|
|
|
|
: null,
|
|
|
|
slivers: [
|
|
|
|
SliverFillRemaining(
|
|
|
|
fillOverscroll: true,
|
|
|
|
hasScrollBody: false,
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
if (options.title != null) ...[
|
|
|
|
const SizedBox(
|
|
|
|
height: 60,
|
|
|
|
),
|
|
|
|
Align(
|
|
|
|
alignment: Alignment.topCenter,
|
|
|
|
child: _wrapWithDefaultStyle(
|
|
|
|
options.title,
|
2023-02-13 14:33:37 +01:00
|
|
|
theme.textTheme.headlineSmall,
|
2022-11-25 11:41:44 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
2023-05-08 18:09:19 +02:00
|
|
|
if (options.spacers.spacerAfterTitle != null) ...[
|
|
|
|
Spacer(flex: options.spacers.spacerAfterTitle!),
|
|
|
|
],
|
2022-11-25 11:41:44 +01:00
|
|
|
if (options.subtitle != null) ...[
|
|
|
|
const SizedBox(
|
|
|
|
height: 10,
|
|
|
|
),
|
|
|
|
Align(
|
|
|
|
alignment: Alignment.topCenter,
|
|
|
|
child: _wrapWithDefaultStyle(
|
|
|
|
options.subtitle,
|
2023-02-13 14:33:37 +01:00
|
|
|
theme.textTheme.titleSmall,
|
2022-11-25 11:41:44 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
2023-05-08 18:09:19 +02:00
|
|
|
if (options.spacers.spacerAfterSubtitle != null) ...[
|
|
|
|
Spacer(flex: options.spacers.spacerAfterSubtitle!),
|
|
|
|
],
|
2022-11-25 11:41:44 +01:00
|
|
|
if (options.image != null) ...[
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(16),
|
|
|
|
child: options.image,
|
|
|
|
),
|
|
|
|
],
|
2023-05-08 18:09:19 +02:00
|
|
|
if (options.spacers.spacerAfterImage != null) ...[
|
|
|
|
Spacer(flex: options.spacers.spacerAfterImage!),
|
|
|
|
],
|
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,
|
|
|
|
child: Align(
|
|
|
|
child: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
options.emailInputContainerBuilder(
|
|
|
|
TextFormField(
|
|
|
|
onChanged: _updateCurrentEmail,
|
|
|
|
validator:
|
|
|
|
widget.options.validations.validateEmail,
|
|
|
|
initialValue: options.initialEmail,
|
|
|
|
keyboardType: TextInputType.emailAddress,
|
|
|
|
textInputAction: TextInputAction.next,
|
2023-02-13 14:33:37 +01:00
|
|
|
style: options.emailTextStyle,
|
2023-03-14 11:16:13 +01:00
|
|
|
decoration: options.emailDecoration,
|
2022-09-29 12:14:30 +02:00
|
|
|
),
|
2022-09-27 16:38:12 +02:00
|
|
|
),
|
2022-11-25 11:41:44 +01:00
|
|
|
options.passwordInputContainerBuilder(
|
|
|
|
TextFormField(
|
|
|
|
obscureText: _obscurePassword,
|
|
|
|
onChanged: _updateCurrentPassword,
|
|
|
|
validator:
|
|
|
|
widget.options.validations.validatePassword,
|
|
|
|
initialValue: options.initialPassword,
|
|
|
|
keyboardType: TextInputType.visiblePassword,
|
|
|
|
textInputAction: TextInputAction.done,
|
2023-02-13 14:33:37 +01:00
|
|
|
style: options.passwordTextStyle,
|
2022-11-25 11:41:44 +01:00
|
|
|
onFieldSubmitted: (_) => _handleLogin(),
|
2023-03-14 11:16:13 +01:00
|
|
|
decoration: options.passwordDecoration.copyWith(
|
2022-11-25 11:41:44 +01:00
|
|
|
suffixIcon: IconButton(
|
|
|
|
onPressed: () {
|
|
|
|
setState(() {
|
|
|
|
_obscurePassword = !_obscurePassword;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
icon: Icon(
|
|
|
|
_obscurePassword
|
|
|
|
? Icons.visibility
|
|
|
|
: Icons.visibility_off,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
if (widget.onForgotPassword != null) ...[
|
|
|
|
Align(
|
|
|
|
alignment: Alignment.topRight,
|
|
|
|
child: options.forgotPasswordButtonBuilder(
|
|
|
|
context,
|
|
|
|
() {
|
|
|
|
widget.onForgotPassword?.call(_currentEmail);
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
() {},
|
|
|
|
options,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
2023-05-08 18:09:19 +02:00
|
|
|
if (options.spacers.spacerAfterForm != null) ...[
|
|
|
|
Spacer(flex: options.spacers.spacerAfterForm!),
|
|
|
|
],
|
2022-11-25 11:41:44 +01:00
|
|
|
const SizedBox(height: 8),
|
|
|
|
AnimatedBuilder(
|
|
|
|
animation: _formValid,
|
|
|
|
builder: (context, _) {
|
|
|
|
return options.loginButtonBuilder(
|
|
|
|
context,
|
|
|
|
_handleLogin,
|
|
|
|
!_formValid.value,
|
|
|
|
() {
|
|
|
|
_formKey.currentState?.validate();
|
|
|
|
},
|
|
|
|
options,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
if (widget.onRegister != null) ...[
|
|
|
|
options.registrationButtonBuilder(
|
|
|
|
context,
|
|
|
|
() {
|
|
|
|
widget.onRegister?.call(
|
|
|
|
_currentEmail,
|
|
|
|
_currentPassword,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
() {},
|
|
|
|
options,
|
|
|
|
),
|
|
|
|
]
|
|
|
|
],
|
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
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget? _wrapWithDefaultStyle(Widget? widget, TextStyle? style) {
|
|
|
|
if (style == null || widget == null) {
|
|
|
|
return widget;
|
|
|
|
} else {
|
|
|
|
return DefaultTextStyle(style: style, child: widget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|