简体   繁体   English

Flutter CustomPainter 绘画行为和表现

[英]Flutter CustomPainter paint behavoir and performance

I am learning and playing around with Flutter. Currently I am trying to reproduce an app I previously made with C# and WPF. In my Flutter app I have a list of Widgets extending CustomPainter.我正在学习和玩 Flutter。目前我正在尝试重现我以前用 C# 和 WPF 制作的应用程序。在我的 Flutter 应用程序中,我有一个扩展 CustomPainter 的小部件列表。 Now I am able to move/drag any of these widgets around the screen.现在我可以在屏幕上移动/拖动这些小部件中的任何一个。 What I see is that always when dragging one of this widgets all CustomPaint widgets are repainted.我看到的是,总是在拖动其中一个小部件时,所有 CustomPaint 小部件都会重新绘制。 I checked what would happen if I decide to resize my window, and you can guess, all CustomPaint widgets are repainted.我检查了如果我决定调整我的 window 的大小会发生什么,你可以猜到,所有 CustomPaint 小部件都被重新绘制了。 I decided to create three different projects.我决定创建三个不同的项目。 One is using statefull widgets and setState to manage the state. The other is using Provider and the last one is using Riverpod.一种是使用 statefull widgets 和 setState 来管理 state。另一种是使用 Provider,最后一种是使用 Riverpod。 Still all Widgets extending CustomPainter are repainted when dragging a single Widget or resizing the window.当拖动单个 Widget 或调整 window 的大小时,所有扩展 CustomPainter 的 Widget 仍然会被重新绘制。

Now my question is, am I doing something wrong in my state management or is this behavior by default?现在我的问题是,我是在我的 state 管理中做错了什么还是默认行为?

Also my C#/WPF app uses half the cpu the Flutter app uses when dragging one shape around.此外,我的 C#/WPF 应用程序使用的 CPU 是 Flutter 应用程序在拖动一个形状时使用的一半。 I did not expect such a difference.我没想到会有这样的差异。 For now I did not make any complex CustomPaint obecjt in my flutter app.现在我没有在我的 flutter 应用程序中制作任何复杂的 CustomPaint obecjt。 But should I expect a big performance reduction if I have many complex CustomPaint widgets?但是,如果我有许多复杂的 CustomPaint 小部件,我是否应该期望性能大幅下降?

am I doing something wrong in my state management or is this behavior by default?我在我的 state 管理中做错了什么还是默认行为?

No you are not doing it wrong.不,你没有做错。 Widgets are rebuilt when the screen size changes.当屏幕尺寸改变时,小部件被重建。

Should I expect a big performance reduction if I have many complex CustomPaint widgets?如果我有许多复杂的 CustomPaint 小部件,我是否应该期望性能大幅下降?

This has a lot to do with what kind of CustomPaint widgets you will be using and what is "many".这与您将使用哪种 CustomPaint 小部件以及什么是“许多”有很大关系。 You can test how long the CustomPaint widget's paint method takes, for example by using the profiler ( https://docs.flutter.dev/perf/ui-performance ).您可以测试 CustomPaint 小部件的绘制方法需要多长时间,例如使用探查器 ( https://docs.flutter.dev/perf/ui-performance )。 My assumption is that the efficiency decreases linearly as you add more CustomPaint widgets我的假设是,随着您添加更多 CustomPaint 小部件,效率会线性下降

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用颤振 customPainter 小部件绘制形状 - Paint shape with flutter customPainter widget 有没有办法在颤振中存储 CustomPainter 的 Path 和 Paint? - Is there a way to store Path and Paint of CustomPainter in flutter? Flutter 提高 CustomPainter animation 性能 - Flutter Improve CustomPainter animation performance Flutter - 如何移动和缩放 Canvas 绘图而无需再次运行 CustomPainter 绘画方法之类的东西? - Flutter - How to move and zoom Canvas drawings without AGAIN running something like the CustomPainter paint method? Flutter:CustomPainter 绘制方法被调用多次而不是只调用一次 - Flutter: CustomPainter paint method gets called several times instead of only once Flutter CustomPainter 半径直 - Flutter CustomPainter radius straight Flutter CustomPainter 平移和缩放 - Flutter CustomPainter Pan and Zoom TextButton 内部的 CustomPainter 颤动 - CustomPainter inside TextButton flutter 为什么我的 Flutter CustomPainter 类在使用 canvas..drawArc() 函数且扫描角度小于 2*pi 时不会在屏幕上绘制任何内容? - Why does my Flutter CustomPainter class not paint anything on the screen when using the canvas..drawArc() function with a sweepAngle less than 2*pi? Flutter:CustomPainter难以确定偏移量 - Flutter: CustomPainter difficulties with offset determination
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM