
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';

import '../pages/login.dart';
import '../utils/constants.dart';

// 登陆页面代理 阻止返回按钮
class HomeRoute extends StatefulWidget {
  @override
  createState() {
    return HomeRouteState();
  }
}

class HomeRouteState extends State<HomeRoute> {
  var status = false;
  DateTime _lastPressedAt; //上次点击时间

  showTips() {
    Fluttertoast.showToast(msg: Exit_Tips);
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onDoubleTap: () {
        return status = true;
      },
      child: WillPopScope(
        onWillPop: () async {
          if (_lastPressedAt == null) {
            showTips();
            _lastPressedAt = DateTime.now();
          } else {
            if (DateTime.now().difference(_lastPressedAt) <
                Duration(seconds: 1)) {
              // 退出程序
              await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
            } else {
              showTips();
              _lastPressedAt = DateTime.now();
            }
          }
          return false;
        },
        child: LoginPage(),
      ),
    );
  }
}