refactor dialog

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou
2023-03-24 15:21:14 +08:00
parent 308d336b20
commit 8ebfd3f628
42 changed files with 671 additions and 476 deletions

View File

@@ -1,10 +1,18 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import '../../common.dart';
import '../../models/model.dart';
import '../../models/platform_model.dart';
void clientClose(String id, OverlayDialogManager dialogManager) {
msgBox(id, 'info', 'Close', 'Are you sure to close the connection?', '',
dialogManager);
}
abstract class ValidationRule {
String get name;
bool validate(String value);
@@ -290,3 +298,541 @@ Future<String> changeDirectAccessPort(
});
return controller.text;
}
class DialogTextField extends StatelessWidget {
final String title;
final String? hintText;
final bool obscureText;
final String? errorText;
final String? helperText;
final Widget? prefixIcon;
final Widget? suffixIcon;
final TextEditingController controller;
final FocusNode? focusNode;
static const kUsernameTitle = 'Username';
static const kUsernameIcon = Icon(Icons.account_circle_outlined);
static const kPasswordTitle = 'Password';
static const kPasswordIcon = Icon(Icons.lock_outline);
DialogTextField(
{Key? key,
this.focusNode,
this.obscureText = false,
this.errorText,
this.helperText,
this.prefixIcon,
this.suffixIcon,
this.hintText,
required this.title,
required this.controller})
: super(key: key);
@override
Widget build(BuildContext context) {
return Row(
children: [
Expanded(
child: TextField(
decoration: InputDecoration(
labelText: title,
hintText: hintText,
prefixIcon: prefixIcon,
suffixIcon: suffixIcon,
helperText: helperText,
helperMaxLines: 8,
errorText: errorText,
),
controller: controller,
focusNode: focusNode,
autofocus: true,
obscureText: obscureText,
),
),
],
).paddingSymmetric(vertical: 4.0);
}
}
class PasswordWidget extends StatefulWidget {
PasswordWidget({
Key? key,
required this.controller,
this.autoFocus = true,
this.hintText,
this.errorText,
}) : super(key: key);
final TextEditingController controller;
final bool autoFocus;
final String? hintText;
final String? errorText;
@override
State<PasswordWidget> createState() => _PasswordWidgetState();
}
class _PasswordWidgetState extends State<PasswordWidget> {
bool _passwordVisible = false;
final _focusNode = FocusNode();
Timer? _timer;
@override
void initState() {
super.initState();
if (widget.autoFocus) {
_timer =
Timer(Duration(milliseconds: 50), () => _focusNode.requestFocus());
}
}
@override
void dispose() {
_timer?.cancel();
_focusNode.unfocus();
_focusNode.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return DialogTextField(
title: translate(DialogTextField.kPasswordTitle),
hintText: translate(widget.hintText ?? 'Enter your password'),
controller: widget.controller,
prefixIcon: DialogTextField.kPasswordIcon,
suffixIcon: IconButton(
icon: Icon(
// Based on passwordVisible state choose the icon
_passwordVisible ? Icons.visibility : Icons.visibility_off,
color: MyTheme.lightTheme.primaryColor),
onPressed: () {
// Update the state i.e. toggle the state of passwordVisible variable
setState(() {
_passwordVisible = !_passwordVisible;
});
},
),
obscureText: !_passwordVisible,
errorText: widget.errorText,
focusNode: _focusNode,
);
}
}
void wrongPasswordDialog(
String id, OverlayDialogManager dialogManager, type, title, text) {
dialogManager.dismissAll();
dialogManager.show((setState, close) {
cancel() {
close();
closeConnection();
}
submit() {
enterPasswordDialog(id, dialogManager);
}
return CustomAlertDialog(
title: null,
content: msgboxContent(type, title, text),
onSubmit: submit,
onCancel: cancel,
actions: [
dialogButton(
'Cancel',
onPressed: cancel,
isOutline: true,
),
dialogButton(
'Retry',
onPressed: submit,
),
]);
});
}
void enterPasswordDialog(String id, OverlayDialogManager dialogManager) async {
await _connectDialog(
id,
dialogManager,
peerPasswordController: TextEditingController(),
);
}
void enterUserLoginDialog(String id, OverlayDialogManager dialogManager) async {
await _connectDialog(
id,
dialogManager,
usernameController: TextEditingController(),
passwordController: TextEditingController(),
);
}
void enterUserLoginAndPasswordDialog(
String id, OverlayDialogManager dialogManager) async {
await _connectDialog(
id,
dialogManager,
usernameController: TextEditingController(),
passwordController: TextEditingController(),
peerPasswordController: TextEditingController(),
);
}
_connectDialog(
String id,
OverlayDialogManager dialogManager, {
TextEditingController? usernameController,
TextEditingController? passwordController,
TextEditingController? peerPasswordController,
}) async {
var remember = await bind.sessionGetRemember(id: id) ?? false;
dialogManager.dismissAll();
dialogManager.show((setState, close) {
cancel() {
close();
closeConnection();
}
submit() {
final username = usernameController?.text.trim() ?? '';
final password = passwordController?.text.trim() ?? '';
final peerPassword = peerPasswordController?.text.trim() ?? '';
if (peerPasswordController != null && peerPassword.isEmpty) return;
gFFI.login(
// username,
// password,
id,
peerPassword,
remember,
);
close();
dialogManager.showLoading(translate('Logging in...'),
onCancel: closeConnection);
}
return CustomAlertDialog(
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.password_rounded, color: MyTheme.accent),
Text(translate(usernameController == null
? 'Password Required'
: 'Login Required'))
.paddingOnly(left: 10),
],
),
content: Column(mainAxisSize: MainAxisSize.min, children: [
usernameController == null
? Offstage()
: DialogTextField(
title: translate(DialogTextField.kUsernameTitle),
controller: usernameController,
prefixIcon: DialogTextField.kUsernameIcon,
errorText: null,
),
passwordController == null
? Offstage()
: PasswordWidget(
controller: passwordController,
autoFocus: false,
),
peerPasswordController == null
? Offstage()
: PasswordWidget(
controller: peerPasswordController,
autoFocus: usernameController == null,
hintText: 'Enter RustDesk password',
),
peerPasswordController == null
? Offstage()
: CheckboxListTile(
contentPadding: const EdgeInsets.all(0),
dense: true,
controlAffinity: ListTileControlAffinity.leading,
title: Text(
translate('Remember RustDesk password'),
),
value: remember,
onChanged: (v) {
if (v != null) {
setState(() => remember = v);
}
},
),
]),
actions: [
dialogButton(
'Cancel',
icon: Icon(Icons.close_rounded),
onPressed: cancel,
isOutline: true,
),
dialogButton(
'OK',
icon: Icon(Icons.done_rounded),
onPressed: submit,
),
],
onSubmit: submit,
onCancel: cancel,
);
});
}
void showWaitUacDialog(
String id, OverlayDialogManager dialogManager, String type) {
dialogManager.dismissAll();
dialogManager.show(
tag: '$id-wait-uac',
(setState, close) => CustomAlertDialog(
title: null,
content: msgboxContent(type, 'Wait', 'wait_accept_uac_tip'),
));
}
// Another username && password dialog?
void showRequestElevationDialog(String id, OverlayDialogManager dialogManager) {
RxString groupValue = ''.obs;
RxString errUser = ''.obs;
RxString errPwd = ''.obs;
TextEditingController userController = TextEditingController();
TextEditingController pwdController = TextEditingController();
void onRadioChanged(String? value) {
if (value != null) {
groupValue.value = value;
}
}
const minTextStyle = TextStyle(fontSize: 14);
var content = Obx(() => Column(children: [
Row(
children: [
Radio(
value: '',
groupValue: groupValue.value,
onChanged: onRadioChanged),
Expanded(
child:
Text(translate('Ask the remote user for authentication'))),
],
),
Align(
alignment: Alignment.centerLeft,
child: Text(
translate(
'Choose this if the remote account is administrator'),
style: TextStyle(fontSize: 13))
.marginOnly(left: 40),
).marginOnly(bottom: 15),
Row(
children: [
Radio(
value: 'logon',
groupValue: groupValue.value,
onChanged: onRadioChanged),
Expanded(
child: Text(translate(
'Transmit the username and password of administrator')),
)
],
),
Row(
children: [
Expanded(
flex: 1,
child: Text(
'${translate('Username')}:',
style: minTextStyle,
).marginOnly(right: 10)),
Expanded(
flex: 3,
child: TextField(
controller: userController,
style: minTextStyle,
decoration: InputDecoration(
isDense: true,
contentPadding: EdgeInsets.symmetric(vertical: 15),
hintText: translate('eg: admin'),
errorText: errUser.isEmpty ? null : errUser.value),
onChanged: (s) {
if (s.isNotEmpty) {
errUser.value = '';
}
},
),
)
],
).marginOnly(left: 40),
Row(
children: [
Expanded(
flex: 1,
child: Text(
'${translate('Password')}:',
style: minTextStyle,
).marginOnly(right: 10)),
Expanded(
flex: 3,
child: TextField(
controller: pwdController,
obscureText: true,
style: minTextStyle,
decoration: InputDecoration(
isDense: true,
contentPadding: EdgeInsets.symmetric(vertical: 15),
errorText: errPwd.isEmpty ? null : errPwd.value),
onChanged: (s) {
if (s.isNotEmpty) {
errPwd.value = '';
}
},
),
),
],
).marginOnly(left: 40),
Align(
alignment: Alignment.centerLeft,
child: Text(translate('still_click_uac_tip'),
style: TextStyle(fontSize: 13, fontWeight: FontWeight.bold))
.marginOnly(top: 20)),
]));
dialogManager.dismissAll();
dialogManager.show(tag: '$id-request-elevation', (setState, close) {
void submit() {
if (groupValue.value == 'logon') {
if (userController.text.isEmpty) {
errUser.value = translate('Empty Username');
return;
}
if (pwdController.text.isEmpty) {
errPwd.value = translate('Empty Password');
return;
}
bind.sessionElevateWithLogon(
id: id,
username: userController.text,
password: pwdController.text);
} else {
bind.sessionElevateDirect(id: id);
}
}
return CustomAlertDialog(
title: Text(translate('Request Elevation')),
content: content,
actions: [
dialogButton('Cancel', onPressed: close, isOutline: true),
dialogButton('OK', onPressed: submit),
],
onSubmit: submit,
onCancel: close,
);
});
}
void showOnBlockDialog(
String id,
String type,
String title,
String text,
OverlayDialogManager dialogManager,
) {
if (dialogManager.existing('$id-wait-uac') ||
dialogManager.existing('$id-request-elevation')) {
return;
}
dialogManager.show(tag: '$id-$type', (setState, close) {
void submit() {
close();
showRequestElevationDialog(id, dialogManager);
}
return CustomAlertDialog(
title: null,
content: msgboxContent(type, title,
"${translate(text)}${type.contains('uac') ? '\n' : '\n\n'}${translate('request_elevation_tip')}"),
actions: [
dialogButton('Wait', onPressed: close, isOutline: true),
dialogButton('Request Elevation', onPressed: submit),
],
onSubmit: submit,
onCancel: close,
);
});
}
void showElevationError(String id, String type, String title, String text,
OverlayDialogManager dialogManager) {
dialogManager.show(tag: '$id-$type', (setState, close) {
void submit() {
close();
showRequestElevationDialog(id, dialogManager);
}
return CustomAlertDialog(
title: null,
content: msgboxContent(type, title, text),
actions: [
dialogButton('Cancel', onPressed: () {
close();
}, isOutline: true),
dialogButton('Retry', onPressed: submit),
],
onSubmit: submit,
onCancel: close,
);
});
}
void showWaitAcceptDialog(String id, String type, String title, String text,
OverlayDialogManager dialogManager) {
dialogManager.dismissAll();
dialogManager.show((setState, close) {
onCancel() {
closeConnection();
}
return CustomAlertDialog(
title: null,
content: msgboxContent(type, title, text),
actions: [
dialogButton('Cancel', onPressed: onCancel, isOutline: true),
],
onCancel: onCancel,
);
});
}
void showRestartRemoteDevice(
PeerInfo pi, String id, OverlayDialogManager dialogManager) async {
final res =
await dialogManager.show<bool>((setState, close) => CustomAlertDialog(
title: Row(children: [
Icon(Icons.warning_rounded, color: Colors.redAccent, size: 28),
Text(translate("Restart Remote Device")).paddingOnly(left: 10),
]),
content: Text(
"${translate('Are you sure you want to restart')} \n${pi.username}@${pi.hostname}($id) ?"),
actions: [
dialogButton(
"Cancel",
icon: Icon(Icons.close_rounded),
onPressed: close,
isOutline: true,
),
dialogButton(
"OK",
icon: Icon(Icons.done_rounded),
onPressed: () => close(true),
),
],
onCancel: close,
onSubmit: () => close(true),
));
if (res == true) bind.sessionRestartRemoteDevice(id: id);
}