2020-05-02 13:32:06 +00:00
|
|
|
import 'package:comunic/ui/widgets/dialogs/auto_sized_dialog_content_widget.dart';
|
2021-02-18 17:58:47 +00:00
|
|
|
import 'package:comunic/ui/widgets/new_password_input_widget.dart';
|
2020-04-30 16:19:01 +00:00
|
|
|
import 'package:comunic/utils/intl_utils.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
/// Ask the user to enter a new password
|
|
|
|
///
|
|
|
|
/// @author Pierre HUBERT
|
|
|
|
|
|
|
|
/// Ask the user to enter a new password
|
2022-03-10 18:39:57 +00:00
|
|
|
Future<String?> showInputNewPassword({
|
|
|
|
required BuildContext context,
|
|
|
|
required UserInfoForPassword userInfo,
|
2021-02-18 17:58:47 +00:00
|
|
|
}) async {
|
2020-04-30 16:19:01 +00:00
|
|
|
return await showDialog(
|
2021-02-18 17:58:47 +00:00
|
|
|
context: context,
|
|
|
|
builder: (c) => _InputNewPasswordDialog(
|
|
|
|
userInfo: userInfo,
|
|
|
|
));
|
2020-04-30 16:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class _InputNewPasswordDialog extends StatefulWidget {
|
2021-02-18 17:58:47 +00:00
|
|
|
final UserInfoForPassword userInfo;
|
|
|
|
|
|
|
|
const _InputNewPasswordDialog({
|
2022-03-10 18:39:57 +00:00
|
|
|
Key? key,
|
|
|
|
required this.userInfo,
|
2022-03-11 16:09:37 +00:00
|
|
|
}) : super(key: key);
|
2021-02-18 17:58:47 +00:00
|
|
|
|
2020-04-30 16:19:01 +00:00
|
|
|
@override
|
|
|
|
__InputNewPasswordDialogState createState() =>
|
|
|
|
__InputNewPasswordDialogState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class __InputNewPasswordDialogState extends State<_InputNewPasswordDialog> {
|
2021-02-18 17:58:47 +00:00
|
|
|
final _controller1 = GlobalKey<NewPasswordInputWidgetState>();
|
2020-04-30 16:19:01 +00:00
|
|
|
final _controller2 = TextEditingController();
|
|
|
|
final _focusScopeNode = FocusScopeNode();
|
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
String get _password => _controller1.currentState!.value;
|
2020-04-30 16:19:01 +00:00
|
|
|
|
2021-02-18 17:58:47 +00:00
|
|
|
bool get _input1Valid =>
|
2022-03-10 18:39:57 +00:00
|
|
|
_controller1.currentState != null && _controller1.currentState!.valid;
|
2020-04-30 16:19:01 +00:00
|
|
|
|
2022-03-11 16:09:37 +00:00
|
|
|
bool get _input2Valid =>
|
|
|
|
_controller1.currentState!.value == _controller2.text;
|
2020-04-30 16:19:01 +00:00
|
|
|
|
|
|
|
bool get _isValid => _input1Valid && _input2Valid;
|
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
void Function()? get _submitAction =>
|
2020-04-30 16:19:01 +00:00
|
|
|
_isValid ? () => Navigator.of(context).pop(_password) : null;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_focusScopeNode.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return AlertDialog(
|
2022-03-10 18:39:57 +00:00
|
|
|
title: Text(tr("New password")!),
|
2020-04-30 16:19:01 +00:00
|
|
|
content: AutoSizeDialogContentWidget(child: _buildContent()),
|
|
|
|
actions: <Widget>[
|
|
|
|
MaterialButton(
|
|
|
|
onPressed: () => Navigator.of(context).pop(),
|
2022-03-10 18:39:57 +00:00
|
|
|
child: Text(tr("Cancel")!.toUpperCase()),
|
2020-04-30 16:19:01 +00:00
|
|
|
),
|
|
|
|
MaterialButton(
|
|
|
|
onPressed: _submitAction,
|
2022-03-10 18:39:57 +00:00
|
|
|
child: Text(tr("Confirm")!.toUpperCase()),
|
2020-04-30 16:19:01 +00:00
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildContent() {
|
|
|
|
return FocusScope(
|
|
|
|
node: _focusScopeNode,
|
|
|
|
child: Column(
|
|
|
|
children: <Widget>[
|
|
|
|
// Input 1
|
2021-02-18 17:58:47 +00:00
|
|
|
NewPasswordInputWidget(
|
|
|
|
key: _controller1,
|
|
|
|
user: widget.userInfo,
|
2022-03-10 18:39:57 +00:00
|
|
|
label: tr("Your new password")!,
|
2020-04-30 16:19:01 +00:00
|
|
|
textInputAction: TextInputAction.next,
|
|
|
|
onSubmitted: () => _focusScopeNode.nextFocus(),
|
|
|
|
),
|
|
|
|
|
|
|
|
// Input 2
|
|
|
|
_buildPasswordField(
|
|
|
|
controller: _controller2,
|
|
|
|
label: tr("Confirm you new password"),
|
|
|
|
errorText: _controller2.text.isNotEmpty && !_input2Valid
|
|
|
|
? tr("This password is not the same as the other one!")
|
|
|
|
: null,
|
|
|
|
textInputAction: TextInputAction.done,
|
|
|
|
onSubmitted: _submitAction),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildPasswordField({
|
2022-03-10 18:39:57 +00:00
|
|
|
required TextEditingController controller,
|
|
|
|
required String? label,
|
|
|
|
required String? errorText,
|
|
|
|
required TextInputAction textInputAction,
|
|
|
|
required void Function()? onSubmitted,
|
2020-04-30 16:19:01 +00:00
|
|
|
}) {
|
|
|
|
return TextFormField(
|
|
|
|
textInputAction: textInputAction,
|
|
|
|
controller: controller,
|
|
|
|
onChanged: (s) => setState(() {}),
|
2022-03-10 18:39:57 +00:00
|
|
|
onFieldSubmitted: (s) => onSubmitted!(),
|
2020-04-30 16:19:01 +00:00
|
|
|
obscureText: true,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
alignLabelWithHint: true,
|
|
|
|
labelText: label,
|
|
|
|
errorText: errorText,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|