main_page.dart 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:auto_route/auto_route.dart';
  4. import 'package:eitc_erm_dental_flutter/app_router.gr.dart';
  5. import 'package:eitc_erm_dental_flutter/dialog/app_start_agreement_dialog.dart';
  6. import 'package:eitc_erm_dental_flutter/dialog/switch_patient_dialog.dart';
  7. import 'package:eitc_erm_dental_flutter/entity/db/local_patient_info.dart';
  8. import 'package:eitc_erm_dental_flutter/exts.dart';
  9. import 'package:eitc_erm_dental_flutter/funcs.dart';
  10. import 'package:eitc_erm_dental_flutter/global.dart';
  11. import 'package:eitc_erm_dental_flutter/pages/main/widget/connected_view.dart';
  12. import 'package:eitc_erm_dental_flutter/pages/main/widget/unconnected_view.dart';
  13. import 'package:eitc_erm_dental_flutter/pages/patient/vm/patient_view_model.dart';
  14. import 'package:eitc_erm_dental_flutter/pages/patient/widget/patient_info_bar.dart';
  15. import 'package:eitc_erm_dental_flutter/vm/global_view_model.dart';
  16. import 'package:flutter/material.dart';
  17. import 'package:flutter/services.dart';
  18. import 'package:flutter_riverpod/flutter_riverpod.dart';
  19. import 'package:flutter_screenutil/flutter_screenutil.dart';
  20. ///主页面
  21. @RoutePage(name: "mainRoute")
  22. class MainPage extends ConsumerStatefulWidget {
  23. const MainPage({super.key});
  24. @override
  25. ConsumerState createState() => _MainPageState();
  26. }
  27. class _MainPageState extends ConsumerState<MainPage> {
  28. ///最后退出时间
  29. DateTime? lastExitTime;
  30. @override
  31. void initState() {
  32. super.initState();
  33. WidgetsBinding.instance.addPostFrameCallback((_) {
  34. ref.read(deviceConnectStatusProvider(videoChannel).notifier).startCheck();
  35. });
  36. }
  37. @override
  38. void dispose() {
  39. if (context.mounted) {
  40. ref.read(deviceConnectStatusProvider(videoChannel).notifier).stopCheck();
  41. }
  42. super.dispose();
  43. }
  44. @override
  45. Widget build(BuildContext context) {
  46. bool isConnected = ref.watch(deviceConnectStatusProvider(videoChannel));
  47. return PopScope(
  48. canPop: !Platform.isAndroid,
  49. onPopInvokedWithResult: _onPopInvoke,
  50. child: Scaffold(
  51. appBar: _getAppBar(),
  52. body: SafeArea(
  53. child: Column(
  54. children: [
  55. PatientInfoBar(
  56. onSwitchPatient: _onSwitchPatient,
  57. onAddPatient: _onAddPatient,
  58. ),
  59. Expanded(
  60. child: isConnected
  61. ? ConnectedView(
  62. startVideo: _startVideo,
  63. )
  64. : const UnconnectedView())
  65. ],
  66. ),
  67. ),
  68. ));
  69. }
  70. void _onPopInvoke(bool didPop, dynamic result) {
  71. if (didPop) {
  72. return;
  73. }
  74. DateTime now = DateTime.now();
  75. if (lastExitTime == null ||
  76. now.millisecondsSinceEpoch - lastExitTime!.millisecondsSinceEpoch >
  77. 2000) {
  78. lastExitTime = now;
  79. showToast(text: getS().tapAgainExit);
  80. } else {
  81. exitApp();
  82. }
  83. }
  84. AppBar _getAppBar() {
  85. Color color = context.onSurfaceVariantColor;
  86. ButtonStyle buttonStyle = ButtonStyle(
  87. padding: WidgetStatePropertyAll(EdgeInsets.symmetric(horizontal: 5.w)));
  88. TextStyle textStyle = TextStyle(fontSize: 12.sp, color: color);
  89. return AppBar(
  90. automaticallyImplyLeading: false,
  91. forceMaterialTransparency: true,
  92. actions: [
  93. TextButton(
  94. onPressed: _gotoHistories,
  95. style: buttonStyle,
  96. child: Row(children: [
  97. Icon(
  98. Icons.history_outlined,
  99. color: color,
  100. size: 24.r,
  101. ),
  102. SizedBox(
  103. width: 4.w,
  104. ),
  105. Text(
  106. getS().history,
  107. style: textStyle,
  108. )
  109. ])),
  110. TextButton(
  111. onPressed: _gotoSettings,
  112. style: buttonStyle,
  113. child: Row(
  114. children: [
  115. Icon(
  116. Icons.settings_outlined,
  117. color: color,
  118. size: 24.r,
  119. ),
  120. SizedBox(
  121. width: 4.w,
  122. ),
  123. Text(getS().settings, style: textStyle),
  124. ],
  125. ),
  126. ),
  127. SizedBox(
  128. width: 12.w,
  129. ),
  130. ],
  131. );
  132. }
  133. ///前往历史记录
  134. void _gotoHistories() {
  135. context.pushRoute(const HistoryRoute());
  136. }
  137. ///前往设置
  138. void _gotoSettings() {
  139. context.pushRoute(const SettingsRoute());
  140. }
  141. ///开始视频
  142. void _startVideo() async {
  143. //正在同步就诊人数据
  144. if (isSyncingPatient) {
  145. showToast(text: getS().syncDataWaiting);
  146. return;
  147. }
  148. //已选择的就诊人ID
  149. if (selectedPatientId < 0) {
  150. showToast(text: getS().pleaseSelectPatient);
  151. return;
  152. }
  153. context.pushRoute(VideoViewRoute());
  154. }
  155. ///当切换咨询人
  156. void _onSwitchPatient() {
  157. if (context.mounted) {
  158. showModalBottomSheet(
  159. context: context,
  160. builder: (ctx) {
  161. return SwitchPatientDialog(onSelectPatient: (info) {
  162. Navigator.pop(ctx);
  163. _onSelectPatient(info);
  164. }, onAddPatient: () {
  165. Navigator.pop(ctx);
  166. _onAddPatient();
  167. });
  168. });
  169. }
  170. }
  171. ///当选择了咨询人
  172. void _onSelectPatient(LocalPatientInfo info) {
  173. logd("选择了咨询人,$info");
  174. //记录选择的就诊人ID
  175. setSelectedPatientId(info.id);
  176. ref.invalidate(localPatientListProvider);
  177. }
  178. ///当添加咨询人
  179. void _onAddPatient() async {
  180. if (!await checkInternetWifi()) {
  181. logd("新增咨询人,但是连的是设备");
  182. return;
  183. }
  184. if (mounted) {
  185. if (!checkLogin(context)) {
  186. return;
  187. }
  188. logd("新增咨询人,打开咨询人列表页面");
  189. LocalPatientInfo? info = await context.pushRoute(PatientListRoute());
  190. if (info != null) {
  191. _onSelectPatient(info);
  192. }
  193. }
  194. }
  195. }