home.dart 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import 'dart:io';
  2. import 'package:ctjt_flutter/common/states.dart';
  3. import 'package:ctjt_flutter/common/styles.dart';
  4. import 'package:ctjt_flutter/common/time.dart';
  5. import 'package:ctjt_flutter/widget/button.dart';
  6. import 'package:ctjt_flutter/widget/contact.dart';
  7. import 'package:ctjt_flutter/widget/drawer.dart';
  8. import 'package:ctjt_flutter/widget/title.dart';
  9. import 'package:flutter/material.dart';
  10. import 'package:fluttertoast/fluttertoast.dart';
  11. import 'package:provider/provider.dart';
  12. // 首页
  13. class HomePage extends StatefulWidget {
  14. HomePage({Key? key}) : super(key: key);
  15. @override
  16. _HomePageState createState() => _HomePageState();
  17. }
  18. class _HomePageState extends State<HomePage> {
  19. int tts = 0;
  20. @override
  21. void initState() {
  22. super.initState();
  23. // 初始化状态
  24. States.init(context);
  25. // 显示隐私协议
  26. Future.delayed(Duration(seconds: 1), () {
  27. var flag = Provider.of<AppVersion>(context, listen: false).showContact;
  28. if (flag) {
  29. Contact.showUserContactDialog(context);
  30. }
  31. });
  32. }
  33. @override
  34. void didChangeDependencies() {
  35. super.didChangeDependencies();
  36. Styles.initSize(); // 在首页中初始化公共样式变量的值
  37. }
  38. @override
  39. Widget build(BuildContext context) {
  40. return WillPopScope(
  41. // 响应Android返回按钮,需要在首页最外层包装此组件
  42. child: Scaffold(
  43. backgroundColor: Colors.white,
  44. appBar: PageHead(
  45. leadingType: LeadingType.Menu,
  46. title: Text("Test"),
  47. ),
  48. // 左侧边栏
  49. drawer: DrawerMenu(),
  50. body: Column(
  51. children: [
  52. TButton(
  53. onPressed: () {
  54. Navigator.pushNamed(context, '/remote_call');
  55. },
  56. child: Text("呼叫测试")),
  57. ],
  58. )),
  59. onWillPop: () {
  60. if (Navigator.canPop(context)) {
  61. print('返回了 -- 回退一层');
  62. Navigator.pop(context);
  63. } else {
  64. int ticks = TimeUtils.getDayNow();
  65. if (ticks - tts <= 2000) {
  66. print('返回了 -- 退出程序');
  67. exit(0);
  68. } else {
  69. print('返回了 -- 再按一次退出程序');
  70. tts = ticks;
  71. Fluttertoast.showToast(
  72. msg: "再按一次退出程序",
  73. toastLength: Toast.LENGTH_LONG,
  74. gravity: ToastGravity.CENTER,
  75. timeInSecForIosWeb: 1);
  76. }
  77. }
  78. return new Future.value(false);
  79. },
  80. );
  81. }
  82. }