Home > OS >  Flutter CustomPainter paint behavoir and performance
Flutter CustomPainter paint behavoir and performance

Time:01-01

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. 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. I checked what would happen if I decide to resize my window, and you can guess, all CustomPaint widgets are repainted. 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. Still all Widgets extending CustomPainter are repainted when dragging a single Widget or resizing the window.

Now my question is, am I doing something wrong in my state management or is this behavior by default?

Also my C#/WPF app uses half the cpu the Flutter app uses when dragging one shape around. I did not expect such a difference. For now I did not make any complex CustomPaint obecjt in my flutter app. But should I expect a big performance reduction if I have many complex CustomPaint widgets?

CodePudding user response:

am I doing something wrong in my state management or is this behavior by default?

Yes and no. Widgets are rebuilt when the screen size changes, but dragging a widget should not make other widgets rebuild. You can try RepaintBoundary (https://api.flutter.dev/flutter/widgets/RepaintBoundary-class.html)

Should I expect a big performance reduction if I have many complex CustomPaint widgets?

This has a lot to do with what kind of CustomPaint widgets you will be using and what is "many". 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). My assumption is that the efficiency decreases linearly as you add more CustomPaint widgets

  • Related