main_page.dart 5.7 KB

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