main_page.dart 5.7 KB

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