How to use textField in a Flame game? - flutter

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.

Related

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.

Keep container on top after dragging

I am migrating game from windows phone to flutter card game. I cannot find the way how to keep the card on the top after dragging.
Stack order is too complex and problematic for such simple action and would involve 4 player active, not active cards, cards on the table, combination sets re-arrangement :( In Windows phone there was a Rectangle with ZIndex which i could set to any rectangle on the screen.
I am missing something similar in flutter. I cannot believe that stack order is only way to solve this because it would make any game development impossible.
I am not using any game engine (maybe thats a problem) but i feel like i do not need it because it is not game-loop based game it is event based - click, drag etc.
Do you know any solution how i can achieve that?
There are 2 options -
1. Stack
Update the position of its children by mutating the List and calling setState or use Positioned.
2. Material
The Material widget has a property named elevation. Set it's value dynamically to achieve the effect

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.