flutter_registration/lib/src/registration_screen.dart

87 lines
2.3 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';
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'),
),
],
),
);
2022-09-22 11:48:04 +02:00
void register(values) => repository
.register(values)
2022-09-20 15:51:22 +02:00
.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: [
2022-09-23 16:26:30 +02:00
(email) => (email == null || email.isEmpty)
2022-09-20 15:51:22 +02:00
? 'Geef uw e-mailadres op'
: null,
2022-09-23 16:26:30 +02:00
(email) => RegExp(
r'^.+@[a-zA-Z]+\.{1}[a-zA-Z]+(\.{0,1}[a-zA-Z]+)$',
).hasMatch(email!)
? 'Geef een geldig e-mailadres op'
: null,
2022-09-20 15:51:22 +02:00
],
)
],
),
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
],
2022-09-22 11:48:04 +02:00
onFinish: register,
2022-09-20 15:51:22 +02:00
);
}
}