rustdesk/flutter/lib/widgets/dialog.dart

266 lines
7.9 KiB
Dart
Raw Normal View History

import 'dart:async';
2022-07-29 18:34:25 +08:00
import 'dart:convert';
2022-03-07 22:54:34 +08:00
import 'package:flutter/material.dart';
2022-04-19 13:07:45 +08:00
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
2022-03-07 22:54:34 +08:00
import '../common.dart';
import '../models/model.dart';
void clientClose() {
msgBox('', 'Close', 'Are you sure to close the connection?');
}
2022-03-30 23:09:19 +08:00
const SEC1 = Duration(seconds: 1);
2022-04-19 13:07:45 +08:00
void showSuccess({Duration duration = SEC1}) {
SmartDialog.dismiss();
showToast(translate("Successful"), duration: SEC1);
2022-03-30 23:09:19 +08:00
}
void showError({Duration duration = SEC1}) {
2022-04-19 13:07:45 +08:00
SmartDialog.dismiss();
showToast(translate("Error"), duration: SEC1);
2022-03-30 23:09:19 +08:00
}
2022-07-29 18:34:25 +08:00
void setPermanentPasswordDialog() {
final pw = FFI.getByName("permanent_password");
final p0 = TextEditingController(text: pw);
final p1 = TextEditingController(text: pw);
2022-03-30 23:09:19 +08:00
var validateLength = false;
var validateSame = false;
DialogManager.show((setState, close) {
return CustomAlertDialog(
title: Text(translate('Set your own password')),
content: Form(
autovalidateMode: AutovalidateMode.onUserInteraction,
child: Column(mainAxisSize: MainAxisSize.min, children: [
TextFormField(
autofocus: true,
obscureText: true,
keyboardType: TextInputType.visiblePassword,
decoration: InputDecoration(
labelText: translate('Password'),
),
controller: p0,
validator: (v) {
if (v == null) return null;
final val = v.trim().length > 5;
if (validateLength != val) {
// use delay to make setState success
Future.delayed(Duration(microseconds: 1),
() => setState(() => validateLength = val));
2022-03-30 23:09:19 +08:00
}
return val
? null
: translate('Too short, at least 6 characters.');
},
),
TextFormField(
obscureText: true,
keyboardType: TextInputType.visiblePassword,
decoration: InputDecoration(
labelText: translate('Confirmation'),
),
controller: p1,
validator: (v) {
if (v == null) return null;
final val = p0.text == v;
if (validateSame != val) {
Future.delayed(Duration(microseconds: 1),
() => setState(() => validateSame = val));
2022-03-30 23:09:19 +08:00
}
return val
? null
: translate('The confirmation is not identical.');
},
),
])),
actions: [
TextButton(
style: flatButtonStyle,
onPressed: () {
close();
},
child: Text(translate('Cancel')),
),
TextButton(
style: flatButtonStyle,
onPressed: (validateLength && validateSame)
? () async {
close();
showLoading(translate("Waiting"));
2022-07-29 18:34:25 +08:00
if (await FFI.serverModel.setPermanentPassword(p0.text)) {
showSuccess();
} else {
showError();
}
}
2022-03-30 23:09:19 +08:00
: null,
child: Text(translate('OK')),
),
],
);
});
}
2022-07-29 18:34:25 +08:00
void setTemporaryPasswordLengthDialog() {
List<String> lengths = ['6', '8', '10'];
String length = FFI.getByName('option', 'temporary-password-length');
var index = lengths.indexOf(length);
if (index < 0) index = 0;
length = lengths[index];
DialogManager.show((setState, close) {
final setLength = (newValue) {
final oldValue = length;
if (oldValue == newValue) return;
setState(() {
length = newValue;
});
Map<String, String> msg = Map()
..["name"] = "temporary-password-length"
..["value"] = newValue;
FFI.setByName("option", jsonEncode(msg));
FFI.setByName("temporary_password");
2022-07-29 22:07:45 +08:00
Future.delayed(Duration(milliseconds: 200), () {
close();
showSuccess();
});
2022-07-29 18:34:25 +08:00
};
return CustomAlertDialog(
title: Text(translate("Set temporary password length")),
content: Column(
mainAxisSize: MainAxisSize.min,
children:
lengths.map((e) => getRadio(e, e, length, setLength)).toList()),
actions: [],
contentPadding: 14,
);
}, backDismiss: true, clickMaskDismiss: true);
}
2022-03-07 22:54:34 +08:00
void enterPasswordDialog(String id) {
final controller = TextEditingController();
var remember = FFI.getByName('remember', id) == 'true';
2022-03-13 23:07:52 +08:00
DialogManager.show((setState, close) {
2022-03-13 00:32:44 +08:00
return CustomAlertDialog(
title: Text(translate('Password Required')),
content: Column(mainAxisSize: MainAxisSize.min, children: [
PasswordWidget(controller: controller),
CheckboxListTile(
contentPadding: const EdgeInsets.all(0),
dense: true,
controlAffinity: ListTileControlAffinity.leading,
title: Text(
translate('Remember password'),
),
value: remember,
onChanged: (v) {
if (v != null) {
setState(() => remember = v);
}
},
2022-03-07 22:54:34 +08:00
),
2022-03-13 00:32:44 +08:00
]),
actions: [
2022-03-07 22:54:34 +08:00
TextButton(
style: flatButtonStyle,
onPressed: () {
2022-03-13 23:07:52 +08:00
close();
backToHome();
2022-03-07 22:54:34 +08:00
},
child: Text(translate('Cancel')),
),
TextButton(
style: flatButtonStyle,
onPressed: () {
2022-03-13 00:32:44 +08:00
var text = controller.text.trim();
if (text == '') return;
FFI.login(text, remember);
2022-03-13 23:07:52 +08:00
close();
2022-03-13 00:32:44 +08:00
showLoading(translate('Logging in...'));
2022-03-07 22:54:34 +08:00
},
2022-03-13 00:32:44 +08:00
child: Text(translate('OK')),
2022-03-07 22:54:34 +08:00
),
2022-03-13 00:32:44 +08:00
],
);
});
}
void wrongPasswordDialog(String id) {
2022-03-13 23:07:52 +08:00
DialogManager.show((setState, close) => CustomAlertDialog(
2022-03-13 00:32:44 +08:00
title: Text(translate('Wrong Password')),
content: Text(translate('Do you want to enter again?')),
actions: [
TextButton(
style: flatButtonStyle,
onPressed: () {
2022-03-13 23:07:52 +08:00
close();
backToHome();
2022-03-13 00:32:44 +08:00
},
child: Text(translate('Cancel')),
),
TextButton(
style: flatButtonStyle,
onPressed: () {
enterPasswordDialog(id);
},
child: Text(translate('Retry')),
),
]));
}
class PasswordWidget extends StatefulWidget {
PasswordWidget({Key? key, required this.controller}) : super(key: key);
final TextEditingController controller;
@override
_PasswordWidgetState createState() => _PasswordWidgetState();
}
class _PasswordWidgetState extends State<PasswordWidget> {
bool _passwordVisible = false;
final _focusNode = FocusNode();
@override
void initState() {
super.initState();
Timer(Duration(milliseconds: 50), () => _focusNode.requestFocus());
}
@override
void dispose() {
_focusNode.unfocus();
_focusNode.dispose();
super.dispose();
}
2022-03-13 00:32:44 +08:00
@override
Widget build(BuildContext context) {
return TextField(
focusNode: _focusNode,
2022-03-13 00:32:44 +08:00
controller: widget.controller,
obscureText: !_passwordVisible,
//This will obscure text dynamically
keyboardType: TextInputType.visiblePassword,
decoration: InputDecoration(
labelText: Translator.call('Password'),
hintText: Translator.call('Enter your password'),
// Here is key idea
suffixIcon: IconButton(
icon: Icon(
// Based on passwordVisible state choose the icon
_passwordVisible ? Icons.visibility : Icons.visibility_off,
color: Theme.of(context).primaryColorDark,
),
onPressed: () {
// Update the state i.e. toogle the state of passwordVisible variable
setState(() {
_passwordVisible = !_passwordVisible;
});
},
),
),
);
}
}