showToast function

Future<void> showToast({
  1. required ToastType type,
  2. String? title,
  3. String? subtitle,
  4. NativeToastOptions options = const NativeToastOptions(),
})

Shows a native toast above Flutter widgets and WebViews.

title and subtitle are optional. A null or blank value is not drawn. If both are missing, this is a no-op.

Icon and background follow type unless NativeToastOptions overrides them.

Implementation

Future<void> showToast({
  required ToastType type,
  String? title,
  String? subtitle,
  NativeToastOptions options = const NativeToastOptions(),
}) async {
  final resolvedTitle = _visibleText(title);
  final resolvedSubtitle = _visibleText(subtitle);
  if (resolvedTitle.isEmpty && resolvedSubtitle.isEmpty) {
    return;
  }

  final resolvedPosition = options.position;
  final resolvedLength = options.length;
  if (resolvedLength.durationMs <= 0 && resolvedLength.durationMs != -1) {
    throw ArgumentError.value(
      resolvedLength.durationMs,
      'options.length',
      'Custom duration must be a positive integer in milliseconds.',
    );
  }
  final resolvedDismiss = options.dismissDirection ??
      (resolvedPosition == ToastPosition.top
          ? NativeToastDismissDirection.up
          : NativeToastDismissDirection.down);
  final resolvedColor = options.bgColor ?? _defaultColorFor(type);
  final resolvedIcon = options.icon ?? _defaultIconFor(type);

  await _channel.invokeMethod('showToast', {
    'type': type.name,
    'title': resolvedTitle,
    'subtitle': resolvedSubtitle,
    'position': resolvedPosition.name,
    'durationMs': resolvedLength.durationMs,
    'color': resolvedColor.toARGB32(),
    'icon': resolvedIcon.name,
    'iconColor': resolvedIcon.color.toARGB32(),
    'dismissDirection': resolvedDismiss.name,
  });
}