make login page use a Form

This commit is contained in:
Henry Hiles 2026-06-05 17:17:42 -04:00
commit a98afcff3d
Signed by: Henry-Hiles
SSH key fingerprint: SHA256:VKQUdS31Q90KvX7EkKMHMBpUspcmItAh86a+v7PGiIs

View file

@ -17,11 +17,14 @@ class LoginPage extends HookConsumerWidget {
final password = useTextEditingController();
final inputError = useState<String?>(null);
final formKey = useRef(GlobalKey<FormState>());
Future<void> tryLogin() async {
isLoading.value = true;
try {
if (formKey.value.currentState?.validate() != true) return;
final error = await client.login(
.new(
username: username.text,
@ -41,6 +44,9 @@ class LoginPage extends HookConsumerWidget {
}
}
String? requiredValidator(String? value) =>
value == null || value.isEmpty ? "This field is required" : null;
return Scaffold(
appBar: Appbar(
leading: IconButton(
@ -50,30 +56,35 @@ class LoginPage extends HookConsumerWidget {
),
body: AlertDialog(
title: Text("Login to ${homeserver.host}"),
content: Column(
mainAxisSize: .min,
crossAxisAlignment: .start,
children: [
TextField(
autofocus: true,
textInputAction: .next,
decoration: .new(label: Text("Username")),
controller: username,
),
SizedBox(height: 12),
TextField(
textInputAction: .done,
onSubmitted: (_) => tryLogin(),
selectAllOnFocus: true,
decoration: .new(
label: Text("Password"),
errorText: inputError.value,
errorMaxLines: 5,
content: Form(
key: formKey.value,
child: Column(
mainAxisSize: .min,
crossAxisAlignment: .start,
children: [
TextFormField(
autofocus: true,
textInputAction: .next,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: requiredValidator,
decoration: .new(label: Text("Username")),
controller: username,
),
controller: password,
obscureText: true,
),
],
SizedBox(height: 12),
TextFormField(
textInputAction: .done,
decoration: .new(
label: Text("Password"),
errorText: inputError.value,
errorMaxLines: 5,
),
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: requiredValidator,
controller: password,
obscureText: true,
),
],
),
),
actions: [
TextButton(