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

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.

Related

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.

How to use textField in a Flame game?

I am developing a game with flame engine. I need to open a text input (textField, textEdit or whatever its name) and ask the user his/her input (player name) as a text in my flame game. How can I do that in flame?
There are two ways to go about doing this:
The first option is to build it yourself. This might allow for the most possible level of customization, and would involve creating a TextBoxComponent and listening to KeyboardEvents on your game. You would need to implement rendering and commands like moving the caret by yourself.
The easier option is to use the widgets overlay feature. Remember that the Flame game is just another widget in the tree, and you can compose widgets using the Flutter Stack widget. With widgets overlay you can more easily add a component that hovers on top of your flame game, that you can use to add your text input. I recommend building a simple Flutter Container with all the text input fields and labels you need and using a Widget Overlay to position that on top of your game. Please read the docs for Widget Overlay for more details. Also be sure to check out a list of all the other flame-provided widgets other than GameWidget that you might want to compose with your Flutter tree.

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.

How to animate fullscreen photo transition on swipe, like in Telegram?

When you swipe vertically, image is dragged towards the swipe, with fading out animation of black background.
And after release, it smoothly returns to it's previous position on the screen, like Hero animation does.
How is it possible to recreate such an effect using Flutter? The same scenario, in fullscreen photo view.
photo_view package is desired for fullscreen, so it shouldn't interfere with zooming.
What you need is actually an out-of-the-box widget, and it's surprisingly easy to use. It's called Hero. Basically, you wrap the widget you want to animate like that in a Hero widget, with a specific tag string, and, when you navigate to another screen, you wrap the destination widget in another Hero with the same tag. An effect like the one you shared can be achieved by wrapping two widgets with the same photo with Heroes in different PageRoutes.
Check out this Flutter widget of the week video to get you started, and this Flutter.dev article for a more detailed explanation on Hero widgets.
Edit: I see you are looking for a more specific image-viewer behavior. Then, I suggest you use the photo_view package, which includes many functionalities to visualize images, including the hero transition with swipe-dow-to-dismiss behaviors, pinching to zoom, etc.
I found the solution.
To create such an animation, you should use extended_image package, which has SlideOutPage widget for creation of such transitions.

How to get a widget to move across the screen every X seconds?

Basically I'm trying to build a game to learn flutter more.
Right now I'm just trying to get a Text widget to move from the top of the phone screen, towards the bottom of the phone screen... I'm building a Space Invaders type of game with just Text.
From what I've read by googling the problem, should I use Flame? Or can everything be done by just using the base Flutter framework (collision detection, moving widgets, etc...)? Thanks.
You can use Draggable class for dragging the item.
A simple way to solve this problem is to have the moving Text widget a child of the Stack() widget. Actually, the child of the Stack() widget is a Positioned() widget, which has left and top properties, which you can update periodically with a Timer.periodic function to move the widget down by incrementing top property. So, your Text widget is a child of the Positioned() widget, that you use for, well, positioning.
I have a working example of this in my slowly_moving_widgets_field project, where I have a bunch of arbitrary widgets moving about the screen every X seconds as you desire.