import 'package:connectivity/connectivity.dart';
import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/pages/home_page/home_page.dart';
import 'package:zhiying_comm/util/application.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

// 启动页，页面初始化等操作~跟原生启动页保持一致，防止白屏问题
class LaunchPage extends StatefulWidget {
  final WidgetBuilder builder;

  const LaunchPage({Key key, this.builder}) : super(key: key);

  @override
  _LaunchPageState createState() => _LaunchPageState();
}

class _LaunchPageState extends State<LaunchPage> {
  @override
  void initState() {
    Connectivity().checkConnectivity().then((connectivityResult) {
      if (connectivityResult == ConnectivityResult.mobile ||
          connectivityResult == ConnectivityResult.wifi) {
        // I am connected to a wifi network.
        Logger.debug('ConnectivityResult.wifi');
        print('ConnectivityResult.wifi');
        Application.init().then((_) {
          if (widget.builder != null) {
            Navigator.of(context)
                .pushReplacement(MaterialPageRoute(builder: widget.builder));
          } else {
            Navigator.of(context).pushReplacement(
                MaterialPageRoute(builder: (context) => HomePage()));
          }
        }).catchError((err) {
          Logger.error(err);
        });
      } else {
        Logger.error('未知网络，APP初始化失败');
        print('unknow');
      }
    });

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(child: Text('这个是启动页，正在初始化...')),
    );
  }
}
