Animate in and out a widget when some data changes in flutter - flutter

I have a widget that is displaying data, which changes over time, i'd like for that old data to fade out, then fade in new data when its available (passed in by the parent).
How can i achieve this?

I think this might help u:
AnimatedOpacity(
opacity: _visible ? 1.0 : 0.0,
duration: Duration(milliseconds: 500),
child: Text('Hello World!),
);

Related

Callback for when an AnimatedFoo widget has finished animating

I was wondering if there is a way to know once an AnimatedFoo widget has completed the animation? I'm currently using AnimatedSize and am trying to find a way to call a function once it has completed. I initially also created an AnimatedController but it doesn't look like it's accepting any controllers.
AnimatedSize(
duration: const Duration(milliseconds: 500),
curve: Curves.bounceOut,
child: Container(...),
),

Flutter current screen out, new in Animation

I need to animate current screen A out, then screen B in. PageRouteBuilder, transition:
#override
Widget buildTransitions(
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child,
) {
var animCurrentWidget = Tween<Offset>(begin: const Offset(0, 0), end: const Offset(0, 1))
.chain(CurveTween(curve: const Interval(0, 0.5, curve: Curves.linear)))
.animate(animation);
var animNewWidget = Tween<Offset>(begin: const Offset(0, 1), end: const Offset(0, 0))
.chain(CurveTween(curve: const Interval(0.5, 1, curve: Curves.linear)))
.animate(animation);
return Stack(
children: <Widget>[
SlideTransition(position: animCurrentWidget, child: parent, transformHitTests: false),
SlideTransition(position: animNewWidget, child: child),
],
);
}
Pretty simple, current screen leaves with up->down, new one goes in with sliding down->up after first one left.
But the tricky part is that you need to have access to current Widget on screen to do that, which removes use of
onGenerateRoute: MyCustomRouting.generateRoute
And forces me to work with routes in local scope of every possible screen app can have.
What are the best practices for tasks like that?
Is there a way to get current widget inside MyCustomRouting.generateRoute without going full static?
Is there a way to get current 'on top' widget inside other widget in the tree?

How to do page view with cross fade animation

I am making a carousel with cross fade animation which I am using AnimatedSwitcher with FadeTransition to achieve such effect. The effect is fine but I want the carousel to be swipeable. I had tried PageView but I do not want the slide animation. Does anyone have any solution to this? Please Help. Thanks in advance.
The custom carousel I currently use.
AnimatedSwitcher(
duration: const Duration(milliseconds: 500),
transitionBuilder: (Widget child, Animation<double> animation) {
return FadeTransition(child: child, opacity: animation);
},
child: Image.asset(
imageList[currentIndex.toInt()],
key: ValueKey<int>(currentIndex),
),
If you do not want slide animation, do not use PageView.
Stick with your current implementation with AnimationSwitcher. If you want to add gesture support, wrap it with GestureDetector and listen on the onHorizontalDrag... or onVerticalDrag... events.

Flutter web - scroll down but horizontal

the effect I want to achieve is that the user scrolls sideways when the user uses the gesture to scroll down. Something like on this page.
ListView does not work with gestures - but it does work with a mouse, but that doesn't solve the problem when the user is using gestures, e.g. on a laptop.
Does anyone have an idea how to handle it?
I did not find any dedicated functionality in some widget, but here is my workaround:
final scrollController = ScrollController();
Listener(
onPointerSignal: (pointerSignal) {
if (pointerSignal is PointerScrollEvent) {
scrollController.animateTo(
scrollController.offset + pointerSignal.scrollDelta.dy,
curve: Curves.decelerate,
duration: const Duration(milliseconds: 200),
);
}
},
child: SingleChildScrollView( // or any other
controller: scrollController,
child: ...,
),
)

Resize Icon every second using GestureDetector's onLongPressStart

This problem is pretty much very similar to messenger's icons. As long as you press that icon, the larger it gets.
This would be fun for others to use soon!
I'm new to flutter but if someone has an idea on how to implement this, I'm very much willing to help.
You can use a ScaleTransition widget that accept animation value
_controller = AnimationController(duration: const Duration(milliseconds: 2000), vsync: this, value: 0.1);
_animation = CurvedAnimation(parent: _controller, curve: Curves.bounceInOut);
ScaleTransition(
scale: _animation,
alignment: Alignment.center,
child: child
)
and in your longPress just call
_controller.forward();