Commit 065f001c by Colton Grubbs Committed by GitHub

Added IgnorePointer and isDismissable features to context toast (#449)

* Add IgnorePointer Parameter

So users can click through toast

* Add isDismissable -> Dismiss toast on tap
parent 00a03044
...@@ -14,26 +14,13 @@ enum Toast { ...@@ -14,26 +14,13 @@ enum Toast {
/// ToastGravity /// ToastGravity
/// Used to define the position of the Toast on the screen /// Used to define the position of the Toast on the screen
enum ToastGravity { enum ToastGravity { TOP, BOTTOM, CENTER, TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT, CENTER_LEFT, CENTER_RIGHT, SNACKBAR, NONE }
TOP,
BOTTOM,
CENTER,
TOP_LEFT,
TOP_RIGHT,
BOTTOM_LEFT,
BOTTOM_RIGHT,
CENTER_LEFT,
CENTER_RIGHT,
SNACKBAR,
NONE
}
/// Plugin to show a toast message on screen /// Plugin to show a toast message on screen
/// Only for android, ios and Web platforms /// Only for android, ios and Web platforms
class Fluttertoast { class Fluttertoast {
/// [MethodChannel] used to communicate with the platform side. /// [MethodChannel] used to communicate with the platform side.
static const MethodChannel _channel = static const MethodChannel _channel = const MethodChannel('PonnamKarthik/fluttertoast');
const MethodChannel('PonnamKarthik/fluttertoast');
/// Let say you have an active show /// Let say you have an active show
/// Use this method to hide the toast immediately /// Use this method to hide the toast immediately
...@@ -103,8 +90,7 @@ class Fluttertoast { ...@@ -103,8 +90,7 @@ class Fluttertoast {
} }
/// Signature for a function to buildCustom Toast /// Signature for a function to buildCustom Toast
typedef PositionedToastBuilder = Widget Function( typedef PositionedToastBuilder = Widget Function(BuildContext context, Widget child);
BuildContext context, Widget child);
/// Runs on dart side this has no interaction with the Native Side /// Runs on dart side this has no interaction with the Native Side
/// Works with all platforms just in two lines of code /// Works with all platforms just in two lines of code
...@@ -235,10 +221,20 @@ class FToast { ...@@ -235,10 +221,20 @@ class FToast {
Duration toastDuration = const Duration(seconds: 2), Duration toastDuration = const Duration(seconds: 2),
ToastGravity? gravity, ToastGravity? gravity,
Duration fadeDuration = const Duration(milliseconds: 350), Duration fadeDuration = const Duration(milliseconds: 350),
bool ignorePointer = false,
bool isDismissable = false,
}) { }) {
if (context == null) if (context == null) throw ("Error: Context is null, Please call init(context) before showing toast.");
throw ("Error: Context is null, Please call init(context) before showing toast."); Widget newChild = _ToastStateFul(
Widget newChild = _ToastStateFul(child, toastDuration, fadeDuration); child,
toastDuration,
fadeDuration,
ignorePointer,
!isDismissable
? null
: () {
removeCustomToast();
});
/// Check for keyboard open /// Check for keyboard open
/// If open will ignore the gravity bottom and change it to center /// If open will ignore the gravity bottom and change it to center
...@@ -249,12 +245,10 @@ class FToast { ...@@ -249,12 +245,10 @@ class FToast {
} }
OverlayEntry newEntry = OverlayEntry(builder: (context) { OverlayEntry newEntry = OverlayEntry(builder: (context) {
if (positionedToastBuilder != null) if (positionedToastBuilder != null) return positionedToastBuilder(context, newChild);
return positionedToastBuilder(context, newChild);
return _getPostionWidgetBasedOnGravity(newChild, gravity); return _getPostionWidgetBasedOnGravity(newChild, gravity);
}); });
_overlayQueue.add(_ToastEntry( _overlayQueue.add(_ToastEntry(entry: newEntry, duration: toastDuration, fadeDuration: fadeDuration));
entry: newEntry, duration: toastDuration, fadeDuration: fadeDuration));
if (_timer == null) _showOverlay(); if (_timer == null) _showOverlay();
} }
...@@ -270,8 +264,7 @@ class FToast { ...@@ -270,8 +264,7 @@ class FToast {
case ToastGravity.TOP_RIGHT: case ToastGravity.TOP_RIGHT:
return Positioned(top: 100.0, right: 24.0, child: child); return Positioned(top: 100.0, right: 24.0, child: child);
case ToastGravity.CENTER: case ToastGravity.CENTER:
return Positioned( return Positioned(top: 50.0, bottom: 50.0, left: 24.0, right: 24.0, child: child);
top: 50.0, bottom: 50.0, left: 24.0, right: 24.0, child: child);
case ToastGravity.CENTER_LEFT: case ToastGravity.CENTER_LEFT:
return Positioned(top: 50.0, bottom: 50.0, left: 24.0, child: child); return Positioned(top: 50.0, bottom: 50.0, left: 24.0, child: child);
case ToastGravity.CENTER_RIGHT: case ToastGravity.CENTER_RIGHT:
...@@ -281,11 +274,7 @@ class FToast { ...@@ -281,11 +274,7 @@ class FToast {
case ToastGravity.BOTTOM_RIGHT: case ToastGravity.BOTTOM_RIGHT:
return Positioned(bottom: 50.0, right: 24.0, child: child); return Positioned(bottom: 50.0, right: 24.0, child: child);
case ToastGravity.SNACKBAR: case ToastGravity.SNACKBAR:
return Positioned( return Positioned(bottom: MediaQuery.of(context!).viewInsets.bottom, left: 0, right: 0, child: child);
bottom: MediaQuery.of(context!).viewInsets.bottom,
left: 0,
right: 0,
child: child);
case ToastGravity.NONE: case ToastGravity.NONE:
return Positioned.fill(child: child); return Positioned.fill(child: child);
case ToastGravity.BOTTOM: case ToastGravity.BOTTOM:
...@@ -352,20 +341,20 @@ class _ToastEntry { ...@@ -352,20 +341,20 @@ class _ToastEntry {
/// internal [StatefulWidget] which handles the show and hide /// internal [StatefulWidget] which handles the show and hide
/// animations for [FToast] /// animations for [FToast]
class _ToastStateFul extends StatefulWidget { class _ToastStateFul extends StatefulWidget {
_ToastStateFul(this.child, this.duration, this.fadeDuration, {Key? key}) _ToastStateFul(this.child, this.duration, this.fadeDuration, this.ignorePointer, this.onDismiss, {Key? key}) : super(key: key);
: super(key: key);
final Widget child; final Widget child;
final Duration duration; final Duration duration;
final Duration fadeDuration; final Duration fadeDuration;
final bool ignorePointer;
final VoidCallback? onDismiss;
@override @override
ToastStateFulState createState() => ToastStateFulState(); ToastStateFulState createState() => ToastStateFulState();
} }
/// State for [_ToastStateFul] /// State for [_ToastStateFul]
class ToastStateFulState extends State<_ToastStateFul> class ToastStateFulState extends State<_ToastStateFul> with SingleTickerProviderStateMixin {
with SingleTickerProviderStateMixin {
/// Start the showing animations for the toast /// Start the showing animations for the toast
showIt() { showIt() {
_animationController!.forward(); _animationController!.forward();
...@@ -389,8 +378,7 @@ class ToastStateFulState extends State<_ToastStateFul> ...@@ -389,8 +378,7 @@ class ToastStateFulState extends State<_ToastStateFul>
vsync: this, vsync: this,
duration: widget.fadeDuration, duration: widget.fadeDuration,
); );
_fadeAnimation = _fadeAnimation = CurvedAnimation(parent: _animationController!, curve: Curves.easeIn);
CurvedAnimation(parent: _animationController!, curve: Curves.easeIn);
super.initState(); super.initState();
showIt(); showIt();
...@@ -415,12 +403,19 @@ class ToastStateFulState extends State<_ToastStateFul> ...@@ -415,12 +403,19 @@ class ToastStateFulState extends State<_ToastStateFul>
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return FadeTransition( return GestureDetector(
opacity: _fadeAnimation as Animation<double>, onTap: widget.onDismiss == null ? null : () => widget.onDismiss!(),
child: Center( behavior: HitTestBehavior.translucent,
child: Material( child: IgnorePointer(
color: Colors.transparent, ignoring: widget.ignorePointer,
child: widget.child, child: FadeTransition(
opacity: _fadeAnimation as Animation<double>,
child: Center(
child: Material(
color: Colors.transparent,
child: widget.child,
),
),
), ),
), ),
); );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment