showToast method

void showToast(
  1. String message, {
  2. ToastPosition toastPosition = ToastPosition.bottom,
})

Shows simple Toast with given message

Implementation

void showToast(String message, {ToastPosition toastPosition = ToastPosition.bottom}) {
  assert(_key.currentState?.overlay != null, 'Tried to show toast but overlayState was null. Key was :$_key');
  assert(_defaultFadeInDuration + _defaultFadeInDelay <= _overlayDuration, '''
  _defaultFadeInDelay + _defaultFadeDuration must be less than or equal to _overlayDuration''');
  final currentOpacity = 0.observable;
  Positioned widgetBuilder(BuildContext context) {
    final size = context.mediaQuerySize;
    return Positioned(
      top: toastPosition == ToastPosition.top ? context.mediaQueryViewPadding.top + _getVerticalPosition(size) : null,
      bottom: toastPosition == ToastPosition.bottom ? context.mediaQueryViewPadding.bottom + _getVerticalPosition(size) : null,
      left: _getHorizontalPosition(size),
      right: _getHorizontalPosition(size),
      child: Observer(
        builder: (context) {
          return AnimatedOpacity(
            opacity: currentOpacity.value.toDouble(),
            duration: _defaultFadeInDuration,
            child: Material(
              color: _defaultToastColor,
              borderRadius: BorderRadius.circular(_toastRadius),
              child: Center(
                child: Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 6),
                  child: Text(
                    message,
                    style: const TextStyle(color: Colors.white),
                    textAlign: TextAlign.center,
                  ),
                ),
              ),
            ),
          );
        },
      ),
    );
  }

  final overlayEntry = OverlayEntry(builder: widgetBuilder);
  _key.currentState!.overlay!.insert(overlayEntry);
  _defaultFadeInDelay.delay(() {
    currentOpacity.value = 1;
  });
  _overlayDuration.delay(() {
    overlayEntry
      ..remove()
      ..dispose();
  });
}