2022-09-27 16:38:12 +02:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
2022-10-27 15:57:54 +02:00
|
|
|
import 'package:flutter_login/src/service/login_validation.dart';
|
2022-09-29 17:22:26 +02:00
|
|
|
import 'package:flutter_login/src/service/validation.dart';
|
2022-09-27 16:38:12 +02:00
|
|
|
|
|
|
|
class LoginOptions {
|
|
|
|
const LoginOptions({
|
|
|
|
this.image,
|
|
|
|
this.title,
|
|
|
|
this.subtitle,
|
2023-02-13 14:33:37 +01:00
|
|
|
this.emailTextStyle,
|
|
|
|
this.passwordTextStyle,
|
2023-03-14 11:16:13 +01:00
|
|
|
this.emailDecoration = const InputDecoration(),
|
|
|
|
this.passwordDecoration = const InputDecoration(),
|
2022-09-27 16:38:12 +02:00
|
|
|
this.initialEmail = '',
|
|
|
|
this.initialPassword = '',
|
|
|
|
this.translations = const LoginTranslations(),
|
2022-09-29 17:22:26 +02:00
|
|
|
this.validationService,
|
2022-09-27 16:38:12 +02:00
|
|
|
this.loginButtonBuilder = _createLoginButton,
|
|
|
|
this.forgotPasswordButtonBuilder = _createForgotPasswordButton,
|
2022-09-29 17:22:26 +02:00
|
|
|
this.requestForgotPasswordButtonBuilder =
|
|
|
|
_createRequestForgotPasswordButton,
|
2022-09-27 16:38:12 +02:00
|
|
|
this.registrationButtonBuilder = _createRegisterButton,
|
2022-09-29 12:14:30 +02:00
|
|
|
this.emailInputContainerBuilder = _createEmailInputContainer,
|
|
|
|
this.passwordInputContainerBuilder = _createPasswordInputContainer,
|
2022-09-27 16:38:12 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
final ButtonBuilder loginButtonBuilder;
|
|
|
|
final ButtonBuilder registrationButtonBuilder;
|
|
|
|
final ButtonBuilder forgotPasswordButtonBuilder;
|
2022-09-29 17:22:26 +02:00
|
|
|
final ButtonBuilder requestForgotPasswordButtonBuilder;
|
2022-09-29 12:14:30 +02:00
|
|
|
final InputContainerBuilder emailInputContainerBuilder;
|
|
|
|
final InputContainerBuilder passwordInputContainerBuilder;
|
2022-09-27 16:38:12 +02:00
|
|
|
|
|
|
|
final Widget? image;
|
|
|
|
final Widget? title;
|
|
|
|
final Widget? subtitle;
|
2023-03-14 11:16:13 +01:00
|
|
|
final InputDecoration emailDecoration;
|
|
|
|
final InputDecoration passwordDecoration;
|
2022-09-27 16:38:12 +02:00
|
|
|
final String initialEmail;
|
|
|
|
final String initialPassword;
|
2023-02-13 14:33:37 +01:00
|
|
|
final TextStyle? emailTextStyle;
|
|
|
|
final TextStyle? passwordTextStyle;
|
2022-09-27 16:38:12 +02:00
|
|
|
final LoginTranslations translations;
|
2022-09-29 17:22:26 +02:00
|
|
|
final ValidationService? validationService;
|
|
|
|
|
|
|
|
ValidationService get validations =>
|
|
|
|
validationService ?? LoginValidationService(this);
|
2022-09-27 16:38:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class LoginTranslations {
|
|
|
|
const LoginTranslations({
|
|
|
|
this.emailEmpty = 'Email is required',
|
|
|
|
this.passwordEmpty = 'Password is required',
|
|
|
|
this.emailInvalid = 'Enter a valid email address',
|
2022-11-04 09:23:48 +01:00
|
|
|
this.loginButton = 'Login',
|
|
|
|
this.forgotPasswordButton = 'Forgot password?',
|
|
|
|
this.requestForgotPasswordButton = 'Send request',
|
|
|
|
this.registrationButton = 'Create Account',
|
2022-09-27 16:38:12 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
final String emailInvalid;
|
|
|
|
final String emailEmpty;
|
|
|
|
final String passwordEmpty;
|
2022-11-04 09:23:48 +01:00
|
|
|
final String loginButton;
|
|
|
|
final String forgotPasswordButton;
|
|
|
|
final String requestForgotPasswordButton;
|
|
|
|
final String registrationButton;
|
2022-09-27 16:38:12 +02:00
|
|
|
}
|
|
|
|
|
2023-03-14 11:16:13 +01:00
|
|
|
Widget _createEmailInputContainer(Widget child) => Padding(
|
|
|
|
padding: const EdgeInsets.only(bottom: 15),
|
|
|
|
child: child,
|
|
|
|
);
|
2022-09-29 12:14:30 +02:00
|
|
|
|
2023-03-14 11:16:13 +01:00
|
|
|
Widget _createPasswordInputContainer(Widget child) => Padding(
|
|
|
|
padding: const EdgeInsets.only(bottom: 15),
|
|
|
|
child: child,
|
|
|
|
);
|
2022-09-29 12:14:30 +02:00
|
|
|
|
2022-09-27 16:38:12 +02:00
|
|
|
Widget _createLoginButton(
|
|
|
|
BuildContext context,
|
|
|
|
OptionalAsyncCallback onPressed,
|
|
|
|
bool disabled,
|
2022-10-31 14:37:09 +01:00
|
|
|
OptionalAsyncCallback onDisabledPress,
|
2022-11-08 09:56:05 +01:00
|
|
|
LoginOptions options,
|
2022-09-27 16:38:12 +02:00
|
|
|
) {
|
|
|
|
return Opacity(
|
|
|
|
opacity: disabled ? 0.5 : 1.0,
|
|
|
|
child: ElevatedButton(
|
2022-10-31 14:37:09 +01:00
|
|
|
onPressed: !disabled ? onPressed : onDisabledPress,
|
2022-11-08 09:56:05 +01:00
|
|
|
child: Text(options.translations.loginButton),
|
2022-09-27 16:38:12 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _createForgotPasswordButton(
|
|
|
|
BuildContext context,
|
|
|
|
OptionalAsyncCallback onPressed,
|
|
|
|
bool disabled,
|
2022-10-31 14:37:09 +01:00
|
|
|
OptionalAsyncCallback onDisabledPress,
|
2022-11-08 09:56:05 +01:00
|
|
|
LoginOptions options,
|
2022-09-27 16:38:12 +02:00
|
|
|
) {
|
|
|
|
return Opacity(
|
|
|
|
opacity: disabled ? 0.5 : 1.0,
|
2022-10-31 14:37:09 +01:00
|
|
|
child: ElevatedButton(
|
|
|
|
onPressed: !disabled ? onPressed : onDisabledPress,
|
2022-11-08 09:56:05 +01:00
|
|
|
child: Text(options.translations.forgotPasswordButton),
|
2022-09-27 16:38:12 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-29 17:22:26 +02:00
|
|
|
Widget _createRequestForgotPasswordButton(
|
|
|
|
BuildContext context,
|
|
|
|
OptionalAsyncCallback onPressed,
|
|
|
|
bool disabled,
|
2022-10-31 14:37:09 +01:00
|
|
|
OptionalAsyncCallback onDisabledPress,
|
2022-11-08 09:56:05 +01:00
|
|
|
LoginOptions options,
|
2022-09-29 17:22:26 +02:00
|
|
|
) {
|
|
|
|
return Opacity(
|
|
|
|
opacity: disabled ? 0.5 : 1.0,
|
2022-10-31 14:37:09 +01:00
|
|
|
child: ElevatedButton(
|
|
|
|
onPressed: !disabled ? onPressed : onDisabledPress,
|
2022-11-08 09:56:05 +01:00
|
|
|
child: Text(options.translations.requestForgotPasswordButton),
|
2022-09-29 17:22:26 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-27 16:38:12 +02:00
|
|
|
Widget _createRegisterButton(
|
|
|
|
BuildContext context,
|
|
|
|
OptionalAsyncCallback onPressed,
|
|
|
|
bool disabled,
|
2022-10-31 14:37:09 +01:00
|
|
|
OptionalAsyncCallback onDisabledPress,
|
2022-11-08 09:56:05 +01:00
|
|
|
LoginOptions options,
|
2022-09-27 16:38:12 +02:00
|
|
|
) {
|
|
|
|
return Opacity(
|
|
|
|
opacity: disabled ? 0.5 : 1.0,
|
2022-10-31 14:37:09 +01:00
|
|
|
child: ElevatedButton(
|
|
|
|
onPressed: !disabled ? onPressed : onDisabledPress,
|
2022-11-08 09:56:05 +01:00
|
|
|
child: Text(options.translations.registrationButton),
|
2022-09-27 16:38:12 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef ButtonBuilder = Widget Function(
|
|
|
|
BuildContext context,
|
|
|
|
OptionalAsyncCallback onPressed,
|
|
|
|
bool isDisabled,
|
2022-10-31 14:37:09 +01:00
|
|
|
OptionalAsyncCallback onDisabledPress,
|
2022-11-08 09:56:05 +01:00
|
|
|
LoginOptions options,
|
2022-09-27 16:38:12 +02:00
|
|
|
);
|
|
|
|
|
2022-09-29 12:14:30 +02:00
|
|
|
typedef InputContainerBuilder = Widget Function(
|
|
|
|
Widget child,
|
|
|
|
);
|
|
|
|
|
2022-09-27 16:38:12 +02:00
|
|
|
typedef OptionalAsyncCallback = FutureOr<void> Function();
|