build method

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

build as with any stateful widget state.

Implementation

@override
Widget build(BuildContext context) {
  widget.stateKeeper.state ??= this;
  if (!widget.flutsterTestRecord.active) {
    return (widget.child);
  }
  RenderBox? box =
      widget.positionKey.currentContext?.findRenderObject() as RenderBox?;
  Offset position = const Offset(0, 0);
  if (box != null) {
    position = box.localToGlobal(Offset.zero);
    if (buttonSize > box.size.width * buttonSizeMaxProportion) {
      buttonSize = box.size.width * buttonSizeMaxProportion;
    }
    if (buttonSize > box.size.height * buttonSizeMaxProportion) {
      buttonSize = box.size.height * buttonSizeMaxProportion;
    }
    if (widget.flutsterTestRecord.buttonSize != null) {
      buttonSize = widget.flutsterTestRecord.buttonSize!;
    }
  }
  double newX = (floatingButtonPosition?.dx ?? 0) - position.dx;
  double newY = (floatingButtonPosition?.dy ?? 0) - position.dy;
  if (newX < 0) {
    newX = 0;
  }
  if (newY < 0) {
    newY = 0;
  }
  if (box != null) {
    if (newX > box.size.width - buttonSize) {
      newX = box.size.width - buttonSize;
    }
    if (newY > box.size.height - buttonSize) {
      newY = box.size.height - buttonSize;
    }
  } else {
    Future.delayed(const Duration(milliseconds: 500)).whenComplete(() {
      try {
        setState(() {});
      } catch (e) {
        debugPrint("Warning: failed to set state without box.");
      }
    });
  }
  return Stack(
    key: widget.positionKey,
    children: [
      KeyboardListener(
        focusNode: focusNode,
        onKeyEvent: (keyEvent) {
          String? text = keyEvent.character;
          widget.flutsterTestRecord.add(FlutsterTestEvent.text(
              flutsterTestRecorderState: this,
              typedText: ifJsonAble(text) ?? "",
              keyEvent: keyEvent,
              widgetName: widget.name));
        },
        child: Listener(
          onPointerDown: (pointerDownEvent) {
            widget.flutsterTestRecord.add(FlutsterTestEvent.tap(
                flutsterTestRecorderState: this,
                tapStart: pointerDownEvent.position,
                widgetName: widget.name));
          },
          onPointerUp: (pointerUpEvent) {
            widget.flutsterTestRecord.tapUp(pointerUpEvent.position);
          },
          child: RepaintBoundary(
            key: screenKey,
            child: widget.child,
          ),
        ),
      ),
      Positioned(
        left: newX,
        top: newY,
        child: SizedBox(
          height: buttonSize,
          width: buttonSize,
          child: Draggable(
            feedback: SizedBox(
              height: buttonSize,
              width: buttonSize,
              child: buildFAB(setState),
            ),
            childWhenDragging: Container(),
            onDragEnd: (details) {
              setState(() {
                floatingButtonPosition = details.offset;
              });
            },
            child: buildFAB(setState),
          ),
        ),
      ),
    ],
  );
}