mirror of
https://gitlab.com/comunic/comunicmobile
synced 2025-06-19 00:05:16 +00:00
Can ask user his password
This commit is contained in:
85
lib/ui/dialogs/input_user_password_dialog.dart
Normal file
85
lib/ui/dialogs/input_user_password_dialog.dart
Normal file
@ -0,0 +1,85 @@
|
||||
import 'package:comunic/helpers/settings_helper.dart';
|
||||
import 'package:comunic/ui/widgets/safe_state.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/material.dart';
|
||||
|
||||
/// Ask the user to enter his password
|
||||
///
|
||||
/// @author Pierre Hubert
|
||||
|
||||
enum _Status { NONE, CHECKING, ERROR }
|
||||
|
||||
Future<String> showUserPasswordDialog(BuildContext context) async {
|
||||
return await showDialog(
|
||||
context: context, builder: (c) => _InputUserPasswordDialog());
|
||||
}
|
||||
|
||||
class _InputUserPasswordDialog extends StatefulWidget {
|
||||
@override
|
||||
__InputUserPasswordDialogState createState() =>
|
||||
__InputUserPasswordDialogState();
|
||||
}
|
||||
|
||||
class __InputUserPasswordDialogState
|
||||
extends SafeState<_InputUserPasswordDialog> {
|
||||
final _controller = TextEditingController();
|
||||
var _status = _Status.NONE;
|
||||
|
||||
String get _currPass => _controller.text;
|
||||
|
||||
bool get _canSubmit =>
|
||||
validatePassword(_controller.text) && _status != _Status.CHECKING;
|
||||
|
||||
void _setStatus(_Status s) => setState(() => _status = s);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: Text(tr("Password required")),
|
||||
content: _buildContent(),
|
||||
actions: <Widget>[
|
||||
MaterialButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
child: Text(tr("Cancel").toUpperCase()),
|
||||
),
|
||||
MaterialButton(
|
||||
onPressed: _canSubmit ? _checkPassword : null,
|
||||
child: Text(tr("Submit").toUpperCase()),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildContent() {
|
||||
if (_status == _Status.CHECKING)
|
||||
return IntrinsicHeight(
|
||||
child: buildCenteredProgressBar(),
|
||||
);
|
||||
|
||||
return TextField(
|
||||
controller: _controller,
|
||||
obscureText: true,
|
||||
onChanged: (s) => _setStatus(_Status.NONE),
|
||||
decoration: InputDecoration(
|
||||
alignLabelWithHint: true,
|
||||
labelText: tr("Your current password"),
|
||||
errorText: _status == _Status.ERROR ? tr("Invalid password!") : null,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _checkPassword() async {
|
||||
try {
|
||||
_setStatus(_Status.CHECKING);
|
||||
|
||||
await SettingsHelper.checkUserPassword(_currPass);
|
||||
|
||||
Navigator.of(context).pop(_currPass);
|
||||
} catch (e, stack) {
|
||||
print("Could not validate user pasword! $e\n$stack");
|
||||
_setStatus(_Status.ERROR);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
import 'package:comunic/ui/dialogs/input_user_password_dialog.dart';
|
||||
import 'package:comunic/utils/intl_utils.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:settings_ui/settings_ui.dart';
|
||||
|
||||
/// Account security settings
|
||||
///
|
||||
@ -12,6 +14,38 @@ class AccountSecuritySettingsScreen extends StatelessWidget {
|
||||
appBar: AppBar(
|
||||
title: Text(tr("Security settings")),
|
||||
),
|
||||
body: _AccountSecuritySettingsScreenBody(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _AccountSecuritySettingsScreenBody extends StatefulWidget {
|
||||
@override
|
||||
__AccountSecuritySettingsScreenBodyState createState() =>
|
||||
__AccountSecuritySettingsScreenBodyState();
|
||||
}
|
||||
|
||||
class __AccountSecuritySettingsScreenBodyState
|
||||
extends State<_AccountSecuritySettingsScreenBody> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SettingsList(
|
||||
sections: [
|
||||
SettingsSection(
|
||||
title: tr("Password"),
|
||||
tiles: [
|
||||
SettingsTile(
|
||||
title: tr("Change password"),
|
||||
onTap: _changePassword,
|
||||
),
|
||||
],
|
||||
)
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// Change current user password
|
||||
void _changePassword() async {
|
||||
final currPassword = await showUserPasswordDialog(context);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user