flutter_registration/lib/src/registration_screen.dart

89 lines
2.4 KiB
Dart
Raw Normal View History

2022-09-20 15:51:22 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_registration/flutter_registration.dart';
import 'package:flutter_registration/src/auth_screen.dart';
2022-09-22 10:09:45 +02:00
import 'package:flutter_registration/src/model/auth_text_field.dart';
2022-09-20 15:51:22 +02:00
class RegistrationScreen extends StatelessWidget {
const RegistrationScreen({
required this.afterRegistration,
required this.repository,
2022-09-22 09:40:25 +02:00
this.additionalSteps = const [],
2022-09-20 15:51:22 +02:00
super.key,
});
final VoidCallback afterRegistration;
final RegistrationRepository repository;
2022-09-22 09:40:25 +02:00
final List<AuthStep> additionalSteps;
2022-09-20 15:51:22 +02:00
@override
Widget build(BuildContext context) {
void showError(String error) => showDialog<String>(
context: context,
builder: (BuildContext context) => AlertDialog(
content: Text(error),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.pop(context, 'Sluit'),
child: const Text('Sluit'),
),
],
),
);
void register(String email, String password) => repository
.register(email, password)
.then(
(value) => afterRegistration(),
)
.catchError(
(error) {
showError(
error.toString(),
);
},
);
return AuthScreen(
title: 'Registreren',
submitBtnTitle: 'Registreren',
steps: [
AuthStep(
fields: [
2022-09-22 10:09:45 +02:00
AuthTextField(
2022-09-20 15:51:22 +02:00
name: 'email',
title: 'Wat is je e-mailadres?',
validators: [
(value) => (value == null || value.isEmpty)
? 'Geef uw e-mailadres op'
: null,
(value) => !value!.contains('@')
? 'Geef een geldig e-mailadres op'
: null,
],
)
],
),
AuthStep(
fields: [
2022-09-22 10:09:45 +02:00
AuthTextField(
2022-09-20 15:51:22 +02:00
name: 'password',
title: 'Kies een wachtwoord',
obscureText: true,
validators: [
(value) => (value == null || value.isEmpty)
? 'Geef een wachtwoord op'
: null,
],
),
],
2022-09-22 09:40:25 +02:00
),
...additionalSteps
2022-09-20 15:51:22 +02:00
],
onFinish: (values) => register(
values['email']!,
values['password']!,
),
);
}
}