How to draw animation frames with CustomPainter in Flutter? - flutter

I have a CustomPainter that can paint all sorts of visuals based on a physical model input parameters.
How can I make a 1 second animation that draws the needed frames between two different end points, essentially calling my CustomPainter to paint intermediate values between the two end points whenever a new frame can be drawn?
Container(
width: 800,
height: 500,
child: CustomPaint(
painter: MyPainter(
context,
inputVal: myProvider
),
))
Basically I want to make a function that runs a one second long sequence where values in myProvider change incrementally from start values to end values, and the CustomPaint redraws the visuals based on current values whenever a new frame is drawn. Is this possible?

Maybe check out tween animation. I think it should help you.

Related

How to create a dynamic page in flutter

I'd like to list some components in a row and when a component reaches the end of the page it should just move to the next row. In this way I expect the page to be adjusted dynamically to the size of the screen. I don’t have a code example because it’s a theoretical question.
You can use the screen width/height to calculate the size of the row widgets.
To get the screen size do the following:
final height = MediaQuery.of(context).size.height;
final width = MediaQuery.of(context).size.width;
final height = MediaQuery.of(context).size.height; final width = MediaQuery.of(context).size.width;
// or you can use screen util package it make your screen responsive
and make your code inside SizedBox
it will work
like this :
SizedBox( height: 100, width: width * 0.85 // this mean width is 85% of screen width// child: //your code ),
The obvious answer is Wrap. Give it some children, and it lays them out by default start to end horizontally, and when the next child doesn't fit, it starts a second line.
You don't even need to put it in a row, but you can certainly use it as part of a row or part of a column.

How to rotate Lottie animation in Flutter

I have a Flutter app that is showing some Lottie animations. I sometimes need to flip the animation by 180 degrees on Y axis so that it it is a mirror image of itself.
In C# this is easily achievable by setting animated visual player's plane projection rotationY property to 180 (see below).
<muxc:AnimatedVisualPlayer x:Name="LottiePlayer">
<muxc:AnimatedVisualPlayer.Projection>
<PlaneProjection RotationY="180" x:Name="LottiePayerRotation"/>
</muxc:AnimatedVisualPlayer.Projection>
In flutter I tried using RotationBox, but that only rotates around X axis. I need to rotate around Y axis (see image below).
I also tried wrapping Lottie animation inside Transform widget (see below), but that doesn't work. After I added that, the Lottie animation completely disappears. I don't quite understand how Matrix4 works, there is very little documentation on it. I found this Matrix4 explanation but I still don't understand it. :-(
Transform(
transform: Matrix4(
1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1,
)..rotateX(0)..rotateY(180)..rotateZ(0),
child: Lottie(
frameRate: FrameRate.max,
composition: _composition,
controller: _controller,
),
),
Note that I do not need the flip to be animated, I just want to flip the Lottie animation instantly so that it looks like a mirror image of itself. So an instant change, not a transition animated.
Any help appreciated.
I have used something like this before to rotate items... not sure if its what you are after
child: Transform.rotate(
angle: 180 / Math.pi, // Rotations are supplied in radians
child: Container())
Turns out I almost had it! What was missing was the alignment. The default value was causing the rotation to get outside the visible area. Changing alignment to "center" fixed that:
child: Transform(
alignment: FractionalOffset.center,
transform: Matrix4(
1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1,
)..rotateY(180),
child: Lottie(
frameRate: FrameRate.max,
composition: _composition,
controller: _controller,
),
Simply wrap your widget with RotatedBox & give quarterTurns value(0, 1, 2, 3)
Assuming my widget is image of right arrow : →
0 no rotation - →
1 90 rotation - ↓
2 180 rotation - ←
3 270 rotation - ↑
So if I want my image to face left side ←:
RotatedBox(
quarterTurns: 2,
child: Image.asset('assets/right_arrow.png'),
),
EDIT
To rotate on axis, you need to use Transform widget.
Give the transform property as Matrix4
NOTE: You have to give rotation in radians.
import 'dart:math' as math;
Transform(
// to mirror, rotate Y axis
// math.pi is 180 degree in radian
transform: Matrix4.rotationY(math.pi),
child: Text('Hello world!'),
),

How to make the application display correctly on various screens?

The application looks good on a Pixel 4 XL (resolution of 1440 x 3040) but is overflowing on a Samsung A5 (720 x 1280).
I expected the components to be scaled based on the resolution/ppi, but it seems that I misunderstood.
So how can I fix this ?
Any help is greatly appreciated.
Thanks.
One way of achieving a great looking UI on different screen sizes is to size your widgets relatively to the screen size, and not with pixel amounts, since when the screen's width or height is smaller or bigger, and the widgets are the same size, the UI will either go out of bounds or you will have empty spaces in your UI.
how to achieve that:
in your build function you can store the screen size in a variable:
Widget build(BuildContext context) {
final screenSize = MediaQuery.of(context).size;
...
then in your widgets that you are returning in the build function, for example a container, change the pixel values to values relative to the screen size:
instead of
return Container(width: 50, height: 70);
write stuff like:
return Container(width: screenSize.width * 0.1,
height: screenSize.height * 0.12);

How to locate list of widgets along the curve in Flutter?

Please tell me, is it possible to locate list of widgets along the curve? Like on picture, Let's say teeth are widgets.
If you don't want to use Stack, then you might try out the CustomPaint widget and drawing in flutter with paths.
There's a tutorial to painting: https://medium.com/flutter-community/paths-in-flutter-a-visual-guide-6c906464dcd0
However, painting is a limited thing and if you have an actual widgets that need to be fully responsive and act as a widgets, you need to use Stack with Positioned. That's the only way to go.
Also, in this question about animating along the curve in flutter you can find some code examples that use both: painting and stack and even animate that. Possibly the most useful code from there is calculating the position of an object that you want to place along some curve:
Offset calculate(path, along) {
PathMetrics pathMetrics = path.computeMetrics();
PathMetric pathMetric = pathMetrics.elementAt(0);
along = pathMetric.length * along;
Tangent pos = pathMetric.getTangentForOffset(along);
return pos.position;
}
In above example you need to have some Path which is a representation of some curve. For example you can get one like this:
Path getPath(){
Size size = Size(300,300);
Path path = Path();
path.moveTo(0, size.height / 2);
path.quadraticBezierTo(size.width / 2, size.height, size.width, size.height / 2);
return path;
}
then you give this path and some along, which is number between 0 and 1, which represents beginning and ending of this curve and you give it to the calculate, which return and Offset, which you can use in your Positioned inside your Stack:
Positioned(
top: calculate(path, along).dy,
left: calculate(path, along).dx,
child: ...
When you only want some shapes to be drawn on a screen then you can use simply painting with CustomPaint widget, but this objects won't be widgets

What is delta, globalPosition, localPosition and primaryDelta in Flutter?

I am struggling to understand the properties delta, globalPosition, localPosition and primaryDelta of DragUpdateDetails. I read the documentation for DragUpdateDetails but it didn't really help much.
I found some question asked: What is primaryDelta and delta in DragUpdateDetails and What is the difference between globalposition and localposition in flutter?. First one has no answer whereas second one only has explanation for globalPosition and localPosition.
This is a sample code for dragging a container:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
double _xPosition = 0;
double _yPosition = 0;
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: SafeArea(
child: Container(
child: Stack(
children: [
Positioned(
top: _yPosition,
left: _xPosition,
child: GestureDetector(
onPanUpdate: (DragUpdateDetails e) {
setState(() {
_xPosition += e.delta.dx;
_yPosition += e.delta.dy;
});
},
child: Container(
height: 200,
width: 200,
color: Colors.black,
),
),
)
],
),
),
),
),
);
}
}
In this example why are we using delta but not globalPosition or localPosition. I mean of course dragging will not work as expected but how to know when to use delta or globalPosition or localPosition?
Can someone provide me a quick explanation for these properties? It would really mean a lot. Thanks!
According to the Source code of DragUpdateDetailsclass:
delta → Offset
The amount the pointer has moved in the coordinate space of the event receiver since the previous update
Meaning, The distance covered by dragging since the last pointer contact. Delta gives dx for horizontal distance and dy for vertical distance.
primaryDelta → double
The amount the pointer has moved along the primary axis in the coordinate space of the event receiver since the previous update
primaryDelta gives the absolute distance in only one primary direction of dragging, meaning if the drag is primarily in horizontal axis(GestureDragUpdateCallback + Horizontal only) then this value represents the drag distance in the horizontal axis. If the drag in is vertical axis (GestureDragUpdateCallback + Vertical only) then this value represents the drag amount in the vertical axis.
Note: if the GestureDragUpdateCallback is for a two-dimensional drag (e.g., a pan), then this value is null.
globalPosition → Offset
The pointer's global position when it triggered this update.
The position of the pointer on the screen with reference to the whole screen area and origin point at the top-left corner of the screen. Global Position gives x for horizontal co-ordinate and y for vertical co-ordinate
localPosition → Offset
The local position in the coordinate system of the event receiver at which the pointer contacted the screen.
The position of the pointer just like the global position, except the referential frame being the widget/render object instead of the whole screen. Here, The widget is the one that has received the pointer contact.
Explanation with respect to the example code
According to the example code that is provided, I can safely say that when you are dealing with the positioning of any draggable widget you must have an initial position.
For the initial position, globalPosition or localPosition can be used. Which one to use is specific to the widget tree and use of the app.
Once the initial position is set, you can use delta or primaryDelta to find the new position for the draggable widget to move to when dragged by following formula:
newXPosition = initialXPosition + (dx or primaryDelta in horizontal axis)
newYPosition = initialYPosition + (dy or primaryDelta in vertical axis)
One thing to keep in mind while using delta and primaryDelta is that, if the widget can/should recognize the drag events in both axis, only delta will be provided, primaryDelat will be null.
Otherwise, if only one direction drag is expected then using only primaryDelta will work as expected, at this point delta will have only one value as a non-zero value and other as 0 based on which direction the drag is to be recognized.
Let me know if you have any other questions about this in the comments.