fix: added context to callbacks

This commit is contained in:
Freek van de Ven 2024-02-15 14:40:56 +01:00 committed by mike doornenbal
commit 77ca14753e
5 changed files with 145 additions and 135 deletions

View file

@ -1,3 +1,10 @@
## 5.1.4
* Added BuildContext to the `onForgotPassword` and `onRegister`.
## 5.1.3
* Added title spacer
## 5.1.2 ## 5.1.2
* Removed bottom padding from password text field * Removed bottom padding from password text field

View file

@ -58,8 +58,8 @@ class LoginScreen extends StatelessWidget {
body: EmailPasswordLoginForm( body: EmailPasswordLoginForm(
options: loginOptions, options: loginOptions,
onLogin: (email, password) => print('$email:$password'), onLogin: (email, password) => print('$email:$password'),
onRegister: (email, password) => print('Register!'), onRegister: (email, password, ctx) => print('Register!'),
onForgotPassword: (email) { onForgotPassword: (email, ctx) {
Navigator.of(context).push( Navigator.of(context).push(
MaterialPageRoute( MaterialPageRoute(
builder: (context) { builder: (context) {

View file

@ -10,6 +10,7 @@ class LoginSpacerOptions {
this.spacerAfterForm, this.spacerAfterForm,
this.spacerAfterButton, this.spacerAfterButton,
this.formFlexValue = 1, this.formFlexValue = 1,
this.titleSpacer = 1,
}); });
/// Flex value for the spacer before the title. /// Flex value for the spacer before the title.
@ -33,4 +34,6 @@ class LoginSpacerOptions {
/// Flex value for the form. Defaults to 1. Use this when also using the /// Flex value for the form. Defaults to 1. Use this when also using the
/// other spacer options. /// other spacer options.
final int formFlexValue; final int formFlexValue;
final int titleSpacer;
} }

View file

@ -13,8 +13,12 @@ class EmailPasswordLoginForm extends StatefulWidget {
}); });
final LoginOptions options; final LoginOptions options;
final void Function(String email)? onForgotPassword; final void Function(String email, BuildContext ctx)? onForgotPassword;
final FutureOr<void> Function(String email, String password)? onRegister; final FutureOr<void> Function(
String email,
String password,
BuildContext context,
)? onRegister;
final FutureOr<void> Function(String email, String password) onLogin; final FutureOr<void> Function(String email, String password) onLogin;
@override @override
@ -74,57 +78,56 @@ class _EmailPasswordLoginFormState extends State<EmailPasswordLoginForm> {
var theme = Theme.of(context); var theme = Theme.of(context);
var options = widget.options; var options = widget.options;
return CustomScrollView( return CustomScrollView(
physics: MediaQuery.of(context).viewInsets.bottom == 0 physics: const ScrollPhysics(),
? const NeverScrollableScrollPhysics()
: null,
slivers: [ slivers: [
SliverFillRemaining( SliverFillRemaining(
fillOverscroll: true,
hasScrollBody: false, hasScrollBody: false,
fillOverscroll: true,
child: Column( child: Column(
children: [ children: [
if (options.spacers.spacerBeforeTitle != null) ...[ Expanded(
Spacer(flex: options.spacers.spacerBeforeTitle!), flex: options.spacers.titleSpacer,
], child: Column(
if (options.title != null) ...[ children: [
const SizedBox( if (options.spacers.spacerBeforeTitle != null) ...[
height: 60, Spacer(flex: options.spacers.spacerBeforeTitle!),
],
if (options.title != null) ...[
Align(
alignment: Alignment.topCenter,
child: _wrapWithDefaultStyle(
options.title,
theme.textTheme.headlineSmall,
),
),
],
if (options.spacers.spacerAfterTitle != null) ...[
Spacer(flex: options.spacers.spacerAfterTitle!),
],
if (options.subtitle != null) ...[
Align(
alignment: Alignment.topCenter,
child: _wrapWithDefaultStyle(
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!),
],
],
), ),
Align( ),
alignment: Alignment.topCenter,
child: _wrapWithDefaultStyle(
options.title,
theme.textTheme.headlineSmall,
),
),
],
if (options.spacers.spacerAfterTitle != null) ...[
Spacer(flex: options.spacers.spacerAfterTitle!),
],
if (options.subtitle != null) ...[
const SizedBox(
height: 10,
),
Align(
alignment: Alignment.topCenter,
child: _wrapWithDefaultStyle(
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!),
],
Expanded( Expanded(
flex: options.spacers.formFlexValue, flex: options.spacers.formFlexValue,
child: ConstrainedBox( child: ConstrainedBox(
@ -133,101 +136,98 @@ class _EmailPasswordLoginFormState extends State<EmailPasswordLoginForm> {
), ),
child: Form( child: Form(
key: _formKey, key: _formKey,
child: Align( child: Column(
child: Column( mainAxisSize: MainAxisSize.min,
mainAxisSize: MainAxisSize.min, children: [
children: [ options.emailInputContainerBuilder(
options.emailInputContainerBuilder( TextFormField(
TextFormField( onChanged: _updateCurrentEmail,
onChanged: _updateCurrentEmail, validator: widget.options.validations.validateEmail,
validator: initialValue: options.initialEmail,
widget.options.validations.validateEmail, keyboardType: TextInputType.emailAddress,
initialValue: options.initialEmail, textInputAction: TextInputAction.next,
keyboardType: TextInputType.emailAddress, style: options.emailTextStyle,
textInputAction: TextInputAction.next, decoration: options.emailDecoration,
style: options.emailTextStyle, ),
decoration: options.emailDecoration, ),
options.passwordInputContainerBuilder(
TextFormField(
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,
), ),
), ),
options.passwordInputContainerBuilder( ),
TextFormField( if (widget.onForgotPassword != null) ...[
obscureText: _obscurePassword, Align(
onChanged: _updateCurrentPassword, alignment: Alignment.topRight,
validator: child: options.forgotPasswordButtonBuilder(
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,
),
),
),
if (widget.onForgotPassword != null) ...[
Align(
alignment: Alignment.topRight,
child: options.forgotPasswordButtonBuilder(
context,
() {
widget.onForgotPassword?.call(_currentEmail);
},
false,
() {},
options,
),
),
],
if (options.spacers.spacerAfterForm != null) ...[
Spacer(flex: options.spacers.spacerAfterForm!),
],
const SizedBox(height: 8),
AnimatedBuilder(
animation: _formValid,
builder: (context, _) => options.loginButtonBuilder(
context, context,
_handleLogin,
!_formValid.value,
() { () {
_formKey.currentState?.validate(); widget.onForgotPassword
}, ?.call(_currentEmail, context);
options,
),
),
if (widget.onRegister != null) ...[
options.registrationButtonBuilder(
context,
() async {
widget.onRegister?.call(
_currentEmail,
_currentPassword,
);
}, },
false, false,
() {}, () {},
options, options,
), ),
], ),
if (options.spacers.spacerAfterButton != null) ...[
Spacer(flex: options.spacers.spacerAfterButton!),
],
], ],
), 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,
context,
);
},
false,
() {},
options,
),
],
if (options.spacers.spacerAfterButton != null) ...[
Spacer(flex: options.spacers.spacerAfterButton!),
],
],
), ),
), ),
), ),

View file

@ -1,6 +1,6 @@
name: flutter_login name: flutter_login
description: Flutter Login Component description: Flutter Login Component
version: 5.1.2 version: 5.1.4
environment: environment:
sdk: ">=2.18.1 <3.0.0" sdk: ">=2.18.1 <3.0.0"