show static method

void show(
  1. String text,
  2. BuildContext context, {
  3. Duration? duration,
  4. Color? background,
  5. TextStyle textStyle = const TextStyle(fontSize: 16, color: Colors.white),
  6. double? radius,
  7. Image? preIcon,
  8. double? verticalOffset,
  9. VoidCallback? onDismiss,
  10. BrnToastGravity? gravity,
})

显示Toast,如不设置duration则会自动根据内容长度来计算(更友好,最长5秒)

Implementation

static void show(
  String text,
  BuildContext context, {
  Duration? duration,
  Color? background,
  TextStyle textStyle = const TextStyle(fontSize: 16, color: Colors.white),
  double? radius,
  Image? preIcon,
  double? verticalOffset,
  VoidCallback? onDismiss,
  BrnToastGravity? gravity,
}) {
  final OverlayState overlayState = Overlay.of(context);

  preToastView?._dismiss();
  preToastView = null;

  final double finalVerticalOffset = getVerticalOffset(
    context: context,
    gravity: gravity,
    verticalOffset: verticalOffset,
  );

  /// 自动根据内容长度决定显示时长,更加人性化
  final int autoDuration = min(text.length * 0.06 + 0.8, 5.0).ceil();
  final Duration finalDuration = duration ?? Duration(seconds: autoDuration);
  final OverlayEntry overlayEntry = OverlayEntry(
    builder: (context) {
      return ToastWidget(
        widget: ToastChild(
          background: background,
          radius: radius,
          msg: text,
          leading: preIcon,
          textStyle: textStyle,
          gravity: gravity,
          verticalOffset: finalVerticalOffset,
        ),
      );
    },
  );
  final ToastView toastView =
      ToastView(overlayState: overlayState, overlayEntry: overlayEntry);
  preToastView = toastView;
  toastView.show(
    duration: finalDuration,
    onDismiss: onDismiss,
  );
}