rustdesk/flutter/lib/mobile/widgets/dialog.dart

286 lines
8.9 KiB
Dart
Raw Normal View History

import 'dart:async';
2022-03-07 22:54:34 +08:00
import 'package:flutter/material.dart';
2022-05-24 23:33:00 +08:00
import '../../common.dart';
import '../../models/model.dart';
import '../../models/platform_model.dart';
2022-03-07 22:54:34 +08:00
2022-08-12 18:42:02 +08:00
void clientClose(OverlayDialogManager dialogManager) {
msgBox('', 'Close', 'Are you sure to close the connection?', dialogManager);
2022-03-07 22:54:34 +08:00
}
void showSuccess() {
showToast(translate("Successful"));
2022-03-30 23:09:19 +08:00
}
void showError() {
showToast(translate("Error"));
2022-03-30 23:09:19 +08:00
}
void showRestartRemoteDevice(
PeerInfo pi, String id, OverlayDialogManager dialogManager) async {
final res =
await dialogManager.show<bool>((setState, close) => CustomAlertDialog(
title: Row(children: [
Icon(Icons.warning_amber_sharp,
color: Colors.redAccent, size: 28),
SizedBox(width: 10),
Text(translate("Restart Remote Device")),
]),
content: Text(
"${translate('Are you sure you want to restart')} \n${pi.username}@${pi.hostname}($id) ?"),
actions: [
TextButton(
onPressed: () => close(), child: Text(translate("Cancel"))),
ElevatedButton(
onPressed: () => close(true), child: Text(translate("OK"))),
],
));
if (res == true) bind.sessionRestartRemoteDevice(id: id);
}
2022-08-12 18:42:02 +08:00
void setPermanentPasswordDialog(OverlayDialogManager dialogManager) async {
final pw = await bind.mainGetPermanentPassword();
2022-07-29 18:34:25 +08:00
final p0 = TextEditingController(text: pw);
final p1 = TextEditingController(text: pw);
2022-03-30 23:09:19 +08:00
var validateLength = false;
var validateSame = false;
2022-08-12 18:42:02 +08:00
dialogManager.show((setState, close) {
2022-03-30 23:09:19 +08:00
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();
2022-08-12 18:42:02 +08:00
dialogManager.showLoading(translate("Waiting"));
if (await gFFI.serverModel.setPermanentPassword(p0.text)) {
dialogManager.dismissAll();
showSuccess();
} else {
dialogManager.dismissAll();
showError();
}
}
2022-03-30 23:09:19 +08:00
: null,
child: Text(translate('OK')),
),
],
);
});
}
2022-08-12 18:42:02 +08:00
void setTemporaryPasswordLengthDialog(
OverlayDialogManager dialogManager) async {
2022-07-29 18:34:25 +08:00
List<String> lengths = ['6', '8', '10'];
String length = await bind.mainGetOption(key: "temporary-password-length");
2022-07-29 18:34:25 +08:00
var index = lengths.indexOf(length);
if (index < 0) index = 0;
length = lengths[index];
2022-08-12 18:42:02 +08:00
dialogManager.show((setState, close) {
2022-07-29 18:34:25 +08:00
final setLength = (newValue) {
final oldValue = length;
if (oldValue == newValue) return;
setState(() {
length = newValue;
});
bind.mainSetOption(key: "temporary-password-length", value: newValue);
bind.mainUpdateTemporaryPassword();
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-08-12 18:42:02 +08:00
void enterPasswordDialog(String id, OverlayDialogManager dialogManager) async {
2022-03-07 22:54:34 +08:00
final controller = TextEditingController();
2022-08-16 15:22:57 +08:00
var remember = await bind.sessionGetRemember(id: id) ?? false;
2022-08-12 18:42:02 +08:00
dialogManager.dismissAll();
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();
2022-08-12 18:42:02 +08:00
backToHomePage();
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;
gFFI.login(id, text, remember);
2022-03-13 23:07:52 +08:00
close();
2022-08-12 18:42:02 +08:00
dialogManager.showLoading(translate('Logging in...'),
onCancel: backToHomePage);
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
],
);
});
}
2022-08-12 18:42:02 +08:00
void wrongPasswordDialog(String id, OverlayDialogManager dialogManager) {
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();
2022-08-12 18:42:02 +08:00
backToHomePage();
2022-03-13 00:32:44 +08:00
},
child: Text(translate('Cancel')),
),
TextButton(
style: flatButtonStyle,
onPressed: () {
2022-08-12 18:42:02 +08:00
enterPasswordDialog(id, dialogManager);
2022-03-13 00:32:44 +08:00
},
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(
2022-08-12 18:42:02 +08:00
labelText: translate('Password'),
hintText: translate('Enter your password'),
2022-03-13 00:32:44 +08:00
// 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;
});
},
),
),
);
}
}