Files
kitchen/lib/src/services/toast_service.dart

219 lines
5.6 KiB
Dart
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:mom_kitchen/src/standards/page_standards.dart';
enum ToastType { success, error, warning, info }
class ToastService {
static final ToastService _instance = ToastService._internal();
factory ToastService() => _instance;
ToastService._internal();
static final FToast _fToast = FToast();
static void init(BuildContext context) {
_fToast.init(context);
}
static Future<void> show({
required String message,
ToastType type = ToastType.info,
Duration duration = const Duration(seconds: 2),
ToastGravity gravity = ToastGravity.BOTTOM,
}) async {
final context = Get.context;
if (context == null) {
await _showNativeToast(message: message, type: type, gravity: gravity);
return;
}
final standards = PageStandards.of(context);
_showCustomToast(
message: message,
type: type,
duration: duration,
gravity: gravity,
standards: standards,
);
}
static Future<void> _showNativeToast({
required String message,
required ToastType type,
required ToastGravity gravity,
}) async {
Color backgroundColor;
switch (type) {
case ToastType.success:
backgroundColor = const Color(0xFF4CAF50);
break;
case ToastType.error:
backgroundColor = const Color(0xFFF44336);
break;
case ToastType.warning:
backgroundColor = const Color(0xFFFF9800);
break;
case ToastType.info:
backgroundColor = const Color(0xFF2196F3);
break;
}
await Fluttertoast.showToast(
msg: message,
toastLength: Toast.LENGTH_SHORT,
gravity: gravity,
backgroundColor: backgroundColor,
textColor: CupertinoColors.white,
fontSize: 16.0,
);
}
static void _showCustomToast({
required String message,
required ToastType type,
required Duration duration,
required ToastGravity gravity,
required PageStandards standards,
}) {
Color backgroundColor;
IconData icon;
String emoji;
switch (type) {
case ToastType.success:
backgroundColor = const Color(0xFF4CAF50);
icon = CupertinoIcons.checkmark_circle_fill;
emoji = '';
break;
case ToastType.error:
backgroundColor = const Color(0xFFF44336);
icon = CupertinoIcons.xmark_circle_fill;
emoji = '';
break;
case ToastType.warning:
backgroundColor = const Color(0xFFFF9800);
icon = CupertinoIcons.exclamationmark_triangle_fill;
emoji = '⚠️';
break;
case ToastType.info:
backgroundColor = standards.primaryColor;
icon = CupertinoIcons.info_circle_fill;
emoji = '';
break;
}
Widget toast = Container(
padding: standards.scaledPadding(
EdgeInsets.symmetric(horizontal: 20, vertical: 12),
),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(standards.scaledRadius(12)),
color: backgroundColor,
boxShadow: [
BoxShadow(
color: backgroundColor.withOpacity(0.3),
blurRadius: 10,
offset: const Offset(0, 4),
),
],
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Container(
padding: standards.scaledPadding(EdgeInsets.all(4)),
decoration: BoxDecoration(
color: CupertinoColors.white.withOpacity(0.2),
borderRadius: BorderRadius.circular(standards.scaledRadius(8)),
),
child: Icon(
icon,
color: CupertinoColors.white,
size: standards.scaledFontSize(20),
),
),
SizedBox(width: standards.scaledWidth(12)),
Flexible(
child: Text(
message,
style: standards.textStyle.copyWith(
color: CupertinoColors.white,
fontWeight: FontWeight.w500,
),
),
),
],
),
);
_fToast.showToast(
child: toast,
toastDuration: duration,
gravity: gravity,
fadeDuration: const Duration(milliseconds: 300),
);
}
static Future<void> success(
String message, {
Duration duration = const Duration(seconds: 2),
ToastGravity gravity = ToastGravity.BOTTOM,
}) async {
await show(
message: message,
type: ToastType.success,
duration: duration,
gravity: gravity,
);
}
static Future<void> error(
String message, {
Duration duration = const Duration(seconds: 2),
ToastGravity gravity = ToastGravity.BOTTOM,
}) async {
await show(
message: message,
type: ToastType.error,
duration: duration,
gravity: gravity,
);
}
static Future<void> warning(
String message, {
Duration duration = const Duration(seconds: 2),
ToastGravity gravity = ToastGravity.BOTTOM,
}) async {
await show(
message: message,
type: ToastType.warning,
duration: duration,
gravity: gravity,
);
}
static Future<void> info(
String message, {
Duration duration = const Duration(seconds: 2),
ToastGravity gravity = ToastGravity.BOTTOM,
}) async {
await show(
message: message,
type: ToastType.info,
duration: duration,
gravity: gravity,
);
}
static void removeCustomToast() {
_fToast.removeCustomToast();
}
static void removeQueuedCustomToasts() {
_fToast.removeQueuedCustomToasts();
}
}