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
* Removed bottom padding from password text field

View file

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

View file

@ -10,6 +10,7 @@ class LoginSpacerOptions {
this.spacerAfterForm,
this.spacerAfterButton,
this.formFlexValue = 1,
this.titleSpacer = 1,
});
/// 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
/// other spacer options.
final int formFlexValue;
final int titleSpacer;
}

View file

@ -13,8 +13,12 @@ class EmailPasswordLoginForm extends StatefulWidget {
});
final LoginOptions options;
final void Function(String email)? onForgotPassword;
final FutureOr<void> Function(String email, String password)? onRegister;
final void Function(String email, BuildContext ctx)? onForgotPassword;
final FutureOr<void> Function(
String email,
String password,
BuildContext context,
)? onRegister;
final FutureOr<void> Function(String email, String password) onLogin;
@override
@ -74,22 +78,21 @@ class _EmailPasswordLoginFormState extends State<EmailPasswordLoginForm> {
var theme = Theme.of(context);
var options = widget.options;
return CustomScrollView(
physics: MediaQuery.of(context).viewInsets.bottom == 0
? const NeverScrollableScrollPhysics()
: null,
physics: const ScrollPhysics(),
slivers: [
SliverFillRemaining(
fillOverscroll: true,
hasScrollBody: false,
fillOverscroll: true,
child: Column(
children: [
Expanded(
flex: options.spacers.titleSpacer,
child: Column(
children: [
if (options.spacers.spacerBeforeTitle != null) ...[
Spacer(flex: options.spacers.spacerBeforeTitle!),
],
if (options.title != null) ...[
const SizedBox(
height: 60,
),
Align(
alignment: Alignment.topCenter,
child: _wrapWithDefaultStyle(
@ -102,9 +105,6 @@ class _EmailPasswordLoginFormState extends State<EmailPasswordLoginForm> {
Spacer(flex: options.spacers.spacerAfterTitle!),
],
if (options.subtitle != null) ...[
const SizedBox(
height: 10,
),
Align(
alignment: Alignment.topCenter,
child: _wrapWithDefaultStyle(
@ -125,6 +125,9 @@ class _EmailPasswordLoginFormState extends State<EmailPasswordLoginForm> {
if (options.spacers.spacerAfterImage != null) ...[
Spacer(flex: options.spacers.spacerAfterImage!),
],
],
),
),
Expanded(
flex: options.spacers.formFlexValue,
child: ConstrainedBox(
@ -133,15 +136,13 @@ class _EmailPasswordLoginFormState extends State<EmailPasswordLoginForm> {
),
child: Form(
key: _formKey,
child: Align(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
options.emailInputContainerBuilder(
TextFormField(
onChanged: _updateCurrentEmail,
validator:
widget.options.validations.validateEmail,
validator: widget.options.validations.validateEmail,
initialValue: options.initialEmail,
keyboardType: TextInputType.emailAddress,
textInputAction: TextInputAction.next,
@ -165,8 +166,7 @@ class _EmailPasswordLoginFormState extends State<EmailPasswordLoginForm> {
? IconButton(
onPressed: () {
setState(() {
_obscurePassword =
!_obscurePassword;
_obscurePassword = !_obscurePassword;
});
},
icon: Icon(
@ -185,7 +185,8 @@ class _EmailPasswordLoginFormState extends State<EmailPasswordLoginForm> {
child: options.forgotPasswordButtonBuilder(
context,
() {
widget.onForgotPassword?.call(_currentEmail);
widget.onForgotPassword
?.call(_currentEmail, context);
},
false,
() {},
@ -196,7 +197,6 @@ class _EmailPasswordLoginFormState extends State<EmailPasswordLoginForm> {
if (options.spacers.spacerAfterForm != null) ...[
Spacer(flex: options.spacers.spacerAfterForm!),
],
const SizedBox(height: 8),
AnimatedBuilder(
animation: _formValid,
builder: (context, _) => options.loginButtonBuilder(
@ -216,6 +216,7 @@ class _EmailPasswordLoginFormState extends State<EmailPasswordLoginForm> {
widget.onRegister?.call(
_currentEmail,
_currentPassword,
context,
);
},
false,
@ -231,7 +232,6 @@ class _EmailPasswordLoginFormState extends State<EmailPasswordLoginForm> {
),
),
),
),
],
),
),

View file

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