我试图在子文本框小部件上绘制红点,如下所示: enter image description here 为此,我用 CustomPaint() 包裹了子小部件。小部件:

 return CustomPaint(
      painter: DotsPainter(), //draws red dots based on child's size
      child: child, //textbox
    );

但结果是这样的: enter image description here 如何使 CustomPainter “覆盖”其子小部件? 谢谢。

最佳答案

CustomPaint 有 2 个可能的画家:

When asked to paint, CustomPaint first asks its painter to paint on the current canvas, then it paints its child, and then, after painting its child, it asks its foregroundPainter to paint.

(强调我的) 因此,如果您将画家移至 foregroundPainter相反,它应该可以正常工作:

return CustomPaint(
  foregroundPainter: DotsPainter(), //draws red dots based on child's size
  child: child, //textbox
);