patient_info_bar.dart 4.6 KB

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