flutter 启动图

作者: 8521477ac444 | 来源:发表于2018-11-05 14:21 被阅读276次

示例代码

class StartPage extends StatefulWidget {

  @override
  _StartPageState createState() => new _StartPageState();

}

class _StartPageState extends State<StartPage> {

  var timer;
  num tnum = 5;

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

  @override
  void dispose() {
    super.dispose();
    timer.cancel();
  }

  renderPage() {

    if(auth) {

      Navigator.of(context).pushAndRemoveUntil(
        new MaterialPageRoute( builder: (BuildContext context) => new LoginWidget()), (Route route) => route == null);
        
    } else {
      
      Navigator.of(context).pushAndRemoveUntil(
        new MaterialPageRoute( builder: (BuildContext context) => new HomePage()),  (Route route) => route == null);
      
    }

  }

  initTnum() {
    print('--------- 初始化页面 -----------');

    timer = Timer.periodic(
        new Duration(milliseconds: 1000), (Void) {
          if(tnum <= 0) {
            setState(() {
              auth = true;
            });
            renderPage();
            // (timer as Timer).cancel();
          } else {
            setState(() {
              tnum = tnum; 
            });
            tnum--;
          }
          // print(tnum);
        }
    );

  }


  @override
  Widget build(BuildContext context) {
    print(this.tnum.toString());
    return new Scaffold(
      body: new Stack(
        // fit: ,
        children: <Widget>[
          new Image.asset(
            "assets/images/start.png", 
            width: MediaQuery.of(context).size.width,
            height: MediaQuery.of(context).size.height,
            fit: BoxFit.cover,
          ),
          new Positioned(
            // left: 0.0,
            right: 20.0,
            top: 20.0,
            child: new Text(
              '跳转${tnum.toString()}',
              style: new TextStyle(
                color: Colors.black26,
                fontSize: 12.0,
                fontFamily: 'serif',
              ),
            ),
          )
        ]
      )
    );
  }

}

相关文章

网友评论

    本文标题:flutter 启动图

    本文链接:https://www.haomeiwen.com/subject/wtwcxqtx.html