wip: add notificator util.

This commit is contained in:
2021-12-14 17:54:07 +08:00
parent 1f542cb585
commit cbaf61f70b
23 changed files with 1830 additions and 73 deletions
+18
View File
@@ -0,0 +1,18 @@
export 'alert_web.dart' if (dart.library.io) 'alert_platform.dart';
import 'package:flutter_platform_alert/flutter_platform_alert.dart';
abstract class Alert {
Future<CustomButton> showAlert({
required String windowTitle,
required String text,
String? positiveButtonTitle,
});
Future<CustomButton> showConfirm({
required String windowTitle,
required String text,
String? positiveButtonTitle,
String? negativeButtonTitle,
});
}
+30
View File
@@ -0,0 +1,30 @@
import 'alert_interface.dart';
import 'package:flutter_platform_alert/flutter_platform_alert.dart';
class AlertImpl implements Alert {
@override
Future<CustomButton> showAlert({
required String windowTitle,
required String text,
String? positiveButtonTitle,
}) =>
FlutterPlatformAlert.showCustomAlert(
windowTitle: windowTitle,
text: text,
positiveButtonTitle: positiveButtonTitle,
);
@override
Future<CustomButton> showConfirm({
required String windowTitle,
required String text,
String? positiveButtonTitle,
String? negativeButtonTitle,
}) =>
FlutterPlatformAlert.showCustomAlert(
windowTitle: windowTitle,
text: text,
positiveButtonTitle: positiveButtonTitle,
negativeButtonTitle: negativeButtonTitle,
);
}
+30
View File
@@ -0,0 +1,30 @@
import 'package:flutter_platform_alert/flutter_platform_alert.dart';
import 'alert_interface.dart';
import 'dart:html' as html;
class AlertImpl implements Alert {
@override
Future<CustomButton> showAlert({
required String windowTitle,
required String text,
String? positiveButtonTitle,
}) {
html.window.alert([windowTitle, text].join('\n'));
return Future.value(CustomButton.positiveButton);
}
@override
Future<CustomButton> showConfirm({
required String windowTitle,
required String text,
String? positiveButtonTitle,
String? negativeButtonTitle,
}) {
var confirmed = html.window.confirm([windowTitle, text].join('\n'));
return Future.value(
confirmed ? CustomButton.positiveButton : CustomButton.negativeButton,
);
}
}
+11 -25
View File
@@ -6,6 +6,15 @@ import 'package:flutter/cupertino.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:uuid/uuid.dart';
extension EnumByName<T extends Enum> on Iterable<T> {
T? findByName(String name) {
for (var value in this) {
if (value.name == name) return value;
}
return null;
}
}
class H {
H._();
@@ -50,19 +59,7 @@ class H {
return preferredPanelsStatus..marketPanelOpened = false;
}
static String? getGestureName(Gesture? gesture) => const {
Gesture.swipe: 'swipe',
Gesture.tap: 'tap',
Gesture.pinch: 'pinch',
}[gesture];
static Gesture getGestureByName(String gestureName) =>
const {
'swipe': Gesture.swipe,
'tap': Gesture.tap,
'pinch': Gesture.pinch,
}[gestureName] ??
Gesture.swipe;
static Gesture getGestureByName(String gestureName) => Gesture.values.findByName(gestureName) ?? Gesture.swipe;
static String? getGestureDirectionName(GestureDirection? direction) => const {
GestureDirection.up: 'up',
@@ -85,19 +82,8 @@ class H {
}[directionName] ??
GestureDirection.none;
static String? getGestureTypeName(GestureType? type) => const {
GestureType.built_in: 'built_in',
GestureType.shortcut: 'shortcut',
GestureType.commandline: 'commandline',
}[type];
static GestureType getGestureTypeByName(String typeName) =>
const {
'built_in': GestureType.built_in,
'shortcut': GestureType.shortcut,
'commandline': GestureType.commandline,
}[typeName] ??
GestureType.built_in;
GestureType.values.findByName(typeName) ?? GestureType.built_in;
static Color? parseQtActiveColor(String? inp) {
if (inp == null) return null;
+127
View File
@@ -0,0 +1,127 @@
import 'package:cherry_toast/cherry_toast.dart';
import 'package:cherry_toast/resources/arrays.dart';
import 'package:flutter/material.dart';
import 'package:flutter_platform_alert/flutter_platform_alert.dart';
import 'alert_interface.dart';
class Notificator {
static Future<CustomButton> showAlert({
required String title,
required String description,
String? positiveButtonTitle,
}) {
return AlertImpl().showAlert(
windowTitle: title,
text: description,
positiveButtonTitle: positiveButtonTitle,
);
}
static Future<CustomButton> showConfirm({
required String title,
required String description,
String? positiveButtonTitle,
String? negativeButtonTitle,
}) {
return AlertImpl().showConfirm(
windowTitle: title,
text: description,
positiveButtonTitle: positiveButtonTitle,
negativeButtonTitle: negativeButtonTitle,
);
}
static _setToastIconBackgroundColor(CherryToast toast, bool isDarkMode) {
var hslColor = HSLColor.fromColor(toast.themeColor);
toast.themeColor = hslColor.withLightness((hslColor.lightness + (isDarkMode ? 0.4 : 0.1)).clamp(0, 1)).toColor();
}
static CherryToast info(
BuildContext context, {
required String title,
String? description,
}) {
var themeData = Theme.of(context);
var toast = CherryToast.info(
title: title,
description: description,
autoDismiss: true,
animationType: ANIMATION_TYPE.FROM_TOP,
animationDuration: Duration(milliseconds: 300),
toastDuration: Duration(seconds: 3),
backgroundColor: themeData.backgroundColor,
titleStyle: themeData.textTheme.bodyText1!.copyWith(fontWeight: FontWeight.bold),
descriptionStyle: themeData.textTheme.bodyText1!,
);
_setToastIconBackgroundColor(toast, themeData.brightness == Brightness.dark);
toast.show(context);
return toast;
}
static CherryToast warning(
BuildContext context, {
required String title,
String? description,
}) {
var themeData = Theme.of(context);
var toast = CherryToast.warning(
title: title,
description: description,
autoDismiss: true,
animationType: ANIMATION_TYPE.FROM_TOP,
animationDuration: Duration(milliseconds: 300),
toastDuration: Duration(seconds: 3),
backgroundColor: themeData.backgroundColor,
titleStyle: themeData.textTheme.bodyText1!.copyWith(fontWeight: FontWeight.bold),
descriptionStyle: themeData.textTheme.bodyText1!,
);
_setToastIconBackgroundColor(toast, themeData.brightness == Brightness.dark);
toast.show(context);
return toast;
}
static CherryToast error(
BuildContext context, {
required String title,
String? description,
}) {
var themeData = Theme.of(context);
var toast = CherryToast.error(
title: title,
description: description,
autoDismiss: true,
animationType: ANIMATION_TYPE.FROM_TOP,
animationDuration: Duration(milliseconds: 300),
toastDuration: Duration(seconds: 3),
backgroundColor: themeData.backgroundColor,
titleStyle: themeData.textTheme.bodyText1!.copyWith(fontWeight: FontWeight.bold),
descriptionStyle: themeData.textTheme.bodyText1!,
);
_setToastIconBackgroundColor(toast, themeData.brightness == Brightness.dark);
toast.show(context);
return toast;
}
static CherryToast success(
BuildContext context, {
required String title,
String? description,
}) {
var themeData = Theme.of(context);
var toast = CherryToast.success(
title: title,
description: description,
autoDismiss: true,
animationType: ANIMATION_TYPE.FROM_TOP,
animationDuration: Duration(milliseconds: 300),
toastDuration: Duration(seconds: 3),
backgroundColor: themeData.backgroundColor,
titleStyle: themeData.textTheme.bodyText1!.copyWith(fontWeight: FontWeight.bold),
descriptionStyle: themeData.textTheme.bodyText1!,
);
_setToastIconBackgroundColor(toast, themeData.brightness == Brightness.dark);
toast.show(context);
return toast;
}
}