fooder-app/lib/screens/register.dart

113 lines
3.2 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';
2024-03-28 16:25:49 +01:00
import 'package:fooder/components/text.dart';
import 'package:fooder/components/button.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();
}
2024-03-28 16:25:49 +01:00
class _RegisterScreen extends BasedState<RegisterScreen> {
2023-07-30 13:42:38 +02:00
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();
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,
],
});
}
void popMeDaddy() {
2023-07-30 13:42:38 +02:00
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.");
popMeDaddy();
2023-07-30 13:42:38 +02:00
} on Exception catch (e) {
showError(e.toString());
}
}
@override
Widget build(BuildContext context) {
2024-03-28 16:25:49 +01:00
var theme = Theme.of(context);
var colorScheme = theme.colorScheme;
2023-07-30 13:42:38 +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.group_add,
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],
2024-03-28 16:25:49 +01:00
autofocus: true,
2023-07-30 13:42:38 +02:00
),
2024-03-28 16:25:49 +01:00
FTextInput(
labelText: 'Password',
2023-07-30 15:37:11 +02:00
controller: passwordController,
autofillHints: const [AutofillHints.password],
2024-03-28 16:25:49 +01:00
obscureText: true,
),
FTextInput(
labelText: 'Confirm password',
controller: passwordConfirmController,
autofillHints: const [AutofillHints.password],
onFieldSubmitted: (_) => _register(),
obscureText: true,
),
FButton(
labelText: 'Register account',
onPressed: _register,
2023-07-30 13:42:38 +02:00
),
2023-07-30 15:37:11 +02:00
],
),
2023-07-30 13:42:38 +02:00
),
),
),
);
}
}