如何在Flutter中使用计时器显示小部件4秒钟

我在BuildWaitingScreen小部件中为初始屏幕创建了UI,我想在名为buildWaitingScreen的小部件中显示初始屏幕4秒钟。我想知道如何给这个小部件一个等待时间吗?是否可以为此或将来延迟使用Timer以获得4秒的延迟?

代码:

Widget buildWaitingScreen() {
      return Scaffold(
          body: Stack(
            fit: StackFit.expand,
            children: <Widget>[
              Container(
                decoration: BoxDecoration(color: Colors.blueAccent),
              ),
              Column(
                mainAxisAlignment: MainAxisAlignment.start,
                children: <Widget>[
                  Expanded(
                    flex: 2,
                    child: Container(
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          CircleAvatar(
                            backgroundColor: Colors.white,
                            radius: 50.0,
                            child: Icon(
                              Icons.directions_car,
                              color: Colors.blue,
                              size: 50.0,
                            ),
                          ),
                          Padding(padding: EdgeInsets.only(top: 10.0),
                          ),
                          Text(
                            "CarWash Locator v.1",
                            style: TextStyle(color: Colors.white,
                                fontSize: 24.0,
                                fontWeight: FontWeight.bold),
                          )
                        ],
                      ),
                    ),
                  ),
                  Expanded(
                    flex: 1,
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        CircularProgressIndicator(),
                        Padding(
                          padding: EdgeInsets.only(top: 20.0),
                        ),
                        Text(
                          "one click! locate car wash!!",
                          style: TextStyle(color: Colors.white,
                              fontSize: 18.0,
                              fontWeight: FontWeight.bold
                          ),
                        )
                      ],
                    ),
                  )
                ],
              )
            ],
          )
      );
  }

  @override
  Widget build(BuildContext context) {
    switch (authStatus) {
      case AuthStatus.NOT_DETERMINED:
        return buildWaitingScreen();
        break;
      case AuthStatus.NOT_LOGGED_IN:
        return new LandingPage(auth: widget.auth,);
        break;
      case AuthStatus.LOGGED_IN:
        if (_userId.length > 0 && _userId != null) {
          return new HomePage(auth: widget.auth,);
        } else
          return  buildWaitingScreen();
        break;
      default:
        return buildWaitingScreen();
    }
  }
}

`

评论
  • jvitae
    jvitae 回复

    用这个。

    Future.delayed(Duration(seconds: 4)).then((value) => {
        // hide your widget
      });