英文:
The custom painting is not being displayed - Flutter
问题
我已创建了一个扩展CustomPaint的类,并重写了paint()方法。出现了一些原因,它没有显示出来,我尝试了很多方法,
此外,当我运行我的应用程序时,自定义绘制也没有显示出来。我做错了什么?
英文:
I have created a class that extends CustomPaint and overridden the paint() method. For some reason is not being displayed I tried a lot of ways,
also When I run my app, the custom painting is not displayed. What am I doing wrong?
答案1
得分: 1
在Flutter中,自定义绘制小部件未显示的最常见原因是它们未添加到小部件树中。要将自定义绘制小部件添加到小部件树中,您需要使用CustomPaint.add()方法。例如:
class MyCustomPaint extends CustomPaint {
@override
void paint(Canvas canvas, Size size) {
// 在这里绘制您的自定义绘画。
}
}
void main() {
runApp(MaterialApp(
home: Scaffold(
body: CustomPaint.add(
child: Container(),
painter: MyCustomPaint(),
),
),
));
}
在这个示例中,MyCustomPaint小部件被添加到小部件树中作为Container小部件的子级。这确保了在运行应用程序时会显示自定义绘制。
我还建议阅读这篇博客 a-deep-dive-into-custompaint。
英文:
The most common reason why custom painting widgets do not display in Flutter is because they are not being added to the widget tree. To add a custom painting widget to the widget tree, you need to use the CustomPaint.add() method. For example:
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(),
),
),
));
}
In this example, the MyCustomPaint widget is being added to the widget tree as a child of the Container widget. This ensures that the custom painting will be displayed when the app is run.
I also suggest to read this blog a-deep-dive-into-custompaint,
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论