feat: add AutoFillGroup to support native password managers

This commit is contained in:
Tobias Leijs 2024-06-25 15:09:04 +02:00
parent fb80dbb196
commit bb65426298
2 changed files with 120 additions and 107 deletions

View file

@ -153,108 +153,118 @@ class _EmailPasswordLoginFormState extends State<EmailPasswordLoginForm> {
), ),
child: Form( child: Form(
key: _formKey, key: _formKey,
child: Column( child: AutofillGroup(
mainAxisSize: MainAxisSize.min, child: Column(
children: [ mainAxisSize: MainAxisSize.min,
options.emailInputContainerBuilder( children: [
TextFormField( options.emailInputContainerBuilder(
textAlign: TextFormField(
options.emailTextAlign ?? TextAlign.start, autofillHints: const [
onChanged: _updateCurrentEmail, AutofillHints.email,
validator: AutofillHints.username,
widget.options.validations.validateEmail, ],
initialValue: options.initialEmail, textAlign:
keyboardType: TextInputType.emailAddress, options.emailTextAlign ?? TextAlign.start,
textInputAction: TextInputAction.next, onChanged: _updateCurrentEmail,
style: options.emailTextStyle, validator:
decoration: options.emailDecoration, widget.options.validations.validateEmail,
), initialValue: options.initialEmail,
), keyboardType: TextInputType.emailAddress,
options.passwordInputContainerBuilder( textInputAction: TextInputAction.next,
TextFormField( style: options.emailTextStyle,
textAlign: decoration: options.emailDecoration,
options.passwordTextAlign ?? TextAlign.start,
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(
padding: options.suffixIconPadding,
onPressed: () {
setState(() {
_obscurePassword =
!_obscurePassword;
});
},
icon: Icon(
_obscurePassword
? Icons.visibility
: Icons.visibility_off,
size: options.suffixIconSize,
),
)
: null,
), ),
), ),
), options.passwordInputContainerBuilder(
if (widget.onForgotPassword != null) ...[ TextFormField(
Align( autofillHints: const [
alignment: Alignment.topRight, AutofillHints.password,
child: options.forgotPasswordButtonBuilder( ],
textAlign: options.passwordTextAlign ??
TextAlign.start,
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(
padding: options.suffixIconPadding,
onPressed: () {
setState(() {
_obscurePassword =
!_obscurePassword;
});
},
icon: Icon(
_obscurePassword
? Icons.visibility
: Icons.visibility_off,
size: options.suffixIconSize,
),
)
: null,
),
),
),
if (widget.onForgotPassword != null) ...[
Align(
alignment: Alignment.topRight,
child: options.forgotPasswordButtonBuilder(
context,
() {
widget.onForgotPassword
?.call(_currentEmail, context);
},
false,
() {},
options,
),
),
] else ...[
const SizedBox(height: 16),
],
if (options.spacers.spacerAfterForm != null) ...[
Spacer(flex: options.spacers.spacerAfterForm!),
],
AnimatedBuilder(
animation: _formValid,
builder: (context, _) =>
options.loginButtonBuilder(
context, context,
_handleLogin,
!_formValid.value,
() { () {
widget.onForgotPassword _formKey.currentState?.validate();
?.call(_currentEmail, context); },
options,
),
),
if (widget.onRegister != null) ...[
options.registrationButtonBuilder(
context,
() async {
widget.onRegister?.call(
_currentEmail,
_currentPassword,
context,
);
}, },
false, false,
() {}, () {},
options, options,
), ),
), ],
] else ...[ if (options.spacers.spacerAfterButton != null) ...[
const SizedBox(height: 16), 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

@ -129,21 +129,24 @@ class _ForgotPasswordFormState extends State<ForgotPasswordForm> {
), ),
child: Form( child: Form(
key: _formKey, key: _formKey,
child: Align( child: AutofillGroup(
alignment: Alignment.center, child: Align(
child: options.emailInputContainerBuilder( alignment: Alignment.center,
TextFormField( child: options.emailInputContainerBuilder(
textAlign: TextFormField(
options.emailTextAlign ?? TextAlign.start, autofillHints: const [AutofillHints.email],
focusNode: _focusNode, textAlign:
onChanged: _updateCurrentEmail, options.emailTextAlign ?? TextAlign.start,
validator: focusNode: _focusNode,
widget.options.validations.validateEmail, onChanged: _updateCurrentEmail,
initialValue: options.initialEmail, validator:
keyboardType: TextInputType.emailAddress, widget.options.validations.validateEmail,
textInputAction: TextInputAction.next, initialValue: options.initialEmail,
style: options.emailTextStyle, keyboardType: TextInputType.emailAddress,
decoration: options.emailDecoration, textInputAction: TextInputAction.next,
style: options.emailTextStyle,
decoration: options.emailDecoration,
),
), ),
), ),
), ),