patient_info_bar.dart 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import 'package:eitc_erm_dental_flutter/entity/db/local_patient_info.dart';
  2. import 'package:eitc_erm_dental_flutter/funcs.dart';
  3. import 'package:eitc_erm_dental_flutter/global.dart';
  4. import 'package:eitc_erm_dental_flutter/pages/patient/vm/patient_view_model.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter_riverpod/flutter_riverpod.dart';
  7. import 'package:flutter_screenutil/flutter_screenutil.dart';
  8. ///就诊人信息条
  9. class PatientInfoBar extends ConsumerStatefulWidget {
  10. final VoidCallback onSwitchPatient;
  11. final VoidCallback onAddPatient;
  12. const PatientInfoBar(
  13. {super.key, required this.onSwitchPatient, required this.onAddPatient});
  14. @override
  15. ConsumerState createState() => _PatientInfoBarState();
  16. }
  17. class _PatientInfoBarState extends ConsumerState<PatientInfoBar> {
  18. bool _hasSyncPatientLIst = false;
  19. @override
  20. void initState() {
  21. super.initState();
  22. ref.listenManual(localPatientListProvider, (_, value) {
  23. isSyncingPatient = value is AsyncLoading;
  24. if (value is! AsyncData<List<LocalPatientInfo>> || _hasSyncPatientLIst) {
  25. return;
  26. }
  27. _hasSyncPatientLIst = true;
  28. logd("读取到本地咨询人数据,开始同步服务器数据");
  29. syncPatientList();
  30. });
  31. }
  32. void syncPatientList() async {
  33. //如果连的不是设备wifi,就同步咨询人数据
  34. if (await isDeviceWifi()) {
  35. return;
  36. }
  37. //检查新版本,之所以放在这里,是因为获取wifi名字时会申请权限
  38. //不能同时弹多个请求权限弹窗,所以这里请求完毕后再检查
  39. if (mounted) {
  40. checkNewVersion(context, ref);
  41. }
  42. logd("同步咨询人数据");
  43. ref.read(localPatientListProvider.notifier).syncPatientList();
  44. }
  45. @override
  46. Widget build(BuildContext context) {
  47. AsyncValue<List<LocalPatientInfo>> value =
  48. ref.watch(localPatientListProvider);
  49. return Container(
  50. margin: EdgeInsets.symmetric(horizontal: 16.w),
  51. padding: EdgeInsets.symmetric(horizontal: 12.w),
  52. height: 62.h,
  53. decoration: BoxDecoration(
  54. color: Theme.of(context).colorScheme.surfaceContainerHigh,
  55. borderRadius: BorderRadius.circular(8.r),
  56. ),
  57. child: _getContent(value),
  58. );
  59. }
  60. ///获取内容
  61. Widget _getContent(AsyncValue<List<LocalPatientInfo>> value) {
  62. return switch (value) {
  63. AsyncData(value: var data) => _getSwitchPatient(data),
  64. AsyncError(error: var e, stackTrace: var stack) => _getError(e, stack),
  65. _ => _getLoading(),
  66. };
  67. }
  68. ///获取切换咨询人
  69. Widget _getSwitchPatient(List<LocalPatientInfo> list) {
  70. if (list.isEmpty) {
  71. return _getAddPatient();
  72. }
  73. //查找已选择的咨询人
  74. LocalPatientInfo? info =
  75. list.where((info) => info.id == selectedPatientId).firstOrNull;
  76. //如果没有就使用第一条记录,并保存id
  77. if (info == null) {
  78. info = list[0];
  79. setSelectedPatientId(info.id);
  80. }
  81. return Row(
  82. children: [
  83. Expanded(
  84. child: Column(
  85. mainAxisSize: MainAxisSize.min,
  86. mainAxisAlignment: MainAxisAlignment.spaceAround,
  87. crossAxisAlignment: CrossAxisAlignment.start,
  88. children: [
  89. _getText(info.name ?? ""),
  90. _getText(setIdCardStar(info.idCardDecrypt))
  91. ],
  92. )),
  93. _getButton(getS().switchPatient, widget.onSwitchPatient),
  94. ],
  95. );
  96. }
  97. Widget _getError(Object e, StackTrace stackTrack) {
  98. loge("读取本地咨询人列表异常,", error: e, stackTrace: stackTrack);
  99. return _getAddPatient();
  100. }
  101. ///获取添加咨询人
  102. Widget _getAddPatient() {
  103. return Row(
  104. children: [
  105. Expanded(child: _getText(getS().needAddPatient)),
  106. _getButton(getS().addPatient, widget.onAddPatient),
  107. ],
  108. );
  109. }
  110. ///获取loading
  111. Widget _getLoading() {
  112. return Row(
  113. children: [
  114. Expanded(child: _getText(getS().syncDataWaiting)),
  115. SizedBox.fromSize(
  116. size: Size.square(25.r),
  117. child: CircularProgressIndicator(),
  118. ),
  119. ],
  120. );
  121. }
  122. ///获取文本
  123. Widget _getText(String text) {
  124. return Text(
  125. text,
  126. );
  127. }
  128. ///获取按钮
  129. Widget _getButton(String text, VoidCallback onPressed) {
  130. return InkWell(
  131. onTap: onPressed,
  132. child: Container(
  133. padding: EdgeInsets.symmetric(horizontal: 8.w, vertical: 6.h),
  134. decoration: BoxDecoration(
  135. color: Theme.of(context).colorScheme.primary,
  136. borderRadius: BorderRadius.circular(4.r),
  137. ),
  138. child: Text(
  139. text,
  140. style: TextStyle(color: Theme.of(context).colorScheme.onPrimary),
  141. ),
  142. ),
  143. );
  144. }
  145. }