fooder-app/lib/login_screen.dart

100 lines
2.4 KiB
Dart
Raw Normal View History

2023-07-29 18:10:10 +02:00
import 'package:flutter/material.dart';
import 'package:fooder_web/based.dart';
class LoginScreen extends BasedScreen {
const LoginScreen({super.key, required super.apiClient});
2023-07-29 18:10:10 +02:00
@override
State<LoginScreen> createState() => _LoginScreen();
}
class _LoginScreen extends State<LoginScreen> {
final usernameController = TextEditingController();
final passwordController = TextEditingController();
@override
void dispose() {
usernameController.dispose();
passwordController.dispose();
super.dispose();
}
2023-07-29 18:42:29 +02:00
void showError(String message)
{
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message, textAlign: TextAlign.center),
backgroundColor: Theme.of(context).colorScheme.error,
),
);
}
void showText(String text)
{
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(text, textAlign: TextAlign.center),
backgroundColor: Theme.of(context).colorScheme.primary,
),
2023-07-29 18:10:10 +02:00
);
2023-07-29 18:42:29 +02:00
}
void popMeDady() {
2023-07-29 18:10:10 +02:00
Navigator.pop(context);
}
2023-07-29 18:42:29 +02:00
// login client when button pressed
void _login() async {
try {
await widget.apiClient.login(
usernameController.text,
passwordController.text,
);
showText("Logged in");
popMeDady();
} on Exception catch (e) {
showError(e.toString());
}
}
2023-07-29 18:10:10 +02:00
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
2023-07-29 18:42:29 +02:00
title: const Text("FOODER login"),
2023-07-29 18:10:10 +02:00
),
body: Center(
child: Container(
constraints: const BoxConstraints(maxWidth: 600),
padding: const EdgeInsets.all(10),
2023-07-29 18:10:10 +02:00
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextFormField(
decoration: const InputDecoration(
labelText: 'Username',
),
controller: usernameController,
),
TextFormField(
obscureText: true,
decoration: const InputDecoration(
labelText: 'Password',
),
controller: passwordController,
),
FilledButton(
onPressed: _login,
child: const Text('Login'),
2023-07-29 18:10:10 +02:00
)
],
),
),
),
);
}
}