fooder-app/lib/screens/login.dart

139 lines
3.9 KiB
Dart
Raw Normal View History

2023-07-29 18:10:10 +02:00
import 'package:flutter/material.dart';
2023-07-30 15:31:36 +02:00
import 'package:flutter/services.dart';
import 'package:fooder/screens/based.dart';
import 'package:fooder/screens/main.dart';
import 'package:fooder/screens/register.dart';
2024-03-28 16:25:49 +01:00
import 'package:fooder/components/text.dart';
import 'package:fooder/components/button.dart';
2023-07-29 18:10:10 +02:00
class LoginScreen extends BasedScreen {
const LoginScreen({super.key, required super.apiClient});
2023-07-29 18:10:10 +02:00
@override
State<LoginScreen> createState() => _LoginScreen();
}
2024-03-28 16:25:49 +01:00
class _LoginScreen extends BasedState<LoginScreen> {
2023-07-29 18:10:10 +02:00
final usernameController = TextEditingController();
final passwordController = TextEditingController();
@override
void dispose() {
usernameController.dispose();
passwordController.dispose();
super.dispose();
}
void popMeDaddy() {
2023-07-29 20:55:32 +02:00
Navigator.pushReplacement(
2023-07-29 20:01:56 +02:00
context,
MaterialPageRoute(
builder: (context) => MainScreen(apiClient: widget.apiClient),
),
);
2023-07-29 18:10:10 +02:00
}
2023-07-29 18:42:29 +02:00
// login client when button pressed
2023-07-29 20:01:56 +02:00
Future<void> _login() async {
2023-07-29 18:42:29 +02:00
try {
await widget.apiClient.login(
usernameController.text,
passwordController.text,
);
showText("Logged in");
popMeDaddy();
2023-07-29 18:42:29 +02:00
} on Exception catch (e) {
showError(e.toString());
}
}
2023-07-29 20:55:32 +02:00
2023-07-29 20:01:56 +02:00
@override
2023-08-28 14:45:32 +02:00
void initState() {
2023-07-29 20:01:56 +02:00
super.initState();
2023-08-28 14:45:32 +02:00
SystemChannels.textInput
.invokeMethod('TextInput.setClientFeatures', <String, dynamic>{
2023-07-30 15:31:36 +02:00
'setAuthenticationConfiguration': true,
'setAutofillHints': <String>[
AutofillHints.username,
AutofillHints.password,
],
});
2023-07-29 20:01:56 +02:00
_asyncInitState().then((value) => null);
}
Future<void> _asyncInitState() async {
await widget.apiClient.loadToken();
2023-07-29 23:54:51 +02:00
if (widget.apiClient.refreshToken == null) {
return;
}
2023-07-29 20:01:56 +02:00
try {
await widget.apiClient.refresh();
showText("Welcome back!");
popMeDaddy();
2023-07-29 20:01:56 +02:00
} on Exception catch (_) {
showError("Session is not longer valid, please log in again");
}
}
2023-07-29 18:42:29 +02:00
2023-07-29 18:10:10 +02:00
@override
Widget build(BuildContext context) {
2024-03-28 16:25:49 +01:00
var theme = Theme.of(context);
var colorScheme = theme.colorScheme;
2023-07-29 18:10:10 +02:00
return Scaffold(
body: Center(
child: Container(
constraints: const BoxConstraints(maxWidth: 600),
padding: const EdgeInsets.all(10),
2023-07-30 15:37:11 +02:00
child: AutofillGroup(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
2024-03-28 16:25:49 +01:00
Icon(
Icons.lock,
size: 100,
2024-03-29 16:47:25 +01:00
color: colorScheme.primary.withOpacity(0.85),
2024-03-28 16:25:49 +01:00
),
FTextInput(
labelText: 'Username',
2023-07-30 15:37:11 +02:00
controller: usernameController,
autofillHints: const [AutofillHints.username],
2023-08-03 13:27:52 +02:00
autofocus: true,
2023-07-29 23:54:51 +02:00
),
2024-03-28 16:25:49 +01:00
FTextInput(
labelText: 'Password',
2023-07-30 15:37:11 +02:00
controller: passwordController,
onFieldSubmitted: (_) => _login(),
autofillHints: const [AutofillHints.password],
2024-03-28 16:25:49 +01:00
obscureText: true,
2023-07-29 23:54:51 +02:00
),
2024-03-28 16:25:49 +01:00
FButton(
labelText: 'Sign In',
onPressed: _login,
2023-07-30 13:42:38 +02:00
),
2023-07-30 15:37:11 +02:00
Padding(
2024-03-28 16:25:49 +01:00
padding: const EdgeInsets.symmetric(vertical: 8),
2023-07-30 15:37:11 +02:00
child: TextButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
2023-08-28 14:45:32 +02:00
builder: (context) =>
RegisterScreen(apiClient: widget.apiClient),
2023-07-30 15:37:11 +02:00
),
);
},
child: const Text('Don\'t have an account? Register here!'),
),
2023-07-30 13:42:38 +02:00
),
2023-07-30 15:37:11 +02:00
],
),
2023-07-29 18:10:10 +02:00
),
),
),
);
}
}