build method

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

Describes the part of the user interface represented by this widget.

The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes). This method can potentially be called in every frame and should not have any side effects beyond building a widget.

The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.

Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.

The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.

The implementation of this method must only depend on:

If a widget's build method is to depend on anything else, use a StatefulWidget instead.

See also:

  • StatelessWidget, which contains the discussion on performance considerations.

Implementation

@override
Widget build(BuildContext context) {
  return ImagePixels(
      imageProvider: imageProvider,
      builder: (context, img) {
        var leftTop = -1;
        var leftBottom = -1;

        var topLeft = -1;
        var topRight = -1;

        var paddBottomLeft = -1;
        var paddBottomRight = -1;

        var paddRightTop = -1;
        var paddRightBottom = -1;

        if (img.height != null && img.width != null) {
          //遍历全部坐标系,判断是黑色的就显示坐标点
          // for (var y = 0; y < img.height!; y++) {
          //   for (var x = 0; x < img.width!; x++) {
          //     var color = img.pixelColorAt!(x, y);
          //     if (color == Colors.black) {
          //       log("---x$x, y$y-------->");
          //     }
          //   }
          // }

          for (var i = 0; i < img.height!; i++) {
            var color = img.pixelColorAt!(0, i);
            if (color == Colors.black) {
              if (leftTop == -1) {
                leftTop = i;
                continue;
              }
              if (leftTop != -1) {
                leftBottom = i;
              }
            }

            var color2 = img.pixelColorAt!(img.width! - 1, i);
            if (color2 == Colors.black) {
              if (paddRightTop == -1) {
                paddRightTop = i;
                continue;
              }
              if (paddRightTop != -1) {
                paddRightBottom = i;
              }
            }
          }

          for (var i = 0; i < img.width!; i++) {
            var color = img.pixelColorAt!(i, 0);
            if (color == Colors.black) {
              if (topLeft == -1) {
                topLeft = i;
                continue;
              }
              if (topLeft != -1) {
                topRight = i;
              }
            }
            var color2 = img.pixelColorAt!(i, img.height! - 1);
            if (color2 == Colors.black) {
              if (paddBottomLeft == -1) {
                paddBottomLeft = i;
                continue;
              }
              if (paddBottomLeft != -1) {
                paddBottomRight = i;
              }
            }
          }
        } else {
          leftTop = 0;
          leftBottom = 0;
          topLeft = 0;
          topRight = 0;
          paddRightTop = 0;
          paddRightBottom = 0;
          paddBottomLeft = 0;
          paddBottomRight = 0;
        }
        // log("打印坐标系:X($topLeft,$topRight),Y($leftTop,$leftBottom)");
        // log("打印填充坐标系:X($paddBottomLeft,$paddBottomRight),Y($paddRightTop,$paddRightBottom)");
        return ClipPath(
          //裁切黑边
          clipper: BlackLineClipper(hideLines: hideLines),
          child: Container(
            //这里填充默认宽(31+17=48)高(8+7=15)
            padding: getDefulteSize(
                paddBottomLeft.toDouble(),
                paddBottomRight.toDouble(),
                paddRightTop.toDouble(),
                paddRightBottom.toDouble(),
                img.width ?? 0.toDouble(),
                img.height ?? 0.toDouble()),
            // padding: EdgeInsets.fromLTRB(
            //     topLeft.toDouble(),
            //     leftTop.toDouble(),
            //     (img.width ?? 0) - topRight.toDouble(),
            //     (img.height ?? 0) - leftBottom.toDouble()),
            //9图填充背景,背景扩展区域,
            decoration: BoxDecoration(
                image: DecorationImage(
              //图片源
              image: imageProvider,
              centerSlice: Rect.fromLTRB(
                topLeft.toDouble() > 0
                    ? topLeft.toDouble()
                    : topRight.toDouble() + 1, //31
                leftTop.toDouble() > 0
                    ? leftTop.toDouble()
                    : leftBottom.toDouble() + 1, //17
                topRight.toDouble() > 0
                    ? topRight.toDouble()
                    : topLeft.toDouble() + 1, //32
                leftBottom.toDouble() > 0
                    ? leftBottom.toDouble()
                    : leftTop.toDouble() + 1,
              ), //18
            )),
            child: child,
          ),
        );
      });
}