import 'package:flutter/material.dart';
import 'util/api.dart';
import 'util/util.dart';
import 'util/session.dart';
import 'util/models.dart';
import 'login_page.dart';
import 'home_page.dart';
class StartPage extends StatefulWidget {
  final Widget child;

  StartPage({
    Key key,
    this.child
  }): super(key: key);

  _StartPageState createState() => _StartPageState();
}

class _StartPageState extends State < StartPage > {

  @override
  void initState() {
    super.initState();
    getKey('token').then((token) {
      print(token);
      if (token != null) {
        print('token存在去验证token');
        checkToken(token).then((resp) {
          if (isNotError(context, resp)) {
            print('获得toekn,跳转主页');
            CheckToken token = CheckToken.fromJson(resp.data);
            setKey('token', token.data);
            Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) {
              return HomePage();
            }));
          } else {
            print('token错误，跳登录页');
            Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) {
              return LoginPage();
            }));
          }
        });
      } else {
        print('token不存在跳转登录页');
        Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) {
          return LoginPage();
        }));
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Container(
        child: Image.asset(
          "lib/images/start_page.png", //本地图片路径必须和配置文件的图片路径一致
          fit: BoxFit.cover
        )
      ),
    );
  }
}