0

i want to switch between two screens continously, i used AnimatedCrossFade but it switch them once,

home: const AnimatedCrossFade(
              duration: Duration(seconds: 2),
              crossFadeState: CrossFadeState.showSecond,
              firstChild: WelcomeScreenIron(),
              secondChild: WelcomeScreenViper(),
            ),

what i need is once i run my app the two first screen appears then the second repeatdly without pressing any buttons how can i do thi using animation?

1 Answers1

1

You can use a Timer.periodic. to switch between children. Place HomePage on home:

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  bool isFirstChild = false;

  Timer? timer;

  @override
  void initState() {
    super.initState();

    timer = Timer.periodic(Duration(seconds: 2), (timer) {
      setState(() {
        isFirstChild = !isFirstChild;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: AnimatedCrossFade(
        duration: Duration(seconds: 2),
        crossFadeState:
            isFirstChild ? CrossFadeState.showSecond : CrossFadeState.showFirst,
         firstChild: WelcomeScreenIron(),
         secondChild: WelcomeScreenViper(),
      ),
    );
  }
}
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56