我已经创建了一个扩展CustomPaint并覆盖Paint()方法的类.由于某些原因没有显示出来,我try 了很多方法,
此外,当我运行我的应用程序时,自定义绘画不会显示.我做错了什么?
我已经创建了一个扩展CustomPaint并覆盖Paint()方法的类.由于某些原因没有显示出来,我try 了很多方法,
此外,当我运行我的应用程序时,自定义绘画不会显示.我做错了什么?
自定义绘画小部件不能在Flight中显示的最常见原因是因为它们没有被添加到小部件树中.要将自定义绘画小部件添加到小部件树中,需要使用CustomPaint.add()方法.例如:
class MyCustomPaint extends CustomPaint {
@override
void paint(Canvas canvas, Size size) {
// Paint your custom painting here.
}
}
void main() {
runApp(MaterialApp(
home: Scaffold(
body: CustomPaint.add(
child: Container(),
painter: MyCustomPaint(),
),
),
));
}
在本例中,MyCustomPaint小部件作为Container小部件的子项被添加到小部件树中.这将确保在应用程序运行时显示自定义绘画.
我也建议读一下这篇博客a-deep-dive-into-custompaint篇,