rustdesk/lib/common.dart

197 lines
5.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';
2021-08-02 20:54:56 +08:00
2022-03-07 22:54:34 +08:00
final globalKey = GlobalKey<NavigatorState>();
var isAndroid = false;
var isIOS = false;
var isWeb = false;
var isDesktop = false;
var version = "";
2022-03-24 17:58:33 +08:00
int androidVersion = 0;
2021-08-02 20:54:56 +08:00
typedef F = String Function(String);
2022-02-10 02:07:53 +08:00
typedef FMethod = String Function(String, dynamic);
2021-08-02 20:54:56 +08:00
class Translator {
2022-02-17 15:22:14 +08:00
static late F call;
}
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._();
2022-02-02 17:25:56 +08:00
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);
2022-01-31 16:22:05 +08:00
static const Color idColor = Color(0xFF00B6F0);
static const Color darkGray = Color(0xFFB9BABC);
2020-11-16 01:13:26 +08:00
}
2021-08-02 20:54:56 +08:00
final ButtonStyle flatButtonStyle = TextButton.styleFrom(
minimumSize: Size(88, 36),
padding: EdgeInsets.symmetric(horizontal: 16.0),
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(2.0)),
),
);
2022-02-28 16:11:21 +08:00
void showLoading(String text) {
DialogManager.reset();
EasyLoading.dismiss();
2022-02-24 16:20:03 +08:00
EasyLoading.show(status: text, maskType: EasyLoadingMaskType.black);
2020-11-16 21:21:27 +08:00
}
2022-03-13 23:07:52 +08:00
backToHome() {
// use [popUntil()] to make sure pop action can't close the current MaterialApp context
Navigator.popUntil(globalKey.currentContext!, ModalRoute.withName("/"));
}
2022-03-13 00:32:44 +08:00
typedef DialogBuilder = CustomAlertDialog Function(
StateSetter setState, Function([dynamic]) close);
2022-03-13 00:32:44 +08:00
class DialogManager {
2022-02-28 16:11:21 +08:00
static BuildContext? _dialogContext;
2020-11-16 22:00:09 +08:00
static void reset([result]) {
2022-03-13 00:32:44 +08:00
if (_dialogContext != null) {
2022-03-24 21:23:22 +08:00
Navigator.pop(_dialogContext!, result);
2022-02-28 16:11:21 +08:00
}
_dialogContext = null;
}
2022-03-13 00:32:44 +08:00
static void register(BuildContext dialogContext) {
2022-02-28 16:11:21 +08:00
_dialogContext = dialogContext;
}
2022-03-13 00:32:44 +08:00
static void drop() {
_dialogContext = null;
}
2022-03-13 00:32:44 +08:00
static Future<T?> show<T>(DialogBuilder builder,
{bool barrierDismissible = false}) async {
if (globalKey.currentContext == null) return null;
EasyLoading.dismiss();
DialogManager.reset();
final res = await showDialog<T>(
context: globalKey.currentContext!,
barrierDismissible: barrierDismissible,
builder: (context) {
DialogManager.register(context);
2022-03-13 23:07:52 +08:00
return StatefulBuilder(
2022-03-24 21:23:22 +08:00
builder: (_, setState) => builder(setState, DialogManager.reset));
2022-03-13 00:32:44 +08:00
});
DialogManager.drop();
return res;
}
2022-02-28 16:11:21 +08:00
}
2022-02-02 17:25:56 +08:00
2022-03-13 00:32:44 +08:00
class CustomAlertDialog extends StatelessWidget {
CustomAlertDialog(
{required this.title,
required this.content,
required this.actions,
this.onWillPop,
this.contentPadding});
2020-11-18 12:49:43 +08:00
2022-03-13 00:32:44 +08:00
final Widget title;
final Widget content;
final List<Widget> actions;
final WillPopCallback? onWillPop;
final double? contentPadding;
@override
Widget build(BuildContext context) {
2020-11-20 00:29:59 +08:00
return WillPopScope(
2022-03-13 00:32:44 +08:00
onWillPop: onWillPop ?? () async => false,
2020-11-20 00:29:59 +08:00
child: AlertDialog(
2022-03-13 00:32:44 +08:00
title: title,
contentPadding: EdgeInsets.all(contentPadding ?? 20),
content: content,
actions: actions,
2020-11-20 00:29:59 +08:00
));
2022-03-13 00:32:44 +08:00
}
2020-11-16 22:00:09 +08:00
}
2020-11-16 22:12:32 +08:00
2022-02-28 16:11:21 +08:00
// EasyLoading
2022-03-13 00:32:44 +08:00
void msgBox(String type, String title, String text, {bool? hasCancel}) {
2020-11-29 01:36:10 +08:00
var wrap = (String text, void Function() onPressed) => ButtonTheme(
padding: EdgeInsets.symmetric(horizontal: 20, vertical: 10),
2022-02-02 17:25:56 +08:00
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
//limits the touch area to the button area
minWidth: 0,
//wraps child's width
2020-11-29 01:36:10 +08:00
height: 0,
2021-08-02 20:54:56 +08:00
child: TextButton(
style: flatButtonStyle,
2020-11-29 01:36:10 +08:00
onPressed: onPressed,
2021-08-02 20:54:56 +08:00
child: Text(Translator.call(text),
style: TextStyle(color: MyTheme.accent))));
2020-11-29 01:36:10 +08:00
2022-02-28 16:11:21 +08:00
EasyLoading.dismiss();
DialogManager.reset();
2020-11-29 01:36:10 +08:00
final buttons = [
Expanded(child: Container()),
2021-08-02 20:54:56 +08:00
wrap(Translator.call('OK'), () {
2022-02-28 16:11:21 +08:00
EasyLoading.dismiss();
2022-03-13 23:07:52 +08:00
backToHome();
2020-11-29 01:36:10 +08:00
})
];
if (hasCancel == null) {
hasCancel = type != 'error';
}
2020-11-29 01:36:10 +08:00
if (hasCancel) {
buttons.insert(
1,
2021-08-02 20:54:56 +08:00
wrap(Translator.call('Cancel'), () {
2022-02-28 16:11:21 +08:00
EasyLoading.dismiss();
2020-11-29 01:36:10 +08:00
}));
}
2022-02-17 15:22:14 +08:00
EasyLoading.show(
2022-03-13 00:32:44 +08:00
status: "",
maskType: EasyLoadingMaskType.black,
indicator: Container(
constraints: BoxConstraints(maxWidth: 300),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(Translator.call(title), style: TextStyle(fontSize: 21)),
SizedBox(height: 20),
Text(Translator.call(text), style: TextStyle(fontSize: 15)),
SizedBox(height: 20),
Row(
children: buttons,
)
],
)));
2020-11-21 14:40:28 +08:00
}
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);
}
2021-08-14 14:14:01 +08:00
hash = hash % 16777216;
return Color((hash & 0xFF7FFF) | (alpha << 24));
2020-11-25 18:33:09 +08:00
}
2022-02-02 17:25:56 +08:00
const K = 1024;
const M = K * K;
const G = M * K;
String readableFileSize(double size) {
if (size < K) {
return size.toString() + " B";
} else if (size < M) {
return (size / K).toStringAsFixed(2) + " KB";
} else if (size < G) {
return (size / M).toStringAsFixed(2) + " MB";
} else {
return (size / G).toStringAsFixed(2) + " GB";
}
}