1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-29 08:16:28 +00:00
comunicmobile/lib/ui/widgets/create_account_widget.dart

291 lines
8.6 KiB
Dart
Raw Normal View History

2021-04-17 08:10:31 +00:00
import 'package:comunic/helpers/account_helper.dart';
import 'package:comunic/helpers/server_config_helper.dart';
import 'package:comunic/models/new_account.dart';
import 'package:comunic/utils/input_utils.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'new_password_input_widget.dart';
/// Create account widget
///
/// @author Pierre Hubert
class CreateAccountWidget extends StatefulWidget {
final void Function() onCreated;
const CreateAccountWidget({Key? key, required this.onCreated})
2021-04-17 08:10:31 +00:00
: super(key: key);
@override
_CreateAccountWidgetState createState() => _CreateAccountWidgetState();
}
class _CreateAccountWidgetState extends State<CreateAccountWidget> {
final _firstNameController = TextEditingController();
final _lastNameController = TextEditingController();
final _emailController = TextEditingController();
final _passwordInputKey = GlobalKey<NewPasswordInputWidgetState>();
final _verifyPasswordController = TextEditingController();
bool? _acceptedTOS = false;
2021-04-17 08:10:31 +00:00
bool _isCreating = false;
CreateAccountResult? _createAccountResult;
2021-04-17 08:10:31 +00:00
bool _showErrors = false;
bool get _isFirstNameValid =>
_firstNameController.text.length >=
srvConfig!.accountInformationPolicy.minFirstNameLength;
2021-04-17 08:10:31 +00:00
bool get _isLastNameValid =>
_lastNameController.text.length >=
srvConfig!.accountInformationPolicy.minLastNameLength;
2021-04-17 08:10:31 +00:00
bool get _isEmailValid => validateEmail(_emailController.text);
bool get _isPasswordValid => _passwordInputKey.currentState!.valid;
2021-04-17 08:10:31 +00:00
bool get _isPasswordConfirmationValid =>
_passwordInputKey.currentState != null &&
_passwordInputKey.currentState!.value == _verifyPasswordController.text;
2021-04-17 08:10:31 +00:00
bool get _isFormValid =>
_isFirstNameValid &&
_isLastNameValid &&
_isEmailValid &&
_isPasswordValid &&
_isPasswordConfirmationValid &&
_acceptedTOS!;
2021-04-17 08:10:31 +00:00
String? get errorMessage => _createAccountResult ==
2021-04-17 08:10:31 +00:00
CreateAccountResult.ERROR_EXISTING_EMAIL
? tr("An account is already associated to this email address!")
: _createAccountResult == CreateAccountResult.ERROR_TOO_MANY_REQUESTS
? tr(
"Too many accounts have been created from this IP address for now. Please try again later.")
: tr(
"An error occurred while creating your account. Please try again.");
@override
Widget build(BuildContext context) {
if (_isCreating)
return Center(
child: CircularProgressIndicator(),
);
return Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: ConstrainedBox(
constraints: BoxConstraints(maxWidth: 370),
child: ListView(
children: <Widget>[
_createAccountResult != null
? buildErrorCard(errorMessage)
: Container(),
// First name
_InputEntry(
controller: _firstNameController,
label: tr("First name")!,
2021-04-17 08:10:31 +00:00
onEdited: _updateUI,
icon: Icon(Icons.perm_identity),
maxLength:
srvConfig!.accountInformationPolicy.maxFirstNameLength,
2021-04-17 08:10:31 +00:00
error: _showErrors && !_isFirstNameValid
? tr("Invalid first name!")
: null,
),
// Last name
_InputEntry(
controller: _lastNameController,
label: tr("Last name")!,
2021-04-17 08:10:31 +00:00
onEdited: _updateUI,
icon: Icon(Icons.perm_identity),
2022-03-11 15:40:56 +00:00
maxLength:
srvConfig!.accountInformationPolicy.maxLastNameLength,
2021-04-17 08:10:31 +00:00
error: _showErrors && !_isLastNameValid
? tr("Invalid last name!")
: null,
),
// Email address
_InputEntry(
controller: _emailController,
label: tr("Email address")!,
2021-04-17 08:10:31 +00:00
onEdited: _updateUI,
icon: Icon(Icons.email),
keyboard: TextInputType.emailAddress,
error: _showErrors && !_isEmailValid
? tr("Invalid email address!")
: null,
),
// Password
NewPasswordInputWidget(
key: _passwordInputKey,
label: tr("Password")!,
2021-04-17 08:10:31 +00:00
onEdited: _updateUI,
icon: Icon(Icons.lock),
user: UserInfoForPassword(
firstName: _firstNameController.text,
lastName: _lastNameController.text,
email: _emailController.text,
),
),
// Verify password
_InputEntry(
controller: _verifyPasswordController,
label: tr("Confirm your password")!,
2021-04-17 08:10:31 +00:00
onEdited: _updateUI,
icon: Icon(Icons.lock_outline),
isPassword: true,
error: _showErrors && !_isPasswordConfirmationValid
? tr("The password and its confirmation do not match!")
: null,
),
2021-04-17 09:16:21 +00:00
SizedBox(height: 10),
2021-04-17 08:10:31 +00:00
// TOS
CheckboxListTile(
title:
Text(tr("I have read and accepted the Terms Of Service.")!),
2021-04-17 08:10:31 +00:00
value: _acceptedTOS,
onChanged: (b) {
_acceptedTOS = b;
_updateUI();
},
subtitle: _showErrors && !_acceptedTOS!
2021-04-17 08:10:31 +00:00
? Text(
tr("You must accept the Terms Of Service to continue.")!,
2021-04-17 08:10:31 +00:00
style: TextStyle(color: Colors.redAccent),
)
: null,
controlAffinity: ListTileControlAffinity.leading,
secondary: IconButton(
icon: Icon(Icons.open_in_new),
onPressed: _openTOS,
),
),
2021-04-17 09:16:21 +00:00
SizedBox(height: 10),
2021-04-17 08:10:31 +00:00
// Submit button
Center(
child: ElevatedButton(
onPressed: _submitForm,
child: Text(tr("Create account")!),
2021-04-17 08:10:31 +00:00
),
),
],
),
),
),
);
}
void _updateUI() {
setState(() {
// Nothing yet
});
}
void _submitForm() async {
if (!_isFormValid) {
_showErrors = true;
_updateUI();
return;
}
setState(() {
_isCreating = true;
});
final result = await AccountHelper().createAccount(NewAccount(
firstName: _firstNameController.text,
lastName: _lastNameController.text,
email: _emailController.text,
password: _passwordInputKey.currentState!.value,
2021-04-17 08:10:31 +00:00
));
setState(() {
_isCreating = false;
});
if (result != CreateAccountResult.SUCCESS) {
_createAccountResult = result;
_showCreateAccountError();
_updateUI();
return;
}
2021-04-17 08:11:31 +00:00
widget.onCreated();
2021-04-17 08:10:31 +00:00
}
void _openTOS() => launch(ServerConfigurationHelper.config!.termsURL);
2021-04-17 08:10:31 +00:00
void _showCreateAccountError() async {
await showCupertinoDialog(
context: context,
builder: (c) => CupertinoAlertDialog(
title: Text(tr("Error while creating your account")!),
content: Text(errorMessage!),
2021-04-17 08:10:31 +00:00
actions: <Widget>[
CupertinoButton(
child: Text(tr("Ok")!.toUpperCase()),
2021-04-17 08:10:31 +00:00
onPressed: () => Navigator.of(context).pop(),
)
],
),
);
}
}
class _InputEntry extends StatelessWidget {
final TextEditingController controller;
final String label;
final VoidCallback onEdited;
final bool isPassword;
final String? error;
final Widget? icon;
final TextInputType? keyboard;
final int? maxLength;
2021-04-17 08:10:31 +00:00
const _InputEntry({
Key? key,
required this.controller,
required this.label,
required this.onEdited,
2021-04-17 08:10:31 +00:00
this.isPassword = false,
this.error,
this.icon,
this.keyboard,
this.maxLength,
2022-03-11 15:40:56 +00:00
}) : super(key: key);
2021-04-17 08:10:31 +00:00
@override
Widget build(BuildContext context) {
return TextField(
controller: controller,
onChanged: (s) => onEdited(),
keyboardType: keyboard,
obscureText: isPassword,
maxLength: maxLength,
decoration: InputDecoration(
2021-04-17 08:59:06 +00:00
counterText: "",
2021-04-17 08:10:31 +00:00
alignLabelWithHint: true,
errorText: error,
labelText: label,
icon: icon,
),
);
}
}