美文网首页
Flutter 启动页面

Flutter 启动页面

作者: 代瑶 | 来源:发表于2020-07-07 14:09 被阅读0次
    image.png

    类似效果如下

    //启动页面
    import 'dart:async';
    
    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    
    import 'IndexPage.dart';
    
    class LaunchPage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return LaunchPageWidget();
      }
    }
    
    class LaunchPageWidget extends StatefulWidget {
      @override
      State<StatefulWidget> createState() => LaunchState();
    }
    
    class LaunchState extends State<LaunchPageWidget> {
      final String launchImage =
          "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1093264713,2279663012&fm=26&gp=0.jpg";
      int _countdown = 5;
      Timer _countdownTimer;
    
      @override
      void initState() {
        super.initState();
        _startRecordTime();
        print('初始化启动页面');
      }
    
      @override
      void dispose() {
        super.dispose();
        print('启动页面结束');
        if (_countdownTimer != null && _countdownTimer.isActive) {
          _countdownTimer.cancel();
          _countdownTimer = null;
        }
      }
    
      void _startRecordTime() {
        _countdownTimer = Timer.periodic(Duration(seconds: 1), (timer) {
          setState(() {
            if (_countdown <= 1) {
    //          Navigator.of(context).pushNamed("/demo1");
              Navigator.of(context).pop();
              Navigator.of(context).push(MaterialPageRoute(builder: (context) {
                return IndexPage();
              }));
              _countdownTimer.cancel();
              _countdownTimer = null;
            } else {
              _countdown -= 1;
            }
          });
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          theme: ThemeData.dark(),
          home: Scaffold(
            body: Stack(
              fit: StackFit.expand,
              children: <Widget>[
                Image.network(launchImage, fit: BoxFit.fill),
                Positioned(
                  top: 30,
                  right: 30,
                  child: Container(
                    padding: EdgeInsets.fromLTRB(5, 2, 5, 2),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(10),
                      color: Colors.black12,
                    ),
                    child: RichText(
                      text: TextSpan(children: <TextSpan>[
                        TextSpan(
                            text: '$_countdown',
                            style: TextStyle(
                              fontSize: 18,
                              color: Colors.blue,
                            )),
                        TextSpan(
                            text: '跳过',
                            style: TextStyle(
                              fontSize: 18,
                              color: Colors.red,
                            )),
                      ]),
                    ),
                  ),
                )
              ],
            ),
          ),
        );
      }
    }
    
    

    相关文章

      网友评论

          本文标题:Flutter 启动页面

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