如何将CustomPaint设置为父窗口小部件?

return new Container(
    color: Color(0xfffff4f0),
    child: new Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        mainAxisSize: MainAxisSize.max,
        children: <Widget>[
          new Expanded(
          flex: 6,
          child: FittedBox(
                  fit: BoxFit.contain,
                  child: widget CustomePaint() // Containers(),

          )),
          new Expanded(
              flex: 4,
          )
    )
)

适合:BoxFit.contain适用于
    Container(
        height: 30,
        width: 10
    )

但不适用于在 Canvas 矩形上绘制的CustomePaint():
canvas.drawRect(new Rect.fromLTWH(0, 0, 10 , 30), new Paint()..color =
Colors.red);

dart - CustomPaint和FittedBox-LMLPHP

最佳答案

看来您只提供了所需代码的一部分。
我已经尝试过几乎您的代码,并且效果很好。我使用了另一种颜色,并在CustomPaint中添加了边框,以更好地查看其绘制位置。


Widget getRootView() {
  return Container(
      color: Colors.green,
      child: Column(crossAxisAlignment: CrossAxisAlignment.stretch, mainAxisSize: MainAxisSize.max, children: <Widget>[
        Expanded(
          flex: 2,
          child: CustomPaint(painter: Painter3()),
        ),
        Expanded(
          flex: 1,
          child: Container(
            color: Colors.red,
            child: Center(child: Text("3")),
          ),
        )
      ]));
}

class Painter3 extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    canvas.drawRect(Offset(10, 10) & Size(size.width - 20, size.height - 20), Paint()..color = Colors.blue);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}

我在这里得到的是:dart - CustomPaint和FittedBox-LMLPHP

10-01 21:06