Flutter hold splash screen for 3 Seconds. How to implement splash screen in Flutter?

前端 未结 9 2004
后悔当初
后悔当初 2021-02-16 00:16

How to show splash screen in flutter for 3 seconds and then go next my login screen.

I have tried.countdowntimer but import is unresolved

import \'pack         


        
9条回答
  •  醉梦人生
    2021-02-16 00:30

    Future.delayed would be a good solution without a countdown.

    But considering you have a countdown, you can use the animation framework Flutter provides.

    The idea behind it would be to use an AnimationController with a duration of 3 seconds. Start the animation as soon as the splashScreen is instantiated. And add a listener to redirect to /login on animation end.

    Then pass that controller to an AnimationBuilder which would handle the formating of your countdown based on animationController.lastElaspedDuration.

    class SplashScreen extends StatefulWidget {
      final Duration duration;
    
      const SplashScreen({this.duration});
    
      @override
      _SplashScreenState createState() => new _SplashScreenState();
    }
    
    class _SplashScreenState extends State with SingleTickerProviderStateMixin {
      AnimationController animationController;
    
      @override
      void initState() {
        animationController = new AnimationController(duration: widget.duration, vsync: this)
          ..forward()
          ..addStatusListener((status) {
            if (status == AnimationStatus.completed) {
              Navigator.pushReplacementNamed(context, '/login');
            }
          });
        super.initState();
      }
    
      @override
      Widget build(BuildContext context) {
        return new AnimatedBuilder(
          animation: animationController,
          builder: (context, _) {
            return new Center(
              child: new Text(animationController.lastElapsedDuration.inSeconds.toString()),
            );
          },
        );
      }
    }
    

提交回复
热议问题