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.
Related
I want to place a draggable widget (a Helper marker) on the map's marker click to simply change the location of the marker by dragging the helper marker. The screen Offset - dx,dy and gMap's ScreenCordinates - dx, dy are different.
var pos = await _controller!.getScreenCoordinate(latLng); gives some coordinates, if i give the coordinates to a Positioned ( top: pos.dy, left: pos.dx, child: helperMarker) like this, the positioned widget is not placed on my expected location.
Am new to flutter and i need to get x axis y axis value of particular place of the image.i have put the image in container and i need to mark one corner of the container image position as (0,0) when I click the any part or place of the image I need to get x axis and y axis value of the clicked position. I don't know how to do it. Please any one help me to do that. Thanks in advance
Use GestureDetector to track mouse position.
// variables (or states based on your needs) to store positions
double x = 0.0;
double y = 0.0;
// a function to update mouse pointer position (location)
void updatePosition(TapDownDetails details) {
setState(() {
x = details.globalPosition.dx;
y = details.globalPosition.dy;
});
}
// image widget
GestureDetector(
onTapDown: updatePosition,
child: Image.asset(yourImagePathHere)), <-- change it to your image path
),
GestureDetector(
onVerticalDragUpdate: (details) {
var dy = details.delta.dy;
var primaryDy = details.primaryDelta;
},
)
I couldn't find out the difference between a regular delta and a primary, both seems to do the same job. Can anyone explain the difference between these two deltas? (As usual Docs are not very clear, at least to me)
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.
I am creating a ruler app where I can measure anything in millimetre.
Something similar to this app https://play.google.com/store/apps/details?id=org.nixgame.ruler&hl=en_IN but very basic.
I am repeating the Divider widget into a Column widget and keeping a gap of 6.299 as given.
class Ruler extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: rulerPin(350),
),
);
}
List<Divider> rulerPin(int count) {
return List.generate(count, (i) {
return Divider(
height: 6.299,
thickness: 1,
);
}).toList();
}
}
But the problem is when I measure with the physical ruler on my mobile phone the values do not match. Check the given screenshots.
I am using this reference Calculate logical pixels from millimeters
Kindly suggest if I am following the correct approach.
You have to find out the pixel frequency of the device. Then with MediaQuery you have to add the pins at equal intervals calculated. There may always be shifts in calculations with fixed numbers.
If it does not happen again, it definitely means that there are errors in the parts that give information.(Like pixel frequency information).
I will research for you and edit this answer.
Result: You can calculate in 3 stage;
1) Get DPI (dots per inc)
double dpi = _getDpi();
android : getting the screen density programmatically in android? with method channel
iOS : You can create a dpi list of apple device models.(device list) They are not too much. And then you can get device model with device_info
2 ) Calculate pixel size
Flutter work with logical pixels.
So we need known how many logical and how many physical pixels
dart
Size logicalSize = MediaQuery.of(context).size;
///How many physical pixel for 1 logical pixel
double pixelRatio = MediaQuery.of(context).devicePixelRatio;
///How many logical pixel for 1 mm.
double pixelCountInMm = dpi / pixelRatio / 25.4;
//e.g output = 7.874015748031496
3 ) Calculate
///I am not sure thickness include height
/// if yes you can set divider height : pixelCountInMm - tickness
List<Divider> rulerPin(int count) {
return List.generate(count, (i) {
return Divider(
height: pixelCountInMm,
thickness: 1,
);
}).toList();
}
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.