Wraparound widget in flutter - flutter

I am looking for way to implement wraparound widget in flutter. I need to create screen which space will be finite but unbounded, In other words I want to do something like when widget1 leaves the side of the screen it have to reappear on the opposite side immediately. I have stack and thanks to positioned I am re-rendering new position of widget based on accelerometer.
I don't know where to even start looking, if there is some package for it or should I implement it from scratch?

Please have a look at the package carousel_slider
https://pub.dev/packages/carousel_slider
It also features infinity scrolling which should provide the effect you want.

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

Flutter - how to have a widget respond to touch and flick?

Currently in the design phase of an app. One of the goals we have is to be able to touch a certain widget, and on tap and hold, have the widget then follow the user's finger where they drag.
Then, if the user releases gently, the widget snaps back to the original location.
However, if the user flicks the widget, we want the widget to fly across the screen, reacting correctly to the user's flick.
Is there anything built-in that can handle this? Also, if this needs to be explained more to make sense, happy to elaborate.
Thanks!
I think for such cases you can use GestureDetector widget. It provides several useful functions. See here:
https://api.flutter.dev/flutter/widgets/GestureDetector-class.html
You can use onPanStart of GestureDetector and then get the offset and use it to move the widget accross the screen.

How can you implement SafeArea for flame?

Heyo,
i have been trying to look for a possibility to implement SafeArea (or something equal to that) in my flame game. I found this (https://github.com/ikbendewilliam/flutter_flame_architecture/blob/main/lib/src/widgets/flame_safe_area.dart) but I dont know how to fully implement this in the screen leading to the game
Is there a simple solution for this issue I am missing?
Since the GameWidget that you wrap your FlameGame in is just a normal Flutter widget you can put the GameWidget as a child of a SafeArea.

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.

Flutter. Throw widget after dragging

I have been searching around for days in the jungle of widgets, trying to figure out which widget suits my purpose best.
What I have done so far: I have a Positioned widget with a listener, that I can drag around in a Stack with onPointerMove.
What I want to achieve is the following:
If I move my finger a bit faster and at some point let go, I would like to have the widget to continue with the given speed (with some friction) and direction (x,y) until it reaches some point.
Im a iOS developer and did this in Swift, but have I reached a point where I met the limit of Flutter?
Flutter has a Draggable widget which contains velocity in the onDragEnd event. You might use this and some basic animation to fling the widget using physics. Check out the Cookbook on the Flutter API site.