TF-1594 Use only one class TmailToast to display the message.

(cherry picked from commit 379c34b32b2e5c4dad141e6a98bdb92597fa6908)
This commit is contained in:
dab246
2023-03-16 18:23:47 +07:00
committed by Dat Vu
parent fe05df3477
commit 4e17813e1a
34 changed files with 752 additions and 930 deletions
+122 -164
View File
@@ -1,79 +1,97 @@
import 'package:core/core.dart';
import 'package:core/presentation/extensions/color_extension.dart';
import 'package:core/presentation/resources/image_paths.dart';
import 'package:core/presentation/utils/responsive_utils.dart';
import 'package:core/presentation/views/toast/tmail_toast.dart';
import 'package:core/presentation/views/toast/toast_position.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:pointer_interceptor/pointer_interceptor.dart';
class AppToast {
final fToast = Get.find<FToast>();
void showToast(String message) {
Fluttertoast.showToast(
msg: message,
fontSize: 16,
textColor: Colors.white,
backgroundColor: AppColor.toastBackgroundColor,
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM);
}
void showSuccessToast(String message, {bool isToastLengthLong = false}) {
Fluttertoast.showToast(
msg: message,
fontSize: 16,
textColor: Colors.white,
backgroundColor: AppColor.toastSuccessBackgroundColor,
toastLength: isToastLengthLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM);
}
void showErrorToast(String message, {bool isToastLengthLong = false}) {
Fluttertoast.showToast(
msg: message,
fontSize: 16,
textColor: Colors.white,
backgroundColor: AppColor.toastErrorBackgroundColor,
toastLength: isToastLengthLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM);
}
void showBottomToast(
BuildContext context,
String message,
{
String? actionName,
Function? onActionClick,
Widget? actionIcon,
Widget? leadingIcon,
double? maxWidth,
bool infinityToast = false,
Color? backgroundColor,
Color? textColor,
Color? textActionColor,
}
void showToastErrorMessage(
BuildContext context,
String message,
{
Color? leadingSVGIconColor,
String? leadingSVGIcon
}
) {
Widget? trailingAction;
final imagePaths = Get.find<ImagePaths>();
showToastMessage(
context,
message,
backgroundColor: AppColor.toastErrorBackgroundColor,
textColor: Colors.white,
leadingSVGIconColor: leadingSVGIconColor ?? (leadingSVGIcon == null ? Colors.white : null),
leadingSVGIcon: leadingSVGIcon ?? imagePaths.icNotConnection
);
}
void showToastSuccessMessage(
BuildContext context,
String message,
{
Color? leadingSVGIconColor,
String? leadingSVGIcon,
}
) {
final imagePaths = Get.find<ImagePaths>();
showToastMessage(
context,
message,
backgroundColor: AppColor.toastSuccessBackgroundColor,
textColor: Colors.white,
leadingSVGIconColor: leadingSVGIconColor ?? (leadingSVGIcon == null ? Colors.white : null),
leadingSVGIcon: leadingSVGIcon ?? imagePaths.icToastSuccessMessage
);
}
void showToastMessage(
BuildContext context,
String message,
{
String? actionName,
Function? onActionClick,
Widget? actionIcon,
Widget? leadingIcon,
String? leadingSVGIcon,
Color? leadingSVGIconColor,
double? maxWidth,
bool infinityToast = false,
Color? backgroundColor,
Color? textColor,
Color? textActionColor,
TextStyle? textStyle,
EdgeInsets? padding,
}
) {
final responsiveUtils = Get.find<ResponsiveUtils>();
Widget? trailingWidget;
if (actionName != null) {
if (actionIcon == null) {
trailingAction = TextButton(
onPressed: () {
ToastView.dismiss();
onActionClick?.call();
},
child: Text(
actionName,
style: TextStyle(
trailingWidget = PointerInterceptor(
child: TextButton(
onPressed: () {
ToastView.dismiss();
onActionClick?.call();
},
child: Text(
actionName,
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.normal,
color: textActionColor ?? AppColor.buttonActionToastWithActionColor),
color: textActionColor ?? Colors.white
),
),
),
);
} else {
trailingAction = Material(
color: Colors.transparent,
child: InkWell(
trailingWidget = PointerInterceptor(
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: () {
ToastView.dismiss();
onActionClick?.call();
@@ -82,118 +100,58 @@ class AppToast {
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 6, horizontal: 8),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
actionIcon,
Text(
actionName,
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.normal,
color: textActionColor ?? Colors.white),
)
]
mainAxisSize: MainAxisSize.min,
children: [
actionIcon,
Text(
actionName,
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.normal,
color: textActionColor ?? Colors.white
),
)
]
),
)),
)
),
),
);
}
}
showToastMessage(
context,
message,
maxWidth: maxWidth,
backgroundColor: backgroundColor,
infinityToast: infinityToast,
textColor: textColor,
leading: leadingIcon,
trailing: trailingAction);
}
Widget? leadingWidget;
if (leadingIcon != null) {
leadingWidget = PointerInterceptor(child: leadingIcon);
} else {
if (leadingSVGIcon != null) {
leadingWidget = PointerInterceptor(
child: SvgPicture.asset(
leadingSVGIcon,
width: 24,
height: 24,
fit: BoxFit.fill,
colorFilter: leadingSVGIconColor?.asFilter(),
)
);
}
}
void showToastMessage(
BuildContext context,
String message, {
Color? textColor,
Widget? leading,
bool infinityToast = false,
Widget? trailing,
double? maxWidth,
Color? backgroundColor
}) {
TMailToast.showToast(
message,
context,
width: maxWidth,
toastPosition: ToastPosition.BOTTOM,
textStyle: TextStyle(
fontSize: 15,
fontWeight: FontWeight.normal,
color: textColor ?? Colors.white),
backgroundColor: backgroundColor ?? AppColor.toastWithActionBackgroundColor,
trailing: trailing != null
? Padding(
padding: const EdgeInsets.only(left: 8),
child: PointerInterceptor(child: trailing))
: null,
leading: leading != null
? Padding(
padding: const EdgeInsets.only(right: 8),
child: PointerInterceptor(child: leading))
: null,
toastBorderRadius: 10.0,
toastDuration: infinityToast ? null : 3,
);
}
void showToastWithIcon(
BuildContext context, {
String? message,
String? icon,
Color? bgColor,
Color? iconColor,
Color? textColor,
double? radius,
EdgeInsets? padding,
TextStyle? textStyle,
double? widthToast,
Duration? toastLength
}) async {
double sizeWidth = context.width > 360 ? 360 : context.width - 40;
final toast = Material(
color: bgColor ?? Colors.white,
elevation: 10,
shadowColor: Colors.black54,
borderRadius: BorderRadius.all(Radius.circular(radius ?? 10)),
child: Container(
padding: padding ?? const EdgeInsets.symmetric(horizontal: 12.0, vertical: 14),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(radius ?? 10.0),
color: bgColor ?? Colors.white,
),
width: widthToast ?? sizeWidth,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
if (icon != null)
SvgPicture.asset(icon,
width: 24,
height: 24,
fit: BoxFit.fill,
colorFilter: iconColor.asFilter()),
if (icon != null)
const SizedBox(width: 10.0),
Expanded(child: Text(
message ?? '',
style: textStyle ?? TextStyle(fontSize: 15, color: textColor ?? Colors.black))),
],
),
message,
context,
width: maxWidth ?? responsiveUtils.getMaxWidthToast(context),
toastPosition: ToastPosition.BOTTOM,
textStyle: textStyle ?? TextStyle(
fontSize: 15,
fontWeight: FontWeight.normal,
color: textColor ?? AppColor.primaryColor
),
);
fToast.init(context);
fToast.showToast(
child: toast,
gravity: ToastGravity.BOTTOM,
toastDuration: toastLength ?? const Duration(seconds: 3),
backgroundColor: backgroundColor ?? Colors.white,
trailing: trailingWidget,
leading: leadingWidget,
padding: padding,
toastDuration: infinityToast ? null : 3,
);
}
}
@@ -77,7 +77,7 @@ class ResponsiveUtils {
if (isPortraitMobile(context)) {
return widthScreen;
} else {
return widthScreen < 444 ? widthScreen : 444;
return widthScreen < 424 ? widthScreen : 424;
}
}