showToast method

void showToast({
  1. required Widget child,
  2. PositionedToastBuilder? positionedToastBuilder,
  3. Duration toastDuration = const Duration(seconds: 2),
  4. ToastGravity? gravity,
})

showToast accepts all the required paramenters and prepares the child calls _showOverlay to display toast

Paramenter child is requried toastDuration default is 2 seconds fadeDuration default is 350 milliseconds

Implementation

void showToast({
  required Widget child,
  PositionedToastBuilder? positionedToastBuilder,
  Duration toastDuration = const Duration(seconds: 2),
  ToastGravity? gravity,
}) {
  if (context == null)
    throw ("Error: Context is null, Please call init(context) before showing toast.");
  Widget newChild = _ToastStateFul(child, toastDuration);

  /// Check for keyboard open
  /// If open will ignore the gravity bottom and change it to center
  if (gravity == ToastGravity.BOTTOM) {
    if (MediaQuery.of(context!).viewInsets.bottom != 0) {
      gravity = ToastGravity.CENTER;
    }
  }

  OverlayEntry newEntry = OverlayEntry(builder: (context) {
    if (positionedToastBuilder != null)
      return positionedToastBuilder(context, newChild);
    return _getPostionWidgetBasedOnGravity(newChild, gravity);
  });
  _overlayQueue.add(_ToastEntry(
      entry: newEntry, duration: toastDuration));
  if (_timer == null) _showOverlay();
}