rustdesk/flutter_hbb/lib/common.dart

101 lines
2.8 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';
2020-11-17 18:10:49 +08:00
2020-11-15 20:04:05 +08:00
class HexColor extends Color {
HexColor(final String hexColor) : super(_getColorFromHex(hexColor));
static int _getColorFromHex(String hexColor) {
hexColor = hexColor.toUpperCase().replaceAll('#', '');
if (hexColor.length == 6) {
hexColor = 'FF' + hexColor;
}
return int.parse(hexColor, radix: 16);
}
}
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-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-16 21:21:27 +08:00
// https://github.com/huangjianke/flutter_easyloading
void showLoading(String text) {
2020-11-18 12:49:43 +08:00
dismissLoading();
2020-11-16 21:21:27 +08:00
EasyLoading.show(status: text);
}
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-16 22:12:32 +08:00
EasyLoading.showSuccess(text);
}
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-16 22:14:50 +08:00
// https://material.io/develop/flutter/components/dialogs
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-20 16:37:48 +08:00
void msgbox(String type, String title, String text, BuildContext context,
[hasCancel = false]) {
2020-11-19 21:59:49 +08:00
showAlertDialog(
context,
(_) => Tuple3(Text(title), Text(text), [
2020-11-20 16:37:48 +08:00
hasCancel
? Spacer()
: FlatButton(
textColor: MyTheme.accent,
onPressed: () {
Navigator.pop(context);
},
child: Text('Cancel'),
),
2020-11-19 21:59:49 +08:00
FlatButton(
textColor: MyTheme.accent,
onPressed: () {
Navigator.pop(context);
Navigator.pop(context);
},
child: Text('OK'),
),
]));
2020-11-18 01:55:26 +08:00
}