TF-1594 Use only one class TmailToast to display the message.
(cherry picked from commit 379c34b32b2e5c4dad141e6a98bdb92597fa6908)
This commit is contained in:
@@ -18,13 +18,13 @@ class ButtonBuilder {
|
||||
bool? _isVertical;
|
||||
Key? _key;
|
||||
Color? _iconColor;
|
||||
Color? _colorButton;
|
||||
TextStyle? _textStyle;
|
||||
BoxDecoration? _decoration;
|
||||
Widget? _iconAction;
|
||||
double? _radiusSplash;
|
||||
double? _maxWidth;
|
||||
EdgeInsets? _padding;
|
||||
EdgeInsets? _margin;
|
||||
String? _tooltip;
|
||||
BoxConstraints? _constraints;
|
||||
|
||||
@@ -48,10 +48,6 @@ class ButtonBuilder {
|
||||
_iconColor = color;
|
||||
}
|
||||
|
||||
void colorButton(Color color) {
|
||||
_colorButton = color;
|
||||
}
|
||||
|
||||
void decoration(BoxDecoration decoration) {
|
||||
_decoration = decoration;
|
||||
}
|
||||
@@ -68,6 +64,10 @@ class ButtonBuilder {
|
||||
_padding = padding;
|
||||
}
|
||||
|
||||
void margin(EdgeInsets? margin) {
|
||||
_margin = margin;
|
||||
}
|
||||
|
||||
void textStyle(TextStyle style) {
|
||||
_textStyle = style;
|
||||
}
|
||||
@@ -100,38 +100,40 @@ class ButtonBuilder {
|
||||
}
|
||||
|
||||
Widget build() {
|
||||
return Material(
|
||||
color: Colors.transparent,
|
||||
child: InkWell(
|
||||
onTap: () => _onPressActionClick != null ? _onPressActionClick!.call() : null,
|
||||
onTapDown: (detail) {
|
||||
if (_onPressActionWithPositionClick != null && _context != null) {
|
||||
final screenSize = MediaQuery.of(_context!).size;
|
||||
final offset = detail.globalPosition;
|
||||
final position = RelativeRect.fromLTRB(
|
||||
offset.dx,
|
||||
offset.dy,
|
||||
screenSize.width - offset.dx,
|
||||
screenSize.height - offset.dy,
|
||||
);
|
||||
_onPressActionWithPositionClick?.call(position);
|
||||
}
|
||||
},
|
||||
customBorder: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(_radiusSplash ?? 20)),
|
||||
child: Tooltip(
|
||||
message: _tooltip ?? '',
|
||||
child: Container(
|
||||
key: _key,
|
||||
alignment: Alignment.center,
|
||||
color: _decoration == null ? _colorButton : null,
|
||||
decoration: _decoration,
|
||||
width: _maxWidth,
|
||||
constraints: _constraints,
|
||||
padding: _padding ?? EdgeInsets.zero,
|
||||
child: _buildBody()
|
||||
),
|
||||
)
|
||||
return Padding(
|
||||
padding: _margin ?? EdgeInsets.zero,
|
||||
child: Material(
|
||||
color: Colors.transparent,
|
||||
child: InkWell(
|
||||
onTap: _onPressActionClick,
|
||||
onTapDown: (detail) {
|
||||
if (_onPressActionWithPositionClick != null && _context != null) {
|
||||
final screenSize = MediaQuery.of(_context!).size;
|
||||
final offset = detail.globalPosition;
|
||||
final position = RelativeRect.fromLTRB(
|
||||
offset.dx,
|
||||
offset.dy,
|
||||
screenSize.width - offset.dx,
|
||||
screenSize.height - offset.dy,
|
||||
);
|
||||
_onPressActionWithPositionClick?.call(position);
|
||||
}
|
||||
},
|
||||
borderRadius: BorderRadius.circular(_radiusSplash ?? 20),
|
||||
child: Tooltip(
|
||||
message: _tooltip ?? '',
|
||||
child: Container(
|
||||
key: _key,
|
||||
alignment: Alignment.center,
|
||||
color: _decoration == null ? Colors.transparent : null,
|
||||
decoration: _decoration,
|
||||
width: _maxWidth,
|
||||
constraints: _constraints,
|
||||
padding: _padding ?? EdgeInsets.zero,
|
||||
child: _buildBody()
|
||||
),
|
||||
)
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -163,11 +165,12 @@ class ButtonBuilder {
|
||||
Widget _buildIcon() => Padding(
|
||||
padding: _paddingIcon ?? const EdgeInsets.all(10),
|
||||
child: SvgPicture.asset(
|
||||
_icon ?? '',
|
||||
width: _size ?? 24,
|
||||
height: _size ?? 24,
|
||||
fit: BoxFit.fill,
|
||||
colorFilter: _iconColor.asFilter()));
|
||||
_icon ?? '',
|
||||
width: _size ?? 24,
|
||||
height: _size ?? 24,
|
||||
fit: BoxFit.fill,
|
||||
colorFilter: _iconColor.asFilter()
|
||||
));
|
||||
|
||||
Widget _buildText() {
|
||||
return Text(
|
||||
@@ -176,8 +179,9 @@ class ButtonBuilder {
|
||||
softWrap: CommonTextStyle.defaultSoftWrap,
|
||||
overflow: CommonTextStyle.defaultTextOverFlow,
|
||||
style: _textStyle ?? const TextStyle(
|
||||
fontSize: 12,
|
||||
color: AppColor.colorTextButton),
|
||||
fontSize: 12,
|
||||
color: AppColor.colorTextButton
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:core/presentation/extensions/color_extension.dart';
|
||||
import 'package:core/presentation/views/toast/toast_position.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
@@ -34,52 +35,68 @@ class TMailToast {
|
||||
/// defines the size width widget of the toast
|
||||
double? width;
|
||||
|
||||
// ignore: type_annotate_public_apis, always_declare_return_types
|
||||
/// defines the padding widget of the toast
|
||||
EdgeInsets? padding;
|
||||
|
||||
static showToast(
|
||||
text,
|
||||
BuildContext context, {
|
||||
toastDuration,
|
||||
toastPosition,
|
||||
backgroundColor = const Color(0xAA000000),
|
||||
textStyle = const TextStyle(fontSize: 15, color: Colors.white),
|
||||
toastBorderRadius = 20.0,
|
||||
backgroundColor,
|
||||
textStyle = const TextStyle(
|
||||
fontSize: 15,
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.normal
|
||||
),
|
||||
toastBorderRadius = 10.0,
|
||||
border,
|
||||
trailing,
|
||||
leading,
|
||||
width,
|
||||
padding,
|
||||
}) {
|
||||
assert(text != null);
|
||||
ToastView.dismiss();
|
||||
ToastView.createView(text, context, toastPosition,
|
||||
backgroundColor, textStyle, toastBorderRadius, border, trailing,
|
||||
leading, width, toastDuration: toastDuration,);
|
||||
ToastView.createView(
|
||||
text,
|
||||
context,
|
||||
toastPosition,
|
||||
backgroundColor,
|
||||
textStyle,
|
||||
toastBorderRadius,
|
||||
border,
|
||||
trailing,
|
||||
leading,
|
||||
width,
|
||||
padding,
|
||||
toastDuration: toastDuration,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ToastView {
|
||||
static final ToastView _instance = ToastView._internal();
|
||||
// ignore: sort_constructors_first
|
||||
factory ToastView() => _instance;
|
||||
// ignore: sort_constructors_first
|
||||
ToastView._internal();
|
||||
|
||||
static OverlayState? overlayState;
|
||||
static OverlayEntry? _overlayEntry;
|
||||
static bool _isVisible = false;
|
||||
|
||||
// ignore: avoid_void_async
|
||||
static void createView(
|
||||
String text,
|
||||
BuildContext context,
|
||||
ToastPosition? toastPosition,
|
||||
Color backgroundColor,
|
||||
TextStyle textStyle,
|
||||
Color? backgroundColor,
|
||||
TextStyle? textStyle,
|
||||
double toastBorderRadius,
|
||||
Border? border,
|
||||
Widget? trailing,
|
||||
Widget? leading,
|
||||
double? width,
|
||||
{int? toastDuration = 2}
|
||||
EdgeInsets? padding,
|
||||
{int? toastDuration}
|
||||
) async {
|
||||
overlayState = Overlay.of(context, rootOverlay: false);
|
||||
|
||||
@@ -89,49 +106,72 @@ class ToastView {
|
||||
child: Container(
|
||||
width: width,
|
||||
decoration: BoxDecoration(
|
||||
color: backgroundColor,
|
||||
color: backgroundColor ?? Colors.white,
|
||||
borderRadius: BorderRadius.circular(toastBorderRadius),
|
||||
border: border,
|
||||
boxShadow: const [
|
||||
BoxShadow(
|
||||
color: AppColor.colorShadowLayerBottom,
|
||||
blurRadius: 24,
|
||||
offset: Offset.zero),
|
||||
BoxShadow(
|
||||
color: AppColor.colorShadowBgContentEmail,
|
||||
blurRadius: 2,
|
||||
offset: Offset.zero),
|
||||
]
|
||||
),
|
||||
margin: const EdgeInsets.symmetric(horizontal: 16),
|
||||
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 16),
|
||||
child: trailing == null && leading == null ?
|
||||
Text(text, softWrap: true, style: textStyle) :
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
if (leading != null) leading,
|
||||
Expanded(child: Text(text, style: textStyle)),
|
||||
if (trailing != null) trailing
|
||||
],
|
||||
),
|
||||
padding: padding ?? const EdgeInsets.all(12),
|
||||
child: trailing == null && leading == null
|
||||
? Text(
|
||||
text,
|
||||
softWrap: true,
|
||||
style: textStyle ?? const TextStyle(
|
||||
fontSize: 15,
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.normal
|
||||
)
|
||||
)
|
||||
: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
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) trailing
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
|
||||
_overlayEntry = OverlayEntry(
|
||||
builder: (BuildContext context) =>
|
||||
_showWidgetBasedOnPosition(
|
||||
toastDuration != null ?
|
||||
ToastCard(
|
||||
child,
|
||||
Duration(seconds: toastDuration),
|
||||
fadeDuration: 500,
|
||||
)
|
||||
: child, toastPosition,
|
||||
));
|
||||
_overlayEntry = OverlayEntry(builder: (context) =>
|
||||
_showWidgetBasedOnPosition(
|
||||
toastDuration != null
|
||||
? ToastCard(child, Duration(seconds: toastDuration))
|
||||
: child,
|
||||
toastPosition,
|
||||
)
|
||||
);
|
||||
|
||||
_isVisible = true;
|
||||
overlayState!.insert(_overlayEntry!);
|
||||
if (toastDuration != null) {
|
||||
await Future.delayed(Duration(seconds: toastDuration));
|
||||
await dismiss();
|
||||
}
|
||||
}
|
||||
|
||||
static Positioned _showWidgetBasedOnPosition(
|
||||
Widget child, ToastPosition? toastPosition) {
|
||||
Widget child,
|
||||
ToastPosition? toastPosition
|
||||
) {
|
||||
switch (toastPosition) {
|
||||
case ToastPosition.BOTTOM:
|
||||
return Positioned(bottom: 60, left: 18, right: 18, child: child);
|
||||
@@ -140,8 +180,7 @@ class ToastView {
|
||||
case ToastPosition.BOTTOM_RIGHT:
|
||||
return Positioned(bottom: 60, right: 18, child: child);
|
||||
case ToastPosition.CENTER:
|
||||
return Positioned(
|
||||
top: 60, bottom: 60, left: 18, right: 18, child: child);
|
||||
return Positioned(top: 60, bottom: 60, left: 18, right: 18, child: child);
|
||||
case ToastPosition.CENTER_LEFT:
|
||||
return Positioned(top: 60, bottom: 60, left: 18, child: child);
|
||||
case ToastPosition.CENTER_RIGHT:
|
||||
@@ -165,9 +204,14 @@ class ToastView {
|
||||
}
|
||||
|
||||
class ToastCard extends StatefulWidget {
|
||||
const ToastCard(this.child, this.duration,
|
||||
{Key? key, this.fadeDuration = 500})
|
||||
: super(key: key);
|
||||
const ToastCard(
|
||||
this.child,
|
||||
this.duration,
|
||||
{
|
||||
Key? key,
|
||||
this.fadeDuration = 300
|
||||
}
|
||||
) : super(key: key);
|
||||
|
||||
final Widget child;
|
||||
final Duration duration;
|
||||
@@ -199,8 +243,7 @@ class ToastStateFulState extends State<ToastCard>
|
||||
vsync: this,
|
||||
duration: Duration(milliseconds: widget.fadeDuration),
|
||||
);
|
||||
_fadeAnimation =
|
||||
CurvedAnimation(parent: _animationController!, curve: Curves.easeIn);
|
||||
_fadeAnimation = CurvedAnimation(parent: _animationController!, curve: Curves.easeIn);
|
||||
super.initState();
|
||||
|
||||
showAnimation();
|
||||
@@ -226,4 +269,4 @@ class ToastStateFulState extends State<ToastCard>
|
||||
opacity: _fadeAnimation as Animation<double>,
|
||||
child: widget.child,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user