提问者:小点点

如何在Flutter应用程序中添加虚假加载?[重复]


我的应用程序不需要加载任何内容,但我想给它一个2秒的加载屏幕以进行可视化操作。我该怎么做?


共1个答案

匿名用户

创建一个DummyPage小部件,它会在2秒后将您带到YourHomePage(您的应用程序的主要小部件)。

更新:我之前使用了Timer,但它需要导入额外的库,您可以按照@anmol. majhail的建议使用Future.delay

void main() {
  runApp(MaterialApp(home: DummyPage()));
}

class DummyPage extends StatefulWidget {
  @override
  _DummyPageState createState() => _DummyPageState();
}

class _DummyPageState extends State<DummyPage> {
  @override
  void initState() {
    super.initState();
    // here is the logic 
    Future.delayed(Duration(seconds: 2)).then((__) {
      Navigator.push(context, MaterialPageRoute(builder: (_) => YourHomePage()));
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(); // this widget stays here for 2 seconds, you can show your app logo here
  }
}