fooder-app/lib/screens/register.dart

136 lines
3.8 KiB
Dart
Raw Normal View History

2023-07-30 13:42:38 +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';
2023-07-30 13:42:38 +02:00
class RegisterScreen extends BasedScreen {
const RegisterScreen({super.key, required super.apiClient});
@override
State<RegisterScreen> createState() => _RegisterScreen();
}
class _RegisterScreen extends State<RegisterScreen> {
final usernameController = TextEditingController();
final passwordController = TextEditingController();
final passwordConfirmController = TextEditingController();
@override
void dispose() {
usernameController.dispose();
passwordController.dispose();
passwordConfirmController.dispose();
super.dispose();
}
2023-07-30 15:31:36 +02:00
@override
void initState() {
super.initState();
SystemChannels.textInput.invokeMethod('TextInput.setClientFeatures', <String, dynamic>{
'setAuthenticationConfiguration': true,
'setAutofillHints': <String>[
AutofillHints.username,
AutofillHints.password,
],
});
}
2023-07-30 13:42:38 +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,
),
);
}
void popMeDady() {
Navigator.pop(context);
}
// login client when button pressed
Future<void> _register() async {
if (passwordController.text != passwordConfirmController.text) {
showError("Passwords don't match");
return;
}
try {
await widget.apiClient.register(
usernameController.text,
passwordController.text,
);
showText("Created account. You can now log in.");
popMeDady();
} on Exception catch (e) {
showError(e.toString());
}
}
@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-30 13:42:38 +02:00
),
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>[
TextFormField(
decoration: const InputDecoration(
labelText: 'Username',
),
controller: usernameController,
autofillHints: const [AutofillHints.username],
2023-07-30 13:42:38 +02:00
),
2023-07-30 15:37:11 +02:00
TextFormField(
obscureText: true,
decoration: const InputDecoration(
labelText: 'Password',
),
controller: passwordController,
autofillHints: const [AutofillHints.password],
2023-07-30 13:42:38 +02:00
),
2023-07-30 15:37:11 +02:00
TextFormField(
obscureText: true,
decoration: const InputDecoration(
labelText: 'Confirm password',
),
controller: passwordConfirmController,
autofillHints: const [AutofillHints.password],
onFieldSubmitted: (_) => _register()
2023-07-30 13:42:38 +02:00
),
2023-07-30 15:37:11 +02:00
Padding(
padding: const EdgeInsets.symmetric(vertical: 10),
child: FilledButton(
onPressed: _register,
child: const Text('Register'),
)
),
],
),
2023-07-30 13:42:38 +02:00
),
),
),
);
}
}