diff --git a/lib/pages/login_page.dart b/lib/pages/login_page.dart index 4f57549..b2aef41 100644 --- a/lib/pages/login_page.dart +++ b/lib/pages/login_page.dart @@ -17,11 +17,14 @@ class LoginPage extends HookConsumerWidget { final password = useTextEditingController(); final inputError = useState(null); + final formKey = useRef(GlobalKey()); Future 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(