showToastInQueue static method

OverlayShower showToastInQueue(
  1. String text, {
  2. String? key,
  3. TextStyle? textStyle,
  4. EdgeInsets? padding,
  5. Decoration? decoration,
  6. Color? backgroundColor,
  7. BorderRadius? radius,
  8. BoxShadow? shadow,
  9. Curve? curve,
  10. Duration? appearDuration,
  11. Duration? dismissDuration,
  12. Duration? onScreenDuration,
  13. Offset? slideBegin,
  14. double? opacityBegin,
  15. Widget appearAnimatedBuilder(
    1. OverlayShower shower,
    2. AnimationController controller,
    3. Widget widget
    )?,
  16. Widget dismissAnimatedBuilder(
    1. OverlayShower shower,
    2. AnimationController controller,
    3. Widget widget
    )?,
  17. EdgeInsets increaseOffset = const EdgeInsets.only(top: 45),
})

Implementation

static OverlayShower showToastInQueue(
  String text, {
  String? key,
  TextStyle? textStyle,
  // text in padding
  EdgeInsets? padding,
  // container decoration;
  Decoration? decoration,
  Color? backgroundColor,
  BorderRadius? radius,
  BoxShadow? shadow,
  // animation
  Curve? curve,
  Duration? appearDuration,
  Duration? dismissDuration,
  Duration? onScreenDuration,
  // animation settings
  Offset? slideBegin,
  double? opacityBegin,
  Widget Function(OverlayShower shower, AnimationController controller, Widget widget)? appearAnimatedBuilder,
  Widget Function(OverlayShower shower, AnimationController controller, Widget widget)? dismissAnimatedBuilder,
  // queue increment offset
  EdgeInsets increaseOffset = const EdgeInsets.only(top: 45),
  // List? queue,
}) {
  OverlayShower shower = showToast(
    text,
    key: key,
    textStyle: textStyle,
    padding: padding,
    decoration: decoration,
    backgroundColor: backgroundColor,
    radius: radius,
    shadow: shadow,
    curve: curve,
    appearDuration: appearDuration,
    dismissDuration: dismissDuration,
    onScreenDuration: onScreenDuration,
    slideBegin: slideBegin,
    opacityBegin: opacityBegin,
    appearAnimatedBuilder: appearAnimatedBuilder,
    dismissAnimatedBuilder: dismissAnimatedBuilder,
  );
  // Use micro, for the caller will modified properties(i.e margin, alignment) outside
  // The appear animation controller will setState, don't worry
  Future.microtask(() {
    String keyInQueue = shower.alignment?.toString() ?? '__Shared_Key__';
    List<OverlayShower?> queue = (sharedToastQueue[keyInQueue] ?? (sharedToastQueue[keyInQueue] = []));

    // 1. get the empty index
    int index = -1;
    for (int i = 0; i < queue.length; i++) {
      OverlayShower? obj = queue[i];
      if (obj == null) {
        index = i;
        queue[i] = shower;
        break;
      }
    }
    if (index == -1) {
      index = queue.length;
      queue.add(shower);
    }

    // 2. calculate the position
    EdgeInsets n = increaseOffset;
    EdgeInsets? m = shower.margin;
    shower.margin = EdgeInsets.only(
      left: n.left * index + (m?.left ?? 0),
      top: n.top * index + (m?.top ?? 0),
      right: n.right * index + (m?.right ?? 0),
      bottom: n.bottom * index + (m?.bottom ?? 0),
    );

    // 3. clear the queue when dismissed
    shower.addDismissCallBack((shower) {
      var index = queue.indexOf(shower);
      if (index != -1) {
        queue[index] = null;
      }
      // if all null
      bool isNullALL = true;
      for (OverlayShower? s in queue) {
        if (s != null) {
          isNullALL = false;
          break;
        }
      }
      if (isNullALL) {
        queue.clear();
      }
    });
  });

  return shower;
}