showAttachedWidget static method

CancelFunc showAttachedWidget({
  1. required ToastBuilder attachedBuilder,
  2. BuildContext? targetContext,
  3. Offset? target,
  4. WrapAnimation? wrapAnimation = nilWrapAnimation,
  5. WrapAnimation? wrapToastAnimation = nilWrapAnimation,
  6. Color backgroundColor = nilColor,
  7. Object verticalOffset = nil,
  8. Object horizontalOffset = nil,
  9. Duration? duration = nilDuration,
  10. Duration? animationDuration = nilDuration,
  11. Duration? animationReverseDuration = nilDuration,
  12. PreferDirection? preferDirection = nilPreferDirection,
  13. VoidCallback? onClose = nilVoidCallback,
  14. Object ignoreContentClick = nil,
  15. Object onlyOne = nil,
  16. Object allowClick = nil,
  17. Object enableKeyboardSafeArea = nil,
  18. Object enableSafeArea = nil,
})

Displays a positioned Toast. This method can display the Toast around a given Widget (usually a Button) or at a specific offset. attachedBuilder is the builder function to generate the Widget to be displayed. targetContext is the target Widget (usually a button). It's usually wrapped in a Builder to obtain the BuildContext. target is the target Offset. The offset is calculated relative to the top-left corner of the screen. You can either use target or targetContext, not both. verticalOffset is the vertical offset, and it depends on preferDirection. It applies in different directions based on the direction. horizontalOffset is the horizontal offset and depends on preferDirection. It applies in different directions based on the direction. preferDirection is the preferred direction. If space allows, it will prefer to display in that direction. enableSafeArea If true, the toast ensures it doesn't display over the app status bar (it means it's safe); false otherwise. wrapAnimation Please refer to showAnimationWidget.wrapAnimation. Default value is null. wrapToastAnimation Please refer to showAnimationWidget.wrapToastAnimation. Default value is attachedAnimation. animationDuration Please refer to showAnimationWidget.animationDuration. animationReverseDuration Please refer to showAnimationWidget.animationReverseDuration. duration Please refer to showEnhancedWidget.duration. ignoreContentClick Please refer to showEnhancedWidget.ignoreContentClick. onlyOne Please refer to showEnhancedWidget.onlyOne. allowClick Please refer to showEnhancedWidget.allowClick. onClose Please refer to showEnhancedWidget.onClose. enableKeyboardSafeArea Please refer to showEnhancedWidget.enableKeyboardSafeArea.

显示一个定位Toast 该方法可以在某个Widget(一般是Button)或者给定一个offset周围显示 attachedBuilder 生成需要显示的Widget的builder函数 targetContext 目标Widget(一般是一个按钮),使用上一般会使用Builder包裹,来获取到BuildContext target 目标Offset,该偏移是以屏幕左上角为原点来计算的 targettargetContext 只能二选一 verticalOffset 垂直偏移跟preferDirection有关,根据不同的方向会作用在不用的方向上 horizontalOffset 水平偏移跟preferDirection有关,根据不同的方向会作用在不用的方向上 preferDirection 偏好方向,如果在空间允许的情况下,会偏向显示在那边 enableSafeArea 如果为true则toast确保不会显示在app状态栏上面(意味着是安全的),false则反之 wrapAnimation 请看showAnimationWidget.wrapAnimation,默认值为null wrapToastAnimation 请看showAnimationWidget.wrapToastAnimation,默认值为attachedAnimation animationDuration 请看showAnimationWidget.animationDuration animationReverseDuration 请看showAnimationWidget.animationReverseDuration duration 请看showEnhancedWidget.duration ignoreContentClick 请看showEnhancedWidget.ignoreContentClick onlyOne 请看showEnhancedWidget.onlyOne allowClick 请看showEnhancedWidget.allowClick onClose 请看showEnhancedWidget.onClose enableKeyboardSafeArea 请看showEnhancedWidget.enableKeyboardSafeArea

Implementation

static CancelFunc showAttachedWidget({
  required ToastBuilder attachedBuilder,
  BuildContext? targetContext,
  Offset? target,
  WrapAnimation? wrapAnimation = nilWrapAnimation,
  WrapAnimation? wrapToastAnimation = nilWrapAnimation,
  Color backgroundColor = nilColor,
  /*double*/ Object verticalOffset = nil,
  /*double*/ Object horizontalOffset = nil,
  Duration? duration = nilDuration,
  Duration? animationDuration = nilDuration,
  Duration? animationReverseDuration = nilDuration,
  PreferDirection? preferDirection = nilPreferDirection,
  VoidCallback? onClose = nilVoidCallback,
  /*bool*/ Object ignoreContentClick = nil,
  /*bool*/ Object onlyOne = nil,
  /*bool*/ Object allowClick = nil,
  /*bool*/ Object enableKeyboardSafeArea = nil,
  /*bool*/ Object enableSafeArea = nil,
}) {
  var o = defaultOption.attached;

  wrapAnimation = returnFirstIfNotNil(wrapAnimation, o.wrapAnimation);
  wrapToastAnimation = returnFirstIfNotNil(wrapToastAnimation, o.wrapToastAnimation);
  backgroundColor = returnFirstIfNotNil(backgroundColor, o.backgroundColor);
  duration = returnFirstIfNotNil(duration, o.duration);
  animationDuration = returnFirstIfNotNil(animationDuration, o.animationDuration);
  animationReverseDuration = returnFirstIfNotNil(animationReverseDuration, o.animationReverseDuration);
  preferDirection = returnFirstIfNotNil(preferDirection, o.preferDirection);
  onClose = returnFirstIfNotNil(onClose, o.onClose);
  ignoreContentClick = returnFirstIfNotNil(ignoreContentClick, o.ignoreContentClick);
  onlyOne = returnFirstIfNotNil(onlyOne, o.onlyOne);
  allowClick = returnFirstIfNotNil(allowClick, o.allowClick);
  enableKeyboardSafeArea = returnFirstIfNotNil(enableKeyboardSafeArea, o.enableKeyboardSafeArea);
  enableSafeArea = returnFirstIfNotNil(enableSafeArea, o.enableSafeArea);

  final verticalOffsetV = returnFirstIfNotNilAndCast<double>(verticalOffset, o.verticalOffset);
  final horizontalOffsetV = returnFirstIfNotNilAndCast<double>(horizontalOffset, o.horizontalOffset);

  assert(verticalOffsetV >= 0.0, 'must be a positive number');
  assert(verticalOffsetV >= 0.0, 'must be a positive number');
  assert(!(targetContext != null && target != null), 'targetContext and target cannot coexist');
  assert(targetContext != null || target != null, 'targetContext and target must exist one');

  assert(isNilOr<bool>([enableKeyboardSafeArea, onlyOne, enableSafeArea, allowClick, ignoreContentClick]), 'Must be of bool type');
  assert(isNilOr<double>([verticalOffsetV, horizontalOffsetV]), 'Must be of bool type');

  Rect targetRect;
  if (target == null) {
    RenderObject renderObject = targetContext!.findRenderObject()!;
    if (renderObject is RenderBox) {
      final position = renderObject.localToGlobal(Offset.zero);
      targetRect = Rect.fromLTWH(position.dx, position.dy, renderObject.size.width, renderObject.size.height);
    } else {
      throw Exception('context.findRenderObject() return result must be RenderBox class');
    }
  } else {
    targetRect = Rect.fromLTWH(target.dx, target.dy, 0, 0); //点矩形
  }
  return showAnimationWidget(
      allowClick: allowClick,
      clickClose: true,
      groupKey: attachedKey,
      crossPage: false,
      onlyOne: onlyOne,
      onClose: onClose,
      enableKeyboardSafeArea: enableKeyboardSafeArea,
      backgroundColor: backgroundColor,
      ignoreContentClick: ignoreContentClick,
      animationDuration: animationDuration ?? o.animationDuration,
      animationReverseDuration: animationReverseDuration,
      duration: duration,
      wrapAnimation: wrapAnimation,
      wrapToastAnimation: (controller, cancel, child) => KeyboardVisibility(
            onKeyboardVisibilityChanged: (open) {
              if (open) {
                cancel();
              }
            },
            child: Builder(
              builder: (context) {
                return CustomSingleChildLayout(
                    delegate: PositionDelegate(
                        target: targetRect,
                        paddingTop: MediaQuery.of(context).padding.top,
                        verticalOffset: verticalOffsetV,
                        horizontalOffset: horizontalOffsetV,
                        enableSafeArea: enableSafeArea == true,
                        preferDirection: preferDirection),
                    child: wrapToastAnimation != null ? wrapToastAnimation(controller, cancel, child) : child);
              }
            ),
          ),
      toastBuilder: attachedBuilder);
}