CustomPainter called endlessly? - flutter

My CustomPainter was working fine with the old Mapbox plugin, but switching to the new Mapbox plugin causes its paint method to get called over and over, without any screen changes or shouldRepaint being called.
My CustomPainter is over the map using a Stack widget.
What is causing this? Or is there a way to guard against it?

Related

How not to rebuild an Overlay Widget for Flutter Flame?

I have a FlameGame widget and have 2 overlays.
Overlay A (with animation)
Overlay B (with button)
At some point in code I need to display booth overlays.
Overlay A will disappear after animation ends.
Overlay B will disappear after button press.
My problem is that if I press the button on overlay B before the animation ends on overlay A, it causes a rebuild on overlay A and the animation restarts. (it does not look nice...)
Tried switching around the order in witch overlays are added. Only makes A appear behind B.
Tried making A stateful. Still, the removal of B causes A to call initState() again.
Until someone suggest a non hacky way, I solved it by:
Adding a double? variable to the FlameGame component of which I am added the overlays to and in the initState() of the A overlay I check if the value is non null.
If it is I set it and keep setting it every time in the addListener() of the animation.
If it is non null, I set my animation value to that value.
In the dispose() I set the double? to null.
It is messy, but if anyone will need solution to this exact problem, I will try to provide the code.

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. 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?
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

Flutter - can't access to previous PaintingContext in RenderObject paint function

I have a LeafRenderObjectWidget and want to update my view according to the new widget parameters. for example, I have drawn a line and wish to update the line using scale and don't want to paint it again on the next build.
the problem is in the paint function of RenderObject I have access to PaintingContext but it is not the previous one. so I can't use context.canvas.save() and restore it again in the paint function.
When creating RenderObjects from scratch it's easy to get carried away, but my advice is to not fear repainting. When you call methods on a canvas all it's doing is adding commands to a list that eventually get sent to the raster thread, no actual rendering work is being done here.
It sounds like you are confused on the purpose of canvas.save and canvas.restore. These methods do not save what you painted to the canvas, they only touch the paint transform set by scale, skew, transform and translate.

Render Flutter Widget on Canvas - CustomPainter

I was looking into building a game with Flutter. I have made multiple apps with Flutter already, but haven't really used the CustomPainter widgets a lot. I can make a lot of the UI I want to with Widgets rather than creating my own custom shape. So how do I convert a widget to an object that I can render on the canvas?

Flutter CustomPainter How to trigger a repaint

I'm working on an app that uses CustomPainter and that repaints when a build is performed. This build instantiates a new instance of the CustomPainter that repaints the canvas. This works OK, however I believe it is possible to trigger a repaint on an instance of CustomPainter directly with having to reinstantiate it in a build. Can someone point me to an example that shows how that is done?