From b4bdec0d5ad226b15230c72fbaac89b3fbe7e2df Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Wed, 28 Apr 2021 11:12:55 +0200 Subject: [PATCH] Add autofill group to fields in auth screen --- lib/screens/auth_screen.dart | 282 ++++++++++++++++++----------------- 1 file changed, 142 insertions(+), 140 deletions(-) diff --git a/lib/screens/auth_screen.dart b/lib/screens/auth_screen.dart index 7fc03f68..02f41002 100644 --- a/lib/screens/auth_screen.dart +++ b/lib/screens/auth_screen.dart @@ -177,163 +177,165 @@ class _AuthCardState extends State { child: Form( key: _formKey, child: SingleChildScrollView( - child: Column( - children: [ - TextFormField( - key: Key('inputUsername'), - decoration: InputDecoration(labelText: AppLocalizations.of(context)!.username), - autofillHints: [AutofillHints.username], - controller: _usernameController, - textInputAction: TextInputAction.next, - keyboardType: TextInputType.emailAddress, - validator: (value) { - if (value!.isEmpty) { - return AppLocalizations.of(context)!.invalidUsername; - } - return null; - }, - onSaved: (value) { - _authData['username'] = value!; - }, - ), - if (_authMode == AuthMode.Signup) + child: AutofillGroup( + child: Column( + children: [ TextFormField( - key: Key('inputEmail'), - decoration: InputDecoration(labelText: AppLocalizations.of(context)!.email), - autofillHints: [AutofillHints.email], - controller: _emailController, - keyboardType: TextInputType.emailAddress, + key: Key('inputUsername'), + decoration: InputDecoration(labelText: AppLocalizations.of(context)!.username), + autofillHints: [AutofillHints.username], + controller: _usernameController, textInputAction: TextInputAction.next, - - // Email is not required + keyboardType: TextInputType.emailAddress, validator: (value) { - if (value!.isNotEmpty && !value.contains('@')) { - return AppLocalizations.of(context)!.invalidEmail; + if (value!.isEmpty) { + return AppLocalizations.of(context)!.invalidUsername; } return null; }, onSaved: (value) { - _authData['email'] = value!; + _authData['username'] = value!; }, ), - TextFormField( - key: Key('inputPassword'), - decoration: InputDecoration(labelText: AppLocalizations.of(context)!.password), - autofillHints: [AutofillHints.password], - obscureText: true, - controller: _passwordController, - textInputAction: TextInputAction.next, - validator: (value) { - if (value!.isEmpty || value.length < 8) { - return AppLocalizations.of(context)!.passwordTooShort; - } - return null; - }, - onSaved: (value) { - _authData['password'] = value!; - }, - ), - if (_authMode == AuthMode.Signup) - TextFormField( - key: Key('inputPassword2'), - decoration: - InputDecoration(labelText: AppLocalizations.of(context)!.confirmPassword), - controller: _password2Controller, - enabled: _authMode == AuthMode.Signup, - obscureText: true, - validator: _authMode == AuthMode.Signup - ? (value) { - if (value != _passwordController.text) { - return AppLocalizations.of(context)!.passwordsDontMatch; - } - return null; - } - : null, - ), - // Off-stage widgets are kept in the tree, otherwise the server URL - // would not be saved to _authData - Offstage( - offstage: _hideCustomServer, - child: Row( - children: [ - Flexible( - flex: 3, - child: TextFormField( - key: Key('inputServer'), - decoration: InputDecoration( - labelText: AppLocalizations.of(context)!.customServerUrl, - helperText: AppLocalizations.of(context)!.customServerHint, - helperMaxLines: 4), - controller: _serverUrlController, - validator: (value) { - if (Uri.tryParse(value!) == null) { - return AppLocalizations.of(context)!.invalidUrl; - } + if (_authMode == AuthMode.Signup) + TextFormField( + key: Key('inputEmail'), + decoration: InputDecoration(labelText: AppLocalizations.of(context)!.email), + autofillHints: [AutofillHints.email], + controller: _emailController, + keyboardType: TextInputType.emailAddress, + textInputAction: TextInputAction.next, - if (value.isEmpty || !value.contains('http')) { - return AppLocalizations.of(context)!.invalidUrl; + // Email is not required + validator: (value) { + if (value!.isNotEmpty && !value.contains('@')) { + return AppLocalizations.of(context)!.invalidEmail; + } + return null; + }, + onSaved: (value) { + _authData['email'] = value!; + }, + ), + TextFormField( + key: Key('inputPassword'), + decoration: InputDecoration(labelText: AppLocalizations.of(context)!.password), + autofillHints: [AutofillHints.password], + obscureText: true, + controller: _passwordController, + textInputAction: TextInputAction.next, + validator: (value) { + if (value!.isEmpty || value.length < 8) { + return AppLocalizations.of(context)!.passwordTooShort; + } + return null; + }, + onSaved: (value) { + _authData['password'] = value!; + }, + ), + if (_authMode == AuthMode.Signup) + TextFormField( + key: Key('inputPassword2'), + decoration: + InputDecoration(labelText: AppLocalizations.of(context)!.confirmPassword), + controller: _password2Controller, + enabled: _authMode == AuthMode.Signup, + obscureText: true, + validator: _authMode == AuthMode.Signup + ? (value) { + if (value != _passwordController.text) { + return AppLocalizations.of(context)!.passwordsDontMatch; + } + return null; } - return null; - }, - onSaved: (value) { - _authData['serverUrl'] = value!; - }, - ), - ), - SizedBox( - width: 20, - ), - Column( - mainAxisSize: MainAxisSize.min, - children: [ - IconButton( - icon: const Icon(Icons.undo), - onPressed: () { - _serverUrlController.text = DEFAULT_SERVER; + : null, + ), + // Off-stage widgets are kept in the tree, otherwise the server URL + // would not be saved to _authData + Offstage( + offstage: _hideCustomServer, + child: Row( + children: [ + Flexible( + flex: 3, + child: TextFormField( + key: Key('inputServer'), + decoration: InputDecoration( + labelText: AppLocalizations.of(context)!.customServerUrl, + helperText: AppLocalizations.of(context)!.customServerHint, + helperMaxLines: 4), + controller: _serverUrlController, + validator: (value) { + if (Uri.tryParse(value!) == null) { + return AppLocalizations.of(context)!.invalidUrl; + } + + if (value.isEmpty || !value.contains('http')) { + return AppLocalizations.of(context)!.invalidUrl; + } + return null; + }, + onSaved: (value) { + _authData['serverUrl'] = value!; }, ), - Text(AppLocalizations.of(context)!.reset) - ], - ), - ], + ), + SizedBox( + width: 20, + ), + Column( + mainAxisSize: MainAxisSize.min, + children: [ + IconButton( + icon: const Icon(Icons.undo), + onPressed: () { + _serverUrlController.text = DEFAULT_SERVER; + }, + ), + Text(AppLocalizations.of(context)!.reset) + ], + ), + ], + ), ), - ), - SizedBox( - height: 20, - ), - if (_isLoading) - CircularProgressIndicator() - else - ElevatedButton( - key: Key('actionButton'), - child: Text(_authMode == AuthMode.Login - ? AppLocalizations.of(context)!.login - : AppLocalizations.of(context)!.register), + SizedBox( + height: 20, + ), + if (_isLoading) + CircularProgressIndicator() + else + ElevatedButton( + key: Key('actionButton'), + child: Text(_authMode == AuthMode.Login + ? AppLocalizations.of(context)!.login + : AppLocalizations.of(context)!.register), + onPressed: () { + return _submit(context); + }, + ), + TextButton( + key: Key('toggleActionButton'), + child: Text( + _authMode == AuthMode.Login + ? AppLocalizations.of(context)!.registerInstead.toUpperCase() + : AppLocalizations.of(context)!.loginInstead.toUpperCase(), + ), + onPressed: _switchAuthMode, + ), + TextButton( + child: Text(_hideCustomServer + ? AppLocalizations.of(context)!.useCustomServer + : AppLocalizations.of(context)!.useDefaultServer), + key: Key('toggleCustomServerButton'), onPressed: () { - return _submit(context); + setState(() { + _hideCustomServer = !_hideCustomServer; + }); }, ), - TextButton( - key: Key('toggleActionButton'), - child: Text( - _authMode == AuthMode.Login - ? AppLocalizations.of(context)!.registerInstead.toUpperCase() - : AppLocalizations.of(context)!.loginInstead.toUpperCase(), - ), - onPressed: _switchAuthMode, - ), - TextButton( - child: Text(_hideCustomServer - ? AppLocalizations.of(context)!.useCustomServer - : AppLocalizations.of(context)!.useDefaultServer), - key: Key('toggleCustomServerButton'), - onPressed: () { - setState(() { - _hideCustomServer = !_hideCustomServer; - }); - }, - ), - ], + ], + ), ), ), ),