Created
March 31, 2020 17:47
-
-
Save Raj-Dave368/9d488f9f5f06c9abef24688462bbd4f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// shiv | |
/// om namah shivay✊🏻omNS omNS | |
/// omNS maro BaBUDO maro mahakal | |
import 'package:flutter/material.dart'; | |
import 'dart:ui'; | |
import 'dart:math'; | |
void main() => runApp(MaterialApp(home: OMNS())); | |
class OMNS extends StatefulWidget { | |
@override | |
_OMNSState createState() => _OMNSState(); | |
} | |
class _OMNSState extends State<OMNS> with SingleTickerProviderStateMixin { | |
AnimationController controller; | |
double sum = 0; | |
final double minh = 120; | |
double get maxh => MediaQuery.of(context).size.height; | |
@override | |
void initState() { | |
controller = | |
AnimationController(vsync: this, duration: Duration(seconds: 1)); | |
controller.addListener(() { | |
setState(() { | |
}); | |
}); | |
super.initState(); | |
} | |
void dragup(DragUpdateDetails details) { | |
sum += details.primaryDelta; | |
/* | |
Amazing knowledge by my omNS shiva bholiyo bhandari babo | |
when we set value of controller.value more than it's range then | |
controller.value set to maximum range (set to end value > Tween(begin:...,end: value)) | |
// controller.value = 10000; | |
// print("omNS controller.value = ${controller.value}"); | |
*/ | |
/* | |
1. if we want to understand the how below line work then just | |
restart app, drag from bottom to top without stopping and | |
see the value of sum variable = it see same as maxh | |
so logic is ::: when drag become maxh then make controller.value = 1 | |
[ | |
now, why we want controller.value = 1 because when it is 1 then | |
lerp() will gives maxh, (see lerpDouble() on docs. ) | |
] | |
means | |
when total drag(sum) = "maxh" then controller.value = "1" | |
so when total drag = "k" then controller.value = "?" | |
maxh = 1 | |
k = ? | |
after solving question we got | |
controller.value should be = k*1/maxh; | |
*/ | |
controller.value -= details.primaryDelta / maxh; | |
print(details.primaryDelta / maxh); | |
} | |
void dragend(DragEndDetails details) { | |
double velocityY = details.velocity.pixelsPerSecond.dy; | |
// here velocityY is normally around 2000 so we are dividing it by 1000 (a Random value) | |
// to get small value which should be between | |
if (velocityY < 0) { | |
controller.fling( | |
velocity: max(2, -velocityY / 1000), | |
animationBehavior: AnimationBehavior.preserve); | |
} else if (velocityY > 0) { | |
controller.fling(velocity: min(-2, -velocityY / 1000)); | |
// NOTE: here also we are putting ^ (minus) because as velocity will >0 (as per condition) | |
// se to decrease the controller.value we need to give negative value | |
} else { | |
// now if we are just dragging little bit and then take finger up | |
// then velocity will be 0.0 | |
// so in this case we should increase/decrease value of controller | |
// to the nearest edge(Top/Bottom) so we can get information about nearest edge(up/down) | |
// using controller.value | |
// if it is < 0.5 then we are nearest to BOTTOM else ... | |
controller.fling(velocity: controller.value < 0.5 ? -1.368 : 1.368); | |
} | |
print(".velocity : ${details.velocity}"); | |
print( | |
".velocity.pixelPerSecond.dy : ${details.velocity.pixelsPerSecond.dy}"); | |
} | |
void get() { | |
// by default animation value range between 0 to 1 so we can use controller.status | |
// without using tween | |
bool isfull = controller.status == AnimationStatus.completed; | |
// this is a rate through which animation.value should increases/decrease | |
// twist here is value of animation increase / decrease from current value of animation | |
// if isfull = false then it increase value of animation by 1.3689 unit of maximum value to maximum value | |
// here we have not specify (Tween())maximum value so it will go up to 1 | |
// similarly if iffull = true then .filng() will decrease value by -3 unit of maximum value | |
// until minimum value is not comes | |
// if Tween()/minimum value is not given then it will take the 0 as minimum | |
controller.fling(velocity: isfull ? -3 : 1.3689); | |
/* | |
here try this to understand and also see the value in console | |
controller.fling(velocity: isfull ? -3 :50); | |
and also unable the slow animation option at Flutter Inspector | |
controller.fling = animation.forward() + rate of increasing value is equal to velocity | |
*/ | |
} | |
@override | |
Widget build(BuildContext context) { | |
double height = MediaQuery.of(context).size.height; | |
double width = MediaQuery.of(context).size.width; | |
return Scaffold( | |
body: Container( | |
width: width, | |
height: height, | |
child: Stack( | |
children: [ | |
Positioned( | |
bottom: height / 2, | |
left: width / 2.9, | |
child: Text("Fun With Flutter", style: TextStyle(fontSize: 36, color: Color(0xff000000), fontWeight: FontWeight.bold)), | |
), | |
Positioned( | |
bottom: 0, | |
left: 0, | |
right: 0, | |
child: GestureDetector( | |
onTap: get, | |
onVerticalDragUpdate: dragup, | |
onVerticalDragEnd: dragend, | |
child: Container( | |
alignment: Alignment.center, | |
height: lerpDouble(minh, maxh, controller.value), | |
child: Text("Drag Up or Down", style: TextStyle(fontSize: 36, color: Color(0xff000000), fontWeight: FontWeight.bold)), | |
color: Colors.yellow, | |
), | |
), | |
), | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment