Animation In Flutter: AnimatedCrossFade
Fade animation between two different widgets and their sizes.

Search for a command to run...
Fade animation between two different widgets and their sizes.

No comments yet. Be the first to comment.
Lessons from Code, Content, and Consistency

Reliability basics: Outbox pattern + why it matters

Introducing Kafka/Redpanda + move to event-driven workflow

Connect Orders โ Inventory (first working service-to-service flow)

Inventory service + gRPC + proto contracts


Show Me button is very quick. It's not smooth.AnimatedCrossFade widget in the bodyScaffold(
backgroundColor: Colors.blueGrey,
appBar: AppBar(
title: const Text("AnimatedCrossFade"),
),
body: Center(
child: AnimatedCrossFade(
// ....
)
)
children widgets that as you might have guessed, firstChild and secondChild.Spoiler Alert widget to the firstChild property because it's the first child we're showing to the user.AnimatedCrossFade(
firstChild: GestureDetector(
onTap: () {
setState(() {
isVisible = true;
});
},
child: Container(
width: 300,
height: 400,
color: Colors.black,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
"Spoiler Alert",
style: TextStyle(fontSize: 18, color: Colors.white),
),
const SizedBox(
height: 20,
),
OutlinedButton(
onPressed: () {
setState(() {
isVisible = true;
});
},
child: const Text("Show me"))
],
),
),
),
)
)
secondChild property.secondChild: GestureDetector(
onTap: () {
setState(() {
isVisible = false;
});
},
child: Container(
width: 300,
height: 400,
color: Colors.blueGrey,
child: Image.network(
"https://memegenerator.net/img/instances/60138700.jpg")
),
),
required properties i.e -durationduration to the AnimatedCrossFade widget. Which is used to define for how long the animation will fade from first to a second child.1 second AnimatedCrossFade(
duration: const Duration(seconds: 1),
firstChild : //...
secondChild: //....
)
crossFadeStateThe last required property is crossFadeState. It is as you might have guessed, the state, which determines whether to show firstChild or secondChild
If it isCrossFadeState.showFirst then, when the animation completes the firstChild will be shown and vice versa
boolean.bool showPost = false;
crossFadeState property add the below code :crossFadeState:
!showPost ? CrossFadeState.showFirst : CrossFadeState.showSecond,

layoutBuilder :width and height of the Post Image. secondChild: GestureDetector(
onTap: () {
setState(() {
showPost = false;
});
},
child: Container(
width: 200, // <- here
height: 300, // <- here
color: Colors.blueGrey,
child: Image.network(
"https://memegenerator.net/img/instances/60138700.jpg"
),
),
),
firstChild state to secondChild state.
height and width. We have to use layoutbuilder of the AnimatedCrossFade widget.4 parameters i.e :topChild, topChildKey, bottomChild, bottomChildKey. layoutbuilder will return a Stack widget with two Positioned widgets. The first Positioned widget will take the bottomChild widget and the second Positioned widget takes the topChild widget.alignment center and clipBehaviour to Clip.none. Otherwise, you'll not get the expected result. AnimatedCrossFade(
layoutBuilder:
(topChild, topChildKey, bottomChild, bottomChildKey) =>
Stack(
clipBehavior: Clip.none,
alignment: Alignment.center,
children: [
Positioned(
key: bottomChildKey,
child: bottomChild,
top: 0.0,
),
Positioned(
key: topChildKey,
child: topChild,
),
],
),
//....
)


