TF-1915 Show toast message when connected to the internet

(cherry picked from commit a1f466725cf1896cfb43144d41c6806a074e06eb)
This commit is contained in:
dab246
2023-06-08 16:54:12 +07:00
committed by Dat Vu
parent 3100e00c9e
commit 9b5f533579
7 changed files with 117 additions and 87 deletions
@@ -189,6 +189,7 @@ class ImagePaths {
String get icMenuMailbox => _getImagePath('ic_menu_mailbox.svg');
String get icDelivering => _getImagePath('ic_delivering.svg');
String get icError => _getImagePath('ic_error.svg');
String get icConnectedInternet => _getImagePath('ic_connected_internet.svg');
String _getImagePath(String imageName) {
return AssetsPaths.images + imageName;
+16 -7
View File
@@ -15,7 +15,8 @@ class AppToast {
String message,
{
Color? leadingSVGIconColor,
String? leadingSVGIcon
String? leadingSVGIcon,
Duration? duration,
}
) {
final imagePaths = Get.find<ImagePaths>();
@@ -25,7 +26,8 @@ class AppToast {
backgroundColor: AppColor.toastErrorBackgroundColor,
textColor: Colors.white,
leadingSVGIconColor: leadingSVGIconColor ?? (leadingSVGIcon == null ? Colors.white : null),
leadingSVGIcon: leadingSVGIcon ?? imagePaths.icNotConnection
leadingSVGIcon: leadingSVGIcon ?? imagePaths.icNotConnection,
duration: duration
);
}
@@ -35,6 +37,7 @@ class AppToast {
{
Color? leadingSVGIconColor,
String? leadingSVGIcon,
Duration? duration,
}
) {
final imagePaths = Get.find<ImagePaths>();
@@ -44,7 +47,8 @@ class AppToast {
backgroundColor: AppColor.toastSuccessBackgroundColor,
textColor: Colors.white,
leadingSVGIconColor: leadingSVGIconColor ?? (leadingSVGIcon == null ? Colors.white : null),
leadingSVGIcon: leadingSVGIcon ?? imagePaths.icToastSuccessMessage
leadingSVGIcon: leadingSVGIcon ?? imagePaths.icToastSuccessMessage,
duration: duration
);
}
@@ -54,6 +58,7 @@ class AppToast {
{
Color? leadingSVGIconColor,
String? leadingSVGIcon,
Duration? duration,
}
) {
final imagePaths = Get.find<ImagePaths>();
@@ -63,7 +68,8 @@ class AppToast {
backgroundColor: AppColor.toastWarningBackgroundColor,
textColor: Colors.white,
leadingSVGIconColor: leadingSVGIconColor ?? (leadingSVGIcon == null ? Colors.white : null),
leadingSVGIcon: leadingSVGIcon ?? imagePaths.icInfoCircleOutline
leadingSVGIcon: leadingSVGIcon ?? imagePaths.icInfoCircleOutline,
duration: duration
);
}
@@ -84,7 +90,8 @@ class AppToast {
Color? textActionColor,
TextStyle? textStyle,
EdgeInsets? padding,
TextAlign? textAlign
TextAlign? textAlign,
Duration? duration,
}
) {
final responsiveUtils = Get.find<ResponsiveUtils>();
@@ -160,7 +167,7 @@ class AppToast {
TMailToast.showToast(
message,
context,
width: maxWidth ?? responsiveUtils.getMaxWidthToast(context),
maxWidth: maxWidth ?? responsiveUtils.getMaxWidthToast(context),
toastPosition: ToastPosition.BOTTOM,
textStyle: textStyle ?? TextStyle(
fontSize: 15,
@@ -172,7 +179,9 @@ class AppToast {
leading: leadingWidget,
padding: padding,
textAlign: textAlign,
toastDuration: infinityToast ? null : 3,
toastDuration: infinityToast
? null
: (duration ?? const Duration(seconds: 3)),
);
}
}
@@ -5,62 +5,25 @@ import 'package:core/presentation/views/toast/toast_position.dart';
import 'package:flutter/material.dart';
class TMailToast {
/// text of type [String] display on toast
String? text;
/// defines the duration of time toast display over screen
int? toastDuration;
/// defines the position of toast over the screen
ToastPosition? toastPosition;
/// defines the background color of the toast
Color? backgroundColor;
/// defines the test style of the toast text
TextStyle? textStyle;
/// defines the border radius of the toast
double? toastBorderRadius;
/// defines the border of the toast
Border? border;
/// defines the trailing widget of the toast
Widget? trailing;
/// defines the leading widget of the toast
Widget? leading;
/// defines the size width widget of the toast
double? width;
/// defines the padding widget of the toast
EdgeInsets? padding;
/// defines the align text of the toast
TextAlign? textAlign;
static showToast(
text,
BuildContext context, {
toastDuration,
toastPosition,
backgroundColor,
textStyle = const TextStyle(
fontSize: 15,
color: Colors.white,
fontWeight: FontWeight.normal
),
toastBorderRadius = 10.0,
border,
trailing,
leading,
width,
padding,
textAlign
}) {
assert(text != null);
String text,
BuildContext context, {
Duration? toastDuration,
ToastPosition? toastPosition,
Color? backgroundColor,
TextStyle textStyle = const TextStyle(
fontSize: 15,
color: Colors.white,
fontWeight: FontWeight.normal
),
double toastBorderRadius = 10.0,
Border? border,
Widget? trailing,
Widget? leading,
double maxWidth = 424,
EdgeInsets? padding,
TextAlign? textAlign
}) {
ToastView.dismiss();
ToastView.createView(
text,
@@ -72,7 +35,7 @@ class TMailToast {
border,
trailing,
leading,
width,
maxWidth,
padding,
textAlign,
toastDuration: toastDuration,
@@ -99,18 +62,17 @@ class ToastView {
Border? border,
Widget? trailing,
Widget? leading,
double? width,
double maxWidth,
EdgeInsets? padding,
TextAlign? textAlign,
{int? toastDuration}
{Duration? toastDuration}
) async {
overlayState = Overlay.of(context, rootOverlay: false);
final Widget child = Center(
child: Material(
color: Colors.transparent,
child: Container(
width: width,
constraints: BoxConstraints(maxWidth: maxWidth),
decoration: BoxDecoration(
color: backgroundColor ?? Colors.white,
borderRadius: BorderRadius.circular(toastBorderRadius),
@@ -126,8 +88,7 @@ class ToastView {
offset: Offset.zero),
]
),
margin: const EdgeInsets.symmetric(horizontal: 16),
padding: padding ?? const EdgeInsets.all(12),
padding: padding ?? const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
child: trailing == null && leading == null
? Text(
text,
@@ -140,21 +101,39 @@ class ToastView {
textAlign: textAlign
)
: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.min,
children: [
if (leading != null) leading,
Expanded(child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 12),
child: Text(
text,
style: textStyle ?? const TextStyle(
fontSize: 15,
color: Colors.white,
fontWeight: FontWeight.normal
if (trailing != null)
Expanded(child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 12),
child: Text(
text,
style: textStyle ?? const TextStyle(
fontSize: 15,
color: Colors.white,
fontWeight: FontWeight.normal
),
textAlign: textAlign
)
))
else
Container(
constraints: BoxConstraints(maxWidth: _getMaxWidthTitleByLeadingTrailingSize(
currentMaxWidth: maxWidth,
leading: leading
)),
padding: const EdgeInsets.symmetric(horizontal: 12),
child: Text(
text,
style: textStyle ?? const TextStyle(
fontSize: 15,
color: Colors.white,
fontWeight: FontWeight.normal
),
textAlign: textAlign
),
textAlign: textAlign
)
)),
),
if (trailing != null) trailing
],
),
@@ -166,7 +145,7 @@ class ToastView {
_overlayEntry = OverlayEntry(builder: (context) =>
_showWidgetBasedOnPosition(
toastDuration != null
? ToastCard(child, Duration(seconds: toastDuration))
? ToastCard(child, toastDuration)
: child,
toastPosition,
)
@@ -175,22 +154,29 @@ class ToastView {
_isVisible = true;
overlayState!.insert(_overlayEntry!);
if (toastDuration != null) {
await Future.delayed(Duration(seconds: toastDuration));
await Future.delayed(toastDuration);
await dismiss();
}
}
static double _getMaxWidthTitleByLeadingTrailingSize({
required double currentMaxWidth,
Widget? leading
}) {
return leading == null ? currentMaxWidth : currentMaxWidth - 90;
}
static Positioned _showWidgetBasedOnPosition(
Widget child,
ToastPosition? toastPosition
) {
switch (toastPosition) {
case ToastPosition.BOTTOM:
return Positioned(bottom: 60, left: 18, right: 18, child: child);
return Positioned(bottom: 80, left: 18, right: 18, child: child);
case ToastPosition.BOTTOM_LEFT:
return Positioned(bottom: 60, left: 18, child: child);
return Positioned(bottom: 80, left: 18, child: child);
case ToastPosition.BOTTOM_RIGHT:
return Positioned(bottom: 60, right: 18, child: child);
return Positioned(bottom: 80, right: 18, child: child);
case ToastPosition.CENTER:
return Positioned(top: 60, bottom: 60, left: 18, right: 18, child: child);
case ToastPosition.CENTER_LEFT: