rustdesk/flutter_hbb/lib/common.dart

234 lines
7.3 KiB
Dart
Raw Normal View History

2020-11-15 20:04:05 +08:00
import 'package:flutter/material.dart';
import 'dart:async';
2020-11-16 21:21:27 +08:00
import 'package:flutter_easyloading/flutter_easyloading.dart';
2020-11-19 21:59:49 +08:00
import 'package:tuple/tuple.dart';
import 'dart:io';
2021-04-25 12:49:03 +08:00
final bool isZh = Platform.localeName.startsWith('zh');
final langs = <String, Map<String, String>>{
'zh': <String, String>{
2021-04-25 00:23:36 +08:00
'Remote ID': '远程ID',
'ID/Relay Server': 'ID/中继服务器',
'About': '关于',
'Mute': '静音',
'ID Server': 'ID服务器',
'Relay Server': '中继服务器',
'Invalid IP': '无效IP',
'Invalid format': '无效格式',
'Cancel': '取消',
'Close': '关闭',
'Retry': '再试',
'OK': '确认',
'Password Required': '需要密码',
2021-04-25 12:47:20 +08:00
'Enter your password': '输入你的密码',
'Please enter your password': '请输入密码',
'Remember password': '记住密码',
'Wrong Password': '密码错误',
'Do you want to enter again?': '还想输入一次吗?',
'Connection Error': '连接错误',
'Error': '错误',
'Reset by the peer': '连接被对方关闭',
'Connecting...': '正在连接...',
'Connection in progress. Please wait.': '连接进行中,请稍等。',
'Please try 1 minute later': '一分钟后再试',
'Login Error': '登录错误',
'Successful': '成功',
'Custom Image Quality': '设置画面质量',
'Privacy mode': '隐私模式',
'Remove': '删除',
'Adjust Window': '调节窗口',
'Good image quality': '好画质',
'Balanced': '一般画质',
'Optimize reaction time': '优化反应时间',
'Custom': '自定义画质',
'Show remote cursor': '显示远程光标',
'Disable clipboard': '禁止剪贴板',
'Lock after session end': '断开后锁定远程电脑',
'Insert': '插入',
'Insert Lock': '锁定远程电脑',
'Refresh': '刷新画面',
2021-07-07 21:18:00 +08:00
'ID does not exist': 'ID不存在',
'Failed to connect to rendezvous server': '连接服务器失败',
'Remote desktop is offline': '远程电脑不在线',
'Key mismatch': 'Key不匹配',
'Timeout': '连接超时',
'Failed to connect to relay server': '无法连接到中继服务器',
'Failed to connect via rendezvous server': '无法通过服务器建立连接',
'Failed to make direct connection to remote desktop': '无法建立直接连接',
'OS Password': '操作系统密码',
2021-04-25 12:47:20 +08:00
'Password': '密码',
'Paste': '粘贴',
'Logging in...': '正在登录...',
'Are you sure to close the connection?': '是否确认关闭连接?',
2021-04-25 00:22:44 +08:00
'Waiting for image...': '等待画面传输...',
},
'en': <String, String>{}
};
String translate(name) {
final tmp = isZh ? langs['zh'] : langs['en'];
final v = tmp[name];
return v != null ? v : name;
}
2020-11-17 18:10:49 +08:00
2020-11-16 01:13:26 +08:00
class MyTheme {
2020-11-20 16:37:48 +08:00
MyTheme._();
2020-11-16 01:13:26 +08:00
static const Color grayBg = Color(0xFFEEEEEE);
static const Color white = Color(0xFFFFFFFF);
2020-11-16 22:00:09 +08:00
static const Color accent = Color(0xFF0071FF);
2020-11-19 17:22:42 +08:00
static const Color accent50 = Color(0x770071FF);
2020-11-27 02:14:27 +08:00
static const Color accent80 = Color(0xAA0071FF);
2020-11-19 18:41:37 +08:00
static const Color canvasColor = Color(0xFF212121);
2020-11-20 13:06:52 +08:00
static const Color border = Color(0xFFCCCCCC);
2020-11-16 01:13:26 +08:00
}
2020-11-26 21:41:25 +08:00
void showLoading(String text, BuildContext context) {
2020-11-26 22:48:15 +08:00
if (_hasDialog && context != null) {
2020-11-26 21:41:25 +08:00
Navigator.pop(context);
}
2020-11-18 12:49:43 +08:00
dismissLoading();
2020-11-29 02:07:26 +08:00
EasyLoading.show(status: text, maskType: EasyLoadingMaskType.black);
2020-11-16 21:21:27 +08:00
}
void dismissLoading() {
EasyLoading.dismiss();
}
2020-11-16 22:00:09 +08:00
2020-11-16 22:12:32 +08:00
void showSuccess(String text) {
2020-11-18 12:49:43 +08:00
dismissLoading();
2020-11-29 02:07:26 +08:00
EasyLoading.showSuccess(text, maskType: EasyLoadingMaskType.black);
2020-11-16 22:12:32 +08:00
}
2020-11-18 12:49:43 +08:00
bool _hasDialog = false;
2020-11-20 00:36:23 +08:00
typedef BuildAlertDailog = Tuple3<Widget, Widget, List<Widget>> Function(
void Function(void Function()));
2020-11-18 12:49:43 +08:00
2020-11-20 02:12:48 +08:00
Future<T> showAlertDialog<T>(BuildContext context, BuildAlertDailog build,
[WillPopCallback onWillPop,
2020-11-20 16:37:48 +08:00
bool barrierDismissible = false,
2020-11-20 02:12:48 +08:00
double contentPadding = 20]) async {
2020-11-18 01:55:26 +08:00
dismissLoading();
2020-11-18 12:49:43 +08:00
if (_hasDialog) {
Navigator.pop(context);
}
_hasDialog = true;
2020-11-18 16:11:19 +08:00
var dialog = StatefulBuilder(builder: (context, setState) {
2020-11-19 21:59:49 +08:00
var widgets = build(setState);
2020-11-20 00:34:17 +08:00
if (onWillPop == null) onWillPop = () async => false;
2020-11-20 00:29:59 +08:00
return WillPopScope(
2020-11-20 00:34:17 +08:00
onWillPop: onWillPop,
2020-11-20 00:29:59 +08:00
child: AlertDialog(
title: widgets.item1,
2020-11-20 02:12:48 +08:00
contentPadding: EdgeInsets.all(contentPadding),
2020-11-20 00:29:59 +08:00
content: widgets.item2,
actions: widgets.item3,
));
2020-11-18 16:11:19 +08:00
});
2020-11-20 02:12:48 +08:00
var res = await showDialog<T>(
2020-11-18 12:49:43 +08:00
context: context,
2020-11-20 02:12:48 +08:00
barrierDismissible: barrierDismissible,
2020-11-18 12:49:43 +08:00
builder: (context) => dialog);
_hasDialog = false;
2020-11-20 02:12:48 +08:00
return res;
2020-11-16 22:00:09 +08:00
}
2020-11-16 22:12:32 +08:00
2020-11-29 01:36:10 +08:00
void msgbox(String type, String title, String text, BuildContext context,
[bool hasCancel]) {
var wrap = (String text, void Function() onPressed) => ButtonTheme(
padding: EdgeInsets.symmetric(horizontal: 20, vertical: 10),
materialTapTargetSize: MaterialTapTargetSize
.shrinkWrap, //limits the touch area to the button area
minWidth: 0, //wraps child's width
height: 0,
child: FlatButton(
focusColor: MyTheme.accent,
onPressed: onPressed,
2021-04-25 12:47:20 +08:00
child:
Text(translate(text), style: TextStyle(color: MyTheme.accent))));
2020-11-29 01:36:10 +08:00
dismissLoading();
if (_hasDialog) {
Navigator.pop(context);
}
final buttons = [
Expanded(child: Container()),
wrap(translate('OK'), () {
2020-11-29 01:36:10 +08:00
dismissLoading();
Navigator.pop(context);
})
];
if (hasCancel == null) {
hasCancel = type != 'error';
}
2020-11-29 01:36:10 +08:00
if (hasCancel) {
buttons.insert(
1,
wrap(translate('Cancel'), () {
2020-11-29 01:36:10 +08:00
dismissLoading();
}));
}
2020-11-29 02:07:26 +08:00
EasyLoading.showWidget(
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(translate(title), style: TextStyle(fontSize: 21)),
2020-11-29 02:07:26 +08:00
SizedBox(height: 20),
Text(translate(text), style: TextStyle(fontSize: 15)),
2020-11-29 02:07:26 +08:00
SizedBox(height: 20),
Row(
children: buttons,
)
],
),
maskType: EasyLoadingMaskType.black);
2020-11-18 01:55:26 +08:00
}
2020-11-21 14:40:28 +08:00
class PasswordWidget extends StatefulWidget {
PasswordWidget({Key key, this.controller}) : super(key: key);
final TextEditingController controller;
@override
_PasswordWidgetState createState() => _PasswordWidgetState();
}
class _PasswordWidgetState extends State<PasswordWidget> {
bool _passwordVisible = false;
@override
Widget build(BuildContext context) {
2020-11-23 01:16:17 +08:00
return TextField(
2020-11-21 14:40:28 +08:00
autofocus: true,
controller: widget.controller,
obscureText: !_passwordVisible, //This will obscure text dynamically
2020-12-22 14:41:14 +08:00
keyboardType: TextInputType.visiblePassword,
2020-11-21 14:40:28 +08:00
decoration: InputDecoration(
2021-04-25 12:47:20 +08:00
labelText: translate('Password'),
hintText: translate('Enter your password'),
2020-11-21 14:40:28 +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;
});
},
),
),
);
}
}
2020-11-25 18:33:09 +08:00
Color str2color(String str, [alpha = 0xFF]) {
var hash = 160 << 16 + 114 << 8 + 91;
for (var i = 0; i < str.length; i += 1) {
hash = str.codeUnitAt(i) + ((hash << 5) - hash);
}
return Color((hash & 0xFFFFFF) | (alpha << 24));
}