Render Flutter Widget on Canvas - CustomPainter - flutter

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?

Related

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

Is there a way to way to use a VideoPlayer widget in place of an Image widget?

The panorama package on pub.dev only works with Image objects, but the premise should be the same if a video is used instead.
I want to modify this package to accept a VideoPlayer or VideoPlayerController widget from the video_player package. What are the things I must do to make it compatible?
The way panorama works is it takes the Image object and passes it to the package flutter_cube where it then makes a Mesh and texture out of it. The panorama viewing is done based off of this.
What I want to achieve is instead of Panorama accepting an Image widget, it should be the VideoPlayer widget. Width and height are really the only two variables needed so I wonder what I should do, those two are easily obtainable from the VideoPlayerController.

Can I use a Widget as a actual component in Flutter Flame

Can Flutter Flame render Widget inside game world like other components? (not overlay widget).
(I want to apply forge2d physic to widget and I'm trying to do it with Flame.)
You can not, you can only do it through the overlay system or use a Stack, but the widgets are never "in" the game, but on top of the game.
The game can only have Flame Components added directly to it.
You can still interact with the overlay widgets from the game though, like this for example: https://twitter.com/spydon/status/1417832832693673988
In that example I simply wrap the ElevatedButton with Positioned and Transform and then I updated those with the values from corresponding BodyComponents.

how to create a scrollView in Flutter Flame

I need a listView for my Game. Is there a simple way to create one? There are no tutorials for such a feature and I couldn't find anything in the documentation.
Flame doesn't provide their own widgets for these things, we rely on Flutters excellent widget system.
So you create the widget directly in Flutter and then you either put your GameWidget in a stack (or similar) and use Flutter's own Navigation to move between the widgets, or you use Flame's overlay system.
For using the overlays you add the overlays that you want to have accessible when you create the GameWidget and then you call game.overlays.add to render a specific widget, and game.overlays.remove to stop rendering it.

Rendering a flutter Widget to an opengl texture?

Let’s say I have a flutter module that provides a widget let's call it InterestingWidget. On the other side I have a native mobile application (ios/android) that displays a rotating cube with OpenGL. I would like to display the InterestingWidget on a face of the cube. Does this sound possible ?
Flutter seems to be able to render a widget to image ( Creating raw image from Widget or Canvas it seems there are problems with camerapreview or video widgets thought), so this step sounds possible.
I'm not sure if this can be done when the Widget is not in the rendering pipe though, the widget would need to render only on image.
Then we need to convert that image to opengltexture on native ios/android side, that sounds possible too with a bit of work.
And finally, I'm not sure about the interactivity, how to send simulated touches to the widget when user touches the face of the cube where the widget is rendered.