Skip to content
Related Articles

Related Articles

Flutter – AnimatedCrossFade Widget

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 27 Apr, 2022

AnimatedCrossFade Widget creates a fade transition between two widgets when one widget is replaced by another. It is used when you need to give a fade kind of transition in between two widgets. It supports any kind of Flutter Widget like Text, Images, Icon as well as anything that is extended from the widget in Flutter. Also, it allows you to change the animation as well as the duration of the animation. A sample GIF is given below to get an idea about what we are going to do in this article.

Flutter - AnimatedCrossFade Widget



    ({Key? key, 
    required Widget firstChild, 
    required Widget secondChild, 
    Curve firstCurve = Curves.linear, 
    Curve secondCurve = Curves.linear, 
    Curve sizeCurve = Curves.linear, 
    AlignmentGeometry alignment = Alignment.topCenter, 
    required CrossFadeState crossFadeState, 
    required Duration duration, 
    Duration? reverseDuration, 
    AnimatedCrossFadeBuilder layoutBuilder = defaultLayoutBuilder


  • alignment: How the children should be aligned while the size is animating.  
  • crossFadeState: The child that will be shown when the animation has been completed.
  • duration: The duration of the whole orchestrated animation.
  • firstChild: The child that is visible when crossFadeState is CrossFadeState.showFirst. It fades out when transitioning crossFadeState from CrossFadeState.showFirst to CrossFadeState.showSecond and vice versa.
  • firstCurve: The fade curve of the first child.
  • hashCode: The hash code for this object. 
  • key: Controls how one widget replaces another widget in the tree.
    layoutBuilderA builder that positions the firstChild and secondChild widgets. 
  • reverseDuration: The duration of the whole orchestrated animation when running in reverse. 
  • runtimeType: A representation of the runtime type of the object.
  • secondChild: The child that is visible when crossFadeState is CrossFadeState.showSecond. It fades in when transitioning crossFadeState from CrossFadeState.showFirst to CrossFadeState.showSecond and vice versa.
  • secondCurve: The fade curve of the second child.
  • sizeCurve: The curve of the animation between the two children’s sizes. 

Note: firstChild and secondChild have required keywords applied which means these parameters are mandatory to use.


The main.dart file.


import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
void main() {
  runApp(const MyApp());
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  // This widget is the root
  // of your application.
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
      home: GFG(),
      debugShowCheckedModeBanner: false,
class GFG extends StatefulWidget {
  const GFG({Key? key}) : super(key: key);
  State<GFG> createState() => _GFGState();
class _GFGState extends State<GFG> with TickerProviderStateMixin {
  late AnimationController _controller;
  bool _bool = true;
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("GeeksForGeeks"),
        actions: [
          // Creating a button to
          // switch between two widgets
            onPressed: () {
                () {
                  _bool = !_bool;
            child: Text("Switch", style: TextStyle(color: Colors.white)),
      body: Center(
        child: AnimatedCrossFade(
            // First widget
            firstChild: Container(
              height: 230,
              width: 250,
            // Second widget
            secondChild: Container(
              height: 250,
              width: 230,
            // Parameter to change between two
            // widgets on this basis of value of _bool
                _bool ? CrossFadeState.showFirst : CrossFadeState.showSecond,
            // Duration for crossFade animation.
            duration: Duration(seconds: 1)),

Note: The firstChild and secondChild can be any widget as long as they extend the class Widget.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!