How to show a new screen for 10 seconds only: Flutter

418 Views Asked by At

I am trying to show a screen in flutter which should appear only for 10 seconds and then should disapper going back to previous screen. How this should be done in the correct manner in flutter. Is it possible to use timer.periodic in Flutter?

2

There are 2 best solutions below

0
Kaushik Chandru On

In the initstate of the screen that you wish to close add

Future.delayed(Duration(seconds: 10), (){
  //Navigator.pushNamed("routeName");
  Navigator.pop(context);
 });
0
Md. Yeasin Sheikh On

You can create new screen with Future.delayed inside initState

class NewPage extends StatefulWidget {
  NewPage({Key? key}) : super(key: key);

  @override
  State<NewPage> createState() => _NewPageState();
}

class _NewPageState extends State<NewPage> {
  @override
  void initState() {
    super.initState();
    Future.delayed(Duration(seconds: 10)).then((value) {
      Navigator.of(context).pop();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
    );
  }
}