flutter_login_widget/lib/src/config/login_options.dart

170 lines
5 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'package:flutter/material.dart';
2023-05-08 18:09:19 +02:00
import 'package:flutter_login/src/config/spacer_options.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';
2023-05-08 18:09:19 +02:00
@immutable
class LoginOptions {
const LoginOptions({
this.image,
this.title,
this.subtitle,
2023-05-08 18:09:19 +02:00
this.maxFormWidth,
2023-02-13 14:33:37 +01:00
this.emailTextStyle,
this.passwordTextStyle,
this.emailDecoration = const InputDecoration(),
this.passwordDecoration = const InputDecoration(),
this.initialEmail = '',
this.initialPassword = '',
2023-05-08 18:09:19 +02:00
this.spacers = const LoginSpacerOptions(),
this.translations = const LoginTranslations(),
2022-09-29 17:22:26 +02:00
this.validationService,
this.loginButtonBuilder = _createLoginButton,
this.forgotPasswordButtonBuilder = _createForgotPasswordButton,
2022-09-29 17:22:26 +02:00
this.requestForgotPasswordButtonBuilder =
_createRequestForgotPasswordButton,
this.registrationButtonBuilder = _createRegisterButton,
this.emailInputContainerBuilder = _createEmailInputContainer,
this.passwordInputContainerBuilder = _createPasswordInputContainer,
this.showObscurePassword = true,
});
final ButtonBuilder loginButtonBuilder;
final ButtonBuilder registrationButtonBuilder;
final ButtonBuilder forgotPasswordButtonBuilder;
2022-09-29 17:22:26 +02:00
final ButtonBuilder requestForgotPasswordButtonBuilder;
final InputContainerBuilder emailInputContainerBuilder;
final InputContainerBuilder passwordInputContainerBuilder;
final Widget? image;
final Widget? title;
final Widget? subtitle;
2023-05-08 18:09:19 +02:00
2024-02-06 16:05:46 +01:00
/// Option to modify the spacing between the title, subtitle, image, form,
/// and button.
2023-05-08 18:09:19 +02:00
final LoginSpacerOptions spacers;
/// Maximum width of the form. Defaults to 400.
final double? maxFormWidth;
final InputDecoration emailDecoration;
final InputDecoration passwordDecoration;
final String initialEmail;
final String initialPassword;
2023-02-13 14:33:37 +01:00
final TextStyle? emailTextStyle;
final TextStyle? passwordTextStyle;
final LoginTranslations translations;
2022-09-29 17:22:26 +02:00
final ValidationService? validationService;
final bool showObscurePassword;
2022-09-29 17:22:26 +02:00
ValidationService get validations =>
validationService ?? LoginValidationService(this);
}
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',
});
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;
}
Widget _createEmailInputContainer(Widget child) => Padding(
padding: const EdgeInsets.only(bottom: 15),
child: child,
);
Widget _createPasswordInputContainer(Widget child) => Padding(
padding: const EdgeInsets.only(bottom: 15),
child: child,
);
Widget _createLoginButton(
BuildContext context,
OptionalAsyncCallback onPressed,
bool disabled,
OptionalAsyncCallback onDisabledPress,
LoginOptions options,
2024-02-06 16:05:46 +01:00
) =>
Opacity(
opacity: disabled ? 0.5 : 1.0,
child: ElevatedButton(
onPressed: !disabled ? onPressed : onDisabledPress,
child: Text(options.translations.loginButton),
),
);
Widget _createForgotPasswordButton(
BuildContext context,
OptionalAsyncCallback onPressed,
bool disabled,
OptionalAsyncCallback onDisabledPress,
LoginOptions options,
2024-02-06 16:05:46 +01:00
) =>
Opacity(
opacity: disabled ? 0.5 : 1.0,
child: ElevatedButton(
onPressed: !disabled ? onPressed : onDisabledPress,
child: Text(options.translations.forgotPasswordButton),
),
);
2022-09-29 17:22:26 +02:00
Widget _createRequestForgotPasswordButton(
BuildContext context,
OptionalAsyncCallback onPressed,
bool disabled,
OptionalAsyncCallback onDisabledPress,
LoginOptions options,
2024-02-06 16:05:46 +01:00
) =>
Opacity(
opacity: disabled ? 0.5 : 1.0,
child: ElevatedButton(
onPressed: !disabled ? onPressed : onDisabledPress,
child: Text(options.translations.requestForgotPasswordButton),
),
);
2022-09-29 17:22:26 +02:00
Widget _createRegisterButton(
BuildContext context,
OptionalAsyncCallback onPressed,
bool disabled,
OptionalAsyncCallback onDisabledPress,
LoginOptions options,
2024-02-06 16:05:46 +01:00
) =>
Opacity(
opacity: disabled ? 0.5 : 1.0,
child: ElevatedButton(
onPressed: !disabled ? onPressed : onDisabledPress,
child: Text(options.translations.registrationButton),
),
);
typedef ButtonBuilder = Widget Function(
BuildContext context,
OptionalAsyncCallback onPressed,
2024-02-06 16:05:46 +01:00
// ignore: avoid_positional_boolean_parameters
bool isDisabled,
OptionalAsyncCallback onDisabledPress,
LoginOptions options,
);
typedef InputContainerBuilder = Widget Function(
Widget child,
);
typedef OptionalAsyncCallback = FutureOr<void> Function();