flutter设置启动页和欢迎页

2022-01-07  本文已影响0人  男人宫
import 'package:flutter/material.dart';
import 'show_tools.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

class StartPage extends StatefulWidget {
  //启动页
  @override
  _StartPageState createState() => _StartPageState();
}

class _StartPageState extends State<StartPage> {
  bool _islogin = false;
  bool _isFirst =false;
  bool _islastPage = false;
  List _welcomeList = ["welcome_1.png","welcome_2.png","welcome_3.png"];
  @override
  void initState() {
    super.initState();
    //判断是不是第一次启动
    _isFirst = SpUtil.getBool("first");
    if(_isFirst){
      String tokenStr = SpUtil.getString("token");
      _islogin = (tokenStr == null || tokenStr.length == 0) ? false : true;
      new Future.delayed(Duration(seconds: 1), () {
        //1秒后跳转到其他路由
        Navigator.of(context).pushReplacementNamed(_islogin ? "/" : "/login");
      });
    }

  }

  @override
  Widget build(BuildContext context) {
    return Container(
        color: Colors.white,
        child: _isFirst ? Image.asset("assets/startiamg/m_splash.png", fit: BoxFit.fill)
          : Stack(
          children: [
            Swiper(
              itemBuilder: (BuildContext context, int index) {
                print(index);
                return new Image.asset(
                  "assets/welcomeimage/${_welcomeList[index]}",
                  fit: BoxFit.fill,
                );
              },
              itemCount: _welcomeList.length,
              loop: false,
              itemWidth: kWith,
              itemHeight: kHeight,
              onIndexChanged:(index){
              if(index == _welcomeList.length - 1){
                setState(() {
                  _islastPage = true;
                });
              }else{
                setState(() {
                  _islastPage = false;
                });
              }
               },
            ),
            _islastPage ? Container(
              width: 100,
              height: 40,
              margin: EdgeInsets.only(left: kWith/2-50,bottom: 40,top: kHeight - 80,right: kWith/2-50),
              decoration: BoxDecoration(
                border: Border.all(color: Colors.white,width: 1),
              ),
              child: Center(
                child: TextButton(
                  onPressed: (){
                    _goWelcome();
                  },
                  child: Text("立即体验",style: TextStyle(color: Colors.white,fontSize: 15,decoration: TextDecoration.none,),),
                ),
              ),
            ):Container()
          ],
        ));
  }

  _goWelcome(){
    SpTools.saveBool("first", true);
    String tokenStr = SpUtil.getString("token");
    _islogin = (tokenStr == null || tokenStr.length == 0) ? false : true;
    Navigator.of(context).pushReplacementNamed(_islogin ? "/" : "/login");
  }

}

上一篇 下一篇

猜你喜欢

热点阅读