Flutter: Fade widgets in after navigation - flutter

Consider two routes, A and B. After navigating from A to B, how can I get the widgets in B to fade in (go from opacity 0 to 1) gradually?

It seems like you what you are looking for is a PageRouteBuilder with a custom transition, like so:
return Navigator.of(context).push(PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) {
return HomePage();
},
transitionDuration: Duration(milliseconds: 500),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
return FadeTransition(
opacity: animation,
child: child,
);
}
));

Related

Flutter push screen with pop animation

i have the code below and what i would like to achieve is to have an animation transition like 'pop' when calling 'push'. the code that i have just slides the new page from the direction that i specify. i would like to have the old screen be the one that is moving instead.
import 'package:flutter/material.dart';
class CustomPageRoute extends PageRouteBuilder {
final Widget child;
final AxisDirection direction;
CustomPageRoute({
required this.child,
required this.direction,
}) : super(
transitionDuration: const Duration(milliseconds: 200),
reverseTransitionDuration: const Duration(milliseconds: 200),
pageBuilder: (context, animation, secondaryAnimation) => child,
);
#override
Widget buildTransitions(BuildContext context, Animation<double> animation,
Animation<double> secondaryAnimation, Widget child) =>
SlideTransition(
position: Tween<Offset>(begin: getBeginOffset(), end: Offset.zero)
.animate(animation),
child: child,
);
Offset getBeginOffset() {
switch (direction) {
case AxisDirection.up:
return const Offset(0, 1);
case AxisDirection.down:
return const Offset(0, -1);
case AxisDirection.right:
return const Offset(-1, 0);
case AxisDirection.left:
return const Offset(1, 0);
}
}
}
You can use pageRouteBuilder with zero duration
Navigator.pushReplacement(
context,
PageRouteBuilder(
pageBuilder: (context, _, __) => NewPage(),
transitionDuration: Duration.zero,
reverseTransitionDuration: Duration.zero,
),
);
Edit
Navigator.push(
context,
PageRouteBuilder(
pageBuilder: (context, _, __) => Page2(),
transitionsBuilder: (context, anim, a2, child) => FadeTransition(opacity: anim, child: child),
transitionDuration: Duration(milliseconds: 2000),
),
);
for anyone looking to achieve the same thing, it can be achieved by using popUntil

How can I get this type of transition in flutter?

I want to get this type of transition animation .
But when I use
class CustomPageTransitionBuilder extends PageTransitionsBuilder {
#override
Widget buildTransitions<T>(
PageRoute<T> route,
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child,
) {
double begin = 0;
double end = 1.0;
var curve = Curves.easeOut;
final tween = Tween(
begin: begin,
end: end,
).chain(CurveTween(
curve: curve,
));
final scaleAnimation = animation.drive(tween);
if (route.settings.name == '/') {
return child;
}
return ScaleTransition(
scale: scaleAnimation,
child: child,
);
}
}
and in Main.dart:
MaterialApp(
theme: ThemeData(
pageTreansitionsTheme: PageTransitionsTheme( builders: {
TargetPlatform.android: CustomPageTransitionBuilder()
}
)
)
I get this type of animation:
Its somehow okay while navigating to the page . But when I go back I see weird animation effect.
Try this Code for Navigation
Navigator.push(
context,
PageRouteBuilder(
transitionDuration: const Duration(seconds: 1),
transitionsBuilder: (BuildContext context,
Animation<double> animation,
Animation<double> secAnimation,
Widget child) {
animation = CurvedAnimation(
parent: animation, curve: Curves.elasticOut);
return ScaleTransition(
scale: animation,
alignment: Alignment.center,
child: child);
},
pageBuilder: (BuildContext context,
Animation<double> animation,
Animation<double> secAnimation) {
return SecondScreen();
}));
You can do it using Hero animation.

How to get this type of page transition animation in flutter?

I want to get this type of page transition effect in flutter. I tried all the packages found on pub.dev but I didn't get this one.
Try this:
Route scaleIn(Widget page) {
return PageRouteBuilder(
transitionDuration: const Duration(milliseconds: 300),
pageBuilder: (context, animation, secondaryAnimation) => page,
transitionsBuilder: (context, animation, secondaryAnimation, page) {
var begin = 0.0;
var end = 1.0;
var curve = Curves.ease;
var tween = Tween(begin: begin, end: end).chain(CurveTween(curve: curve));
return ScaleTransition(
scale: animation.drive(tween),
child: page,
);
},
);
}
And use it like so:
Navigator.push(context, scaleIn(NewPage()));

Flutter PageTransitionSwitcher is not working

I want to add an animation when transitioning from user's profile page to the previous page. so I used PageTransitionSwitcher as already I'm using animations package for another purpose in the app.This is how I implemented it inside the back button of the page.When I click on back button it is not working.
onPress: () => {
PageTransitionSwitcher(
transitionBuilder: (child, animation, secondaryAnimation) =>
SharedAxisTransition(animation: animation, secondaryAnimation: secondaryAnimation,
transitionType: SharedAxisTransitionType.horizontal),
child: NewMainTabs(),
)
},
you can use this function to build the route.
import 'package:animations/animations.dart';
import 'package:flutter/material.dart';
Route buildSharedZAxisTransitionPageRoute(Widget Function(BuildContext) builder,{RouteSettings? settings}) {
return PageRouteBuilder(
settings: settings,
pageBuilder: (context, animation, secondaryAnimation) => builder(context),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
return SharedAxisTransition(
fillColor: Theme.of(context).cardColor,
animation: animation,
secondaryAnimation: secondaryAnimation,
transitionType: SharedAxisTransitionType.scaled,
child: child,
);
});
}
Then you can call upper function to create your route and push it into navigator or just rebuild with another widget
Widget build(BuildContext context) {
return PageTransitionSwitcher(
transitionBuilder: (
Widget child,
Animation<double> primaryAnimation,
Animation<double> secondaryAnimation,
) {
return FadeThroughTransition(
child: child,
animation: primaryAnimation,
secondaryAnimation: secondaryAnimation,
);
},
child: MyWidget(),
);
or
Navigator.of(context, rootNavigator: true).push(
buildSharedZAxisTransitionPageRoute(
(_) => const MyWidget())

Flutter CupertinoPageRoute duration

How do I add a duration to the CupertinoPageRoute? Currently it slides too quick and the effect is not very good.
Navigator.push(
context,
CupertinoPageRoute<Null>(
builder: (context) => View(),
),
);
My solution was not via CupertinoPageRoute, but maybe it can help.
Create a Route that you can customise:
Route yourCustomRoute() {
return PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) => YourView(),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
var begin = Offset(-0.1, -0.1); //start position from top and left corner f.e.
var end = Offset.zero;
var curve = Curves.ease;
var tween =
Tween(begin: begin, end: end).chain(CurveTween(curve: curve));
return SlideTransition(
position: animation.drive(tween),
child: child,
);
},
transitionDuration: Duration(seconds: 10) //any duration you want
);
}
Add your route to Navigator:
Navigator.of(context).push(yourCustomRoute());
Read about routing between two pages here: https://flutter.dev/docs/cookbook/animation/page-route-animation