rustdesk/flutter_hbb/lib/common.dart
2020-11-20 16:37:48 +08:00

101 lines
2.8 KiB
Dart

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:tuple/tuple.dart';
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);
}
}
class MyTheme {
MyTheme._();
static const Color grayBg = Color(0xFFEEEEEE);
static const Color white = Color(0xFFFFFFFF);
static const Color accent = Color(0xFF0071FF);
static const Color accent50 = Color(0x770071FF);
static const Color canvasColor = Color(0xFF212121);
static const Color border = Color(0xFFCCCCCC);
}
// https://github.com/huangjianke/flutter_easyloading
void showLoading(String text) {
dismissLoading();
EasyLoading.show(status: text);
}
void dismissLoading() {
EasyLoading.dismiss();
}
void showSuccess(String text) {
dismissLoading();
EasyLoading.showSuccess(text);
}
bool _hasDialog = false;
typedef BuildAlertDailog = Tuple3<Widget, Widget, List<Widget>> Function(
void Function(void Function()));
// https://material.io/develop/flutter/components/dialogs
Future<T> showAlertDialog<T>(BuildContext context, BuildAlertDailog build,
[WillPopCallback onWillPop,
bool barrierDismissible = false,
double contentPadding = 20]) async {
dismissLoading();
if (_hasDialog) {
Navigator.pop(context);
}
_hasDialog = true;
var dialog = StatefulBuilder(builder: (context, setState) {
var widgets = build(setState);
if (onWillPop == null) onWillPop = () async => false;
return WillPopScope(
onWillPop: onWillPop,
child: AlertDialog(
title: widgets.item1,
contentPadding: EdgeInsets.all(contentPadding),
content: widgets.item2,
actions: widgets.item3,
));
});
var res = await showDialog<T>(
context: context,
barrierDismissible: barrierDismissible,
builder: (context) => dialog);
_hasDialog = false;
return res;
}
void msgbox(String type, String title, String text, BuildContext context,
[hasCancel = false]) {
showAlertDialog(
context,
(_) => Tuple3(Text(title), Text(text), [
hasCancel
? Spacer()
: FlatButton(
textColor: MyTheme.accent,
onPressed: () {
Navigator.pop(context);
},
child: Text('Cancel'),
),
FlatButton(
textColor: MyTheme.accent,
onPressed: () {
Navigator.pop(context);
Navigator.pop(context);
},
child: Text('OK'),
),
]));
}