Open In App

Flutter – AnimatedCrossFade Widget

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.

 

Constructor

AnimatedCrossFade
    ({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
})

Properties

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



Example

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.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: GFG(),
      debugShowCheckedModeBanner: false,
    );
  }
}
 
class GFG extends StatefulWidget {
  const GFG({Key? key}) : super(key: key);
 
  @override
  State<GFG> createState() => _GFGState();
}
 
class _GFGState extends State<GFG> with TickerProviderStateMixin {
  late AnimationController _controller;
  bool _bool = true;
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("GeeksForGeeks"),
        actions: [
          // Creating a button to
          // switch between two widgets
          TextButton(
            onPressed: () {
              setState(
                () {
                  _bool = !_bool;
                },
              );
            },
            child: Text("Switch", style: TextStyle(color: Colors.white)),
          ),
        ],
      ),
      body: Center(
        child: AnimatedCrossFade(
            // First widget
            firstChild: Container(
              height: 230,
              width: 250,
              color: Colors.blue,
            ),
            // Second widget
            secondChild: Container(
              height: 250,
              width: 230,
              color: Colors.green,
            ),
            // Parameter to change between two
            // widgets on this basis of value of _bool
            crossFadeState:
                _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.



Output:


Article Tags :