login: implement login on enter

This commit is contained in:
2025-09-29 22:18:41 +02:00
parent c0091e7f44
commit bde317e159

View File

@@ -18,8 +18,11 @@ class _LoginPageState extends State<LoginPage> {
final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
final _formKey = GlobalKey<FormState>();
final StorageHelper _storageHelper = StorageHelper();
String? _error;
bool loggingIn = false;
@override
void initState() {
@@ -83,47 +86,76 @@ class _LoginPageState extends State<LoginPage> {
);
}
String? validateNotEmpty(String? content) {
if (content == null || content.isEmpty) {
return "Do not leave this field empty";
}
return null;
}
void handleLoginPressed() async {
if (loggingIn) return;
loggingIn = true;
if (_formKey.currentState!.validate()) {
await _login(
_urlController.text,
_emailController.text,
_passwordController.text,
);
}
loggingIn = false;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Login')),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
TextFormField(
controller: _urlController,
validator: validateNotEmpty,
decoration: InputDecoration(labelText: 'Url'),
textInputAction: TextInputAction.next,
),
TextField(
TextFormField(
controller: _emailController,
validator: validateNotEmpty,
autofocus: true,
decoration: InputDecoration(labelText: 'Email'),
textInputAction: TextInputAction.next,
),
TextField(
TextFormField(
controller: _passwordController,
validator: validateNotEmpty,
// focusNode: _passwordFocusNode,
decoration: InputDecoration(labelText: 'Password'),
obscureText: true,
textInputAction: TextInputAction
.next, // with submit or go, onFieldSubmitted is not called
onFieldSubmitted: (_) => handleLoginPressed(),
),
// ),
SizedBox(height: 5),
ElevatedButton(
onPressed: loggingIn ? null : handleLoginPressed,
child: Text('Login'),
),
if (_error != null)
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Text(_error!, style: TextStyle(color: Colors.red)),
),
SizedBox(height: 16),
ElevatedButton(
onPressed: () {
_login(
_urlController.text,
_emailController.text,
_passwordController.text,
);
},
child: Text('Login'),
),
],
),
),
),
);
}
}