在混乱中,当在脚手架/容器小部件中使用CustomPaint()时,其行为似乎很奇怪]

问题描述 投票:0回答:1
所以,我试图在Flutter中使用CustomPaint小部件在屏幕上绘制一条对角线。从CustomPaint小部件中,我正在调用名为BottomCurvePainter的CustomPainter类。其代码是这样的。

class BottomCurvePainter extends CustomPainter { @override void paint(Canvas canvas, Size size) { // TODO: implement paint Paint paint = Paint(); paint.color = Colors.black; paint.style = PaintingStyle.stroke; paint.strokeWidth = 5.0; Path path = Path(); path.lineTo(size.width, size.height); canvas.drawPath(path, paint); } @override bool shouldRepaint(CustomPainter oldDelegate) { // TODO: implement shouldRepaint //throw UnimplementedError(); return true; } }

我在家里打电话的方式如下:-

class _MyHomePageState extends State<MyHomePage> { @override Widget build(BuildContext context) { return Scaffold( body: Container( color: Colors.orange, child: CustomPaint( painter: BottomCurvePainter(), ), ), ); } }

但是这似乎不起作用。但是,如果我删除了脚手架小部件并直接返回容器,则可以使它工作,就像这样-

class _MyHomePageState extends State<MyHomePage> { @override Widget build(BuildContext context) { return Container( color: Colors.orange, child: CustomPaint( painter: BottomCurvePainter(), ), ); } }

有人可以解释为什么将容器包装在脚手架中无法正常工作吗?谢谢。

所以,我试图在Flutter中使用CustomPaint小部件在屏幕上绘制一条对角线。从CustomPaint小部件中,我正在调用名为BottomCurvePainter的CustomPainter类。代码...

flutter flutter-layout
1个回答
0
投票
您应指定容器的width和height属性。之所以没有出现,是因为您没有指定将在支架中放置的容器的尺寸(或约束)。看看flutter文档中的container widget
© www.soinside.com 2019 - 2024. All rights reserved.