Add loader for logging in

This commit is contained in:
Brandon Presley 2023-12-29 14:50:00 +13:00
parent 302f46804e
commit 5c4526f1fc

View File

@ -15,11 +15,36 @@ class _LoginPageState extends State<LoginPage> {
final serverController = TextEditingController();
final usernameController = TextEditingController();
final passwordController = TextEditingController();
bool loggingIn = false;
String failedMessage = '';
void connectMatrix() async {
final provider = Provider.of<ZenithClientProvider>(context, listen: false);
await provider.initialize(serverController.text, usernameController.text,
passwordController.text);
setState(() {
loggingIn = true;
});
try {
final provider =
Provider.of<ZenithClientProvider>(context, listen: false);
await provider.initialize(serverController.text, usernameController.text,
passwordController.text);
} catch (error) {
print("Error signing in $error");
setState(() {
failedMessage = error.toString();
});
Future.delayed(const Duration(seconds: 10), () {
setState(() {
failedMessage = '';
});
});
return;
} finally {
setState(() {
loggingIn = false;
});
}
final prefs = await SharedPreferences.getInstance();
prefs.setString("homeserver", serverController.text);
@ -40,40 +65,47 @@ class _LoginPageState extends State<LoginPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text("Login"),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextFormField(
controller: serverController,
decoration: const InputDecoration(
labelText: 'Server', hintText: 'https://matrix.org'),
),
TextFormField(
controller: usernameController,
decoration: const InputDecoration(
labelText: 'Username', hintText: 'john'),
),
TextFormField(
controller: passwordController,
decoration: const InputDecoration(labelText: 'Password'),
obscureText: true,
),
],
return Consumer<ZenithClientProvider>(
builder: (context, provider, child) => Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text("Login"),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextFormField(
controller: serverController,
decoration: const InputDecoration(
labelText: 'Server', hintText: 'https://matrix.org'),
),
TextFormField(
controller: usernameController,
decoration: const InputDecoration(
labelText: 'Username', hintText: 'john'),
),
TextFormField(
controller: passwordController,
decoration: const InputDecoration(labelText: 'Password'),
obscureText: true,
onFieldSubmitted: (value) => connectMatrix(),
),
Text(failedMessage,
style: Theme.of(context).textTheme.headlineSmall)
],
),
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: connectMatrix,
tooltip: 'Log in',
child: const Icon(Icons.login),
floatingActionButton: loggingIn
? const CircularProgressIndicator()
: FloatingActionButton(
onPressed: connectMatrix,
tooltip: 'Log in',
child: const Icon(Icons.login),
),
),
);
}