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';
|
2023-07-29 18:10:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
class LoginScreen extends BasedScreen {
|
2023-07-29 18:27:11 +02:00
|
|
|
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 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");
|
|
|
|
popMeDady();
|
|
|
|
} on Exception catch (e) {
|
|
|
|
showError(e.toString());
|
|
|
|
}
|
|
|
|
}
|
2023-07-29 20:55:32 +02:00
|
|
|
|
2023-07-29 20:01:56 +02:00
|
|
|
@override
|
|
|
|
void initState () {
|
|
|
|
super.initState();
|
2023-07-30 15:31:36 +02:00
|
|
|
SystemChannels.textInput.invokeMethod('TextInput.setClientFeatures', <String, dynamic>{
|
|
|
|
'setAuthenticationConfiguration': true,
|
|
|
|
'setAutofillHints': <String>[
|
|
|
|
AutofillHints.username,
|
|
|
|
AutofillHints.password,
|
|
|
|
],
|
|
|
|
});
|
2023-07-29 20:01:56 +02:00
|
|
|
_asyncInitState().then((value) => null);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _asyncInitState() async {
|
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!");
|
|
|
|
popMeDady();
|
|
|
|
} 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) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
2023-07-30 14:40:45 +02:00
|
|
|
title: const Text("🅵🅾🅾🅳🅴🆁"),
|
2023-07-29 18:10:10 +02:00
|
|
|
),
|
|
|
|
body: Center(
|
|
|
|
child: Container(
|
2023-07-29 18:27:11 +02:00
|
|
|
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>[
|
|
|
|
TextFormField(
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
labelText: 'Username',
|
|
|
|
),
|
|
|
|
controller: usernameController,
|
|
|
|
autofillHints: const [AutofillHints.username],
|
2023-07-29 23:54:51 +02:00
|
|
|
),
|
2023-07-30 15:37:11 +02:00
|
|
|
TextFormField(
|
|
|
|
obscureText: true,
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
labelText: 'Password',
|
|
|
|
),
|
|
|
|
controller: passwordController,
|
|
|
|
onFieldSubmitted: (_) => _login(),
|
|
|
|
autofillHints: const [AutofillHints.password],
|
2023-07-29 23:54:51 +02:00
|
|
|
),
|
2023-07-30 15:37:11 +02:00
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 10),
|
|
|
|
child: FilledButton(
|
|
|
|
onPressed: _login,
|
|
|
|
child: const Text('Login'),
|
|
|
|
),
|
2023-07-30 13:42:38 +02:00
|
|
|
),
|
2023-07-30 15:37:11 +02:00
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 10),
|
|
|
|
child: TextButton(
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.push(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (context) => RegisterScreen(apiClient: widget.apiClient),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
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
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|