build method

  1. @override
Widget build(
  1. BuildContext context
)

Implementation

@override
Widget build(BuildContext context) {
  // Check if widget is visible before wasting resources on building it
  if (!widget.model.visible) return const Offstage();

  // compute size
  if (width == null || height == null) {
    return UnconstrainedBox(
        child:
            MeasureView(Material(child: BoxView(widget.model)), onMeasured));
  }

  ColorScheme t = Theme.of(context).colorScheme;

  // set the models default border color
  widget.model.defaultBorderColor = t.surfaceVariant;

  // Overlay Manager
  ModalManagerView? manager =
      context.findAncestorWidgetOfExactType<ModalManagerView>();

  // SafeArea
  double sa = MediaQuery.of(context).padding.top;

  // Exceeds Width of Viewport
  atMaxWidth = false;
  maxWidth = MediaQuery.of(context).size.width;
  if (width! >= (maxWidth - (padding * 4))) {
    atMaxWidth = true;
    width = (maxWidth - (padding * 4));
  }
  if (width! <= minimumWidth) width = minimumWidth;

  // Exceeds Height of Viewport
  atMaxHeight = false;
  maxHeight = MediaQuery.of(context).size.height - sa;
  if (height! >= (maxHeight - (padding * 4))) {
    atMaxHeight = true;
    height = (maxHeight - (padding * 4));
  }
  if (height! <= minimumHeight) height = minimumHeight;

  // Content Box
  body ??= Material(child: BoxView(widget.model));

  // Non-Minimized View
  if (!minimized) {
    Widget resize =
        const Icon(Icons.apps, size: 24, color: Colors.transparent);
    Widget resizeableBR = !widget.model.resizeable
        ? Container()
        : GestureDetector(
            onPanUpdate: onResizeBR,
            onTapDown: onBringToFront,
            child: MouseRegion(
                cursor: SystemMouseCursors.resizeUpLeftDownRight,
                child: resize));
    Widget resizeableBL = !widget.model.resizeable
        ? Container()
        : GestureDetector(
            onPanUpdate: onResizeBL,
            onTapDown: onBringToFront,
            child: MouseRegion(
                cursor: SystemMouseCursors.resizeUpRightDownLeft,
                child: resize));
    Widget resizeableTL = !widget.model.resizeable
        ? Container()
        : GestureDetector(
            onPanUpdate: onResizeTL,
            onTapDown: onBringToFront,
            child: MouseRegion(
                cursor: SystemMouseCursors.resizeUpLeftDownRight,
                child: resize));
    Widget resizeableTR = !widget.model.resizeable
        ? Container()
        : GestureDetector(
            onPanUpdate: onResizeTR,
            onTapDown: onBringToFront,
            child: MouseRegion(
                cursor: SystemMouseCursors.resizeUpRightDownLeft,
                child: resize));

    Widget resize2 =
        SizedBox(width: FmlEngine.isMobile ? 34 : 24, height: height);
    Widget resizeableL = !widget.model.resizeable
        ? Container()
        : GestureDetector(
            onPanUpdate: onResizeL,
            onTapDown: onBringToFront,
            child: MouseRegion(
                cursor: SystemMouseCursors.resizeLeftRight, child: resize2));
    Widget resizeableR = !widget.model.resizeable
        ? Container()
        : GestureDetector(
            onPanUpdate: onResizeR,
            onTapDown: onBringToFront,
            child: MouseRegion(
                cursor: SystemMouseCursors.resizeLeftRight, child: resize2));

    Widget resize3 =
        SizedBox(width: width, height: FmlEngine.isMobile ? 34 : 24);
    Widget resizeableT = !widget.model.resizeable
        ? Container()
        : GestureDetector(
            onPanUpdate: onResizeT,
            onTapDown: onBringToFront,
            child: MouseRegion(
                cursor: SystemMouseCursors.resizeUpDown, child: resize3));
    Widget resizeableB = !widget.model.resizeable
        ? Container()
        : GestureDetector(
            onPanUpdate: onResizeB,
            onTapDown: onBringToFront,
            child: MouseRegion(
                cursor: SystemMouseCursors.resizeUpDown, child: resize3));

    // Positioned
    dx ??= (maxWidth / 2) - ((width! + (padding * 2)) / 2);
    dy ??= (maxHeight / 2) - ((height! + (padding * 2)) / 2) + sa;

    // Original Size/Position
    originalDx ??= dx;
    originalDy ??= dy;
    originalWidth ??= width;
    originalHeight ??= height;

    // Last Size/Position
    lastDx ??= dx;
    lastDy ??= dy;
    lastWidth ??= width;
    lastHeight ??= height;

    Widget frame = UnconstrainedBox(
        child: ClipRect(
            child: SizedBox(height: height, width: width, child: body)));

    double headerHeight = 30;
    var header = _buildHeader(t);

    // View
    Widget content = UnconstrainedBox(
        child: Container(
            color: Colors.transparent,
            height: height! + (padding * 2) + headerHeight,
            width: width! + (padding * 2),
            child: Stack(children: [
              Positioned(top: padding, left: padding, child: header),
              Positioned(
                  top: headerHeight + padding, left: padding, child: frame),
              Positioned(top: 0, left: 0, child: resizeableL),
              Positioned(top: 0, right: 0, child: resizeableR),
              Positioned(top: 0, left: 0, child: resizeableT),
              Positioned(bottom: 0, left: 0, child: resizeableB),
              Positioned(top: 0, left: 0, child: resizeableTL),
              Positioned(bottom: 0, left: 0, child: resizeableBL),
              Positioned(bottom: 0, right: 0, child: resizeableBR),
              Positioned(top: 0, right: 0, child: resizeableTR),
            ])));

    // Remove from Park
    if (manager != null) manager.model.unpark(widget);

    Widget curtain = GestureDetector(
        onDoubleTap: onRestoreTo,
        onTapDown: onBringToFront,
        onPanStart: (_) => onBringToFront(null),
        onPanUpdate: onDrag,
        onPanEnd: onDragEnd,
        behavior: HitTestBehavior.deferToChild,
        child: content);

    // Return View
    return Positioned(top: dy, left: dx, child: curtain);
  }

  // Minimized View
  else {
    // Get Parking Spot
    int? slot = 0;
    if (manager != null) slot = manager.model.park(widget);

    // Build View
    Widget scaled = Card(
        margin: const EdgeInsets.all(1),
        elevation: 5,
        color: t.secondary.withOpacity(0.5),
        borderOnForeground: false,
        shape: RoundedRectangleBorder(
            borderRadius: const BorderRadius.all(Radius.circular(4.0)),
            side: BorderSide(width: 2, color: t.primary)),
        child: SizedBox(
            width: 100,
            height: 50,
            child: Padding(
                padding: const EdgeInsets.all(5),
                child: FittedBox(child: body))));
    Widget curtain = GestureDetector(
        onTap: onRestore,
        child: const MouseRegion(
            cursor: SystemMouseCursors.click,
            child: SizedBox(width: 100, height: 50)));
    Widget view = Stack(children: [scaled, curtain]);

    // Return View
    return Positioned(
        bottom: 10, left: 10 + (slot! * 110).toDouble(), child: view);
  }
}