upload_select_clinic_page.dart 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import 'package:auto_route/auto_route.dart';
  2. import 'package:eitc_erm_dental_flutter/app_router.gr.dart';
  3. import 'package:eitc_erm_dental_flutter/entity/clinic_info.dart';
  4. import 'package:eitc_erm_dental_flutter/entity/history_item_info.dart';
  5. import 'package:eitc_erm_dental_flutter/exts.dart';
  6. import 'package:eitc_erm_dental_flutter/funcs.dart';
  7. import 'package:eitc_erm_dental_flutter/http/api_exception.dart';
  8. import 'package:eitc_erm_dental_flutter/pages/upload/vm/upload_view_model.dart';
  9. import 'package:eitc_erm_dental_flutter/widget/custom_divider.dart';
  10. import 'package:flutter/material.dart';
  11. import 'package:flutter_riverpod/flutter_riverpod.dart';
  12. import 'package:flutter_screenutil/flutter_screenutil.dart';
  13. ///上传选择医院页面
  14. @RoutePage(name: "uploadSelectClinicRoute")
  15. class UploadSelectClinicPage extends StatefulWidget {
  16. final List<HistoryItemInfo> uploadList;
  17. ///是否是从查看页面上传
  18. final bool isFromView;
  19. const UploadSelectClinicPage(
  20. {super.key, required this.uploadList, required this.isFromView});
  21. @override
  22. State<UploadSelectClinicPage> createState() => _UploadSelectClinicPageState();
  23. }
  24. class _UploadSelectClinicPageState extends State<UploadSelectClinicPage> {
  25. bool _isExpand = false;
  26. List<ClinicInfo> _clinicList = [];
  27. bool _isToUpload = false;
  28. @override
  29. void initState() {
  30. super.initState();
  31. screenDisableRotate();
  32. }
  33. @override
  34. void dispose() {
  35. super.dispose();
  36. //如果不是前往上传页且是从查看页面上传,就恢复屏幕旋转
  37. if (!_isToUpload && widget.isFromView) {
  38. screenEnableRotate();
  39. }
  40. }
  41. @override
  42. Widget build(BuildContext context) {
  43. return Scaffold(
  44. appBar: AppBar(),
  45. body: SafeArea(
  46. child: SizedBox.expand(
  47. child: Padding(
  48. padding: EdgeInsets.fromLTRB(34.w, 52.h, 34.w, 24.h),
  49. child: Column(
  50. children: [
  51. Text(
  52. getS().pleaseSelectUploadClinic,
  53. style: Theme.of(context).textTheme.titleLarge,
  54. ),
  55. SizedBox(
  56. height: 24.h,
  57. ),
  58. _isExpand
  59. ? _getList(context, _clinicList)
  60. : Consumer(builder: (ctx, ref, _) {
  61. AsyncValue<List<ClinicInfo>> value =
  62. ref.watch(clinicListProvider);
  63. return switch (value) {
  64. AsyncData(value: var data) =>
  65. data.isEmpty ? _getEmpty() : _getSelector(ctx, data),
  66. AsyncError(:final error) =>
  67. _getError(ApiException.from(error)),
  68. _ => Expanded(
  69. child: Center(
  70. child: CircularProgressIndicator(),
  71. )),
  72. };
  73. }),
  74. ],
  75. ),
  76. ),
  77. )),
  78. );
  79. }
  80. ///获取错误Widget
  81. Widget _getError(ApiException exception) {
  82. return Expanded(
  83. child: Center(
  84. child: Row(
  85. mainAxisSize: MainAxisSize.min,
  86. children: [
  87. const Icon(
  88. Icons.error,
  89. color: Colors.red,
  90. ),
  91. SizedBox(
  92. width: 5.w,
  93. ),
  94. Text(
  95. "${exception.message}",
  96. style: const TextStyle(color: Colors.red),
  97. )
  98. ],
  99. ),
  100. ));
  101. }
  102. ///获取无数据Widget
  103. Widget _getEmpty() {
  104. return Expanded(
  105. child: Center(
  106. child: Row(
  107. mainAxisSize: MainAxisSize.min,
  108. children: [
  109. const Icon(Icons.error),
  110. SizedBox(
  111. width: 5.w,
  112. ),
  113. Text(getS().noData),
  114. ],
  115. ),
  116. ));
  117. }
  118. ///返回选择器样式的第一条医院名字widget
  119. Widget _getSelector(BuildContext context, List<ClinicInfo> list) {
  120. _clinicList = list;
  121. return GestureDetector(
  122. onTap: () {
  123. setState(() {
  124. _isExpand = true;
  125. });
  126. },
  127. child: Container(
  128. padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 14.h),
  129. decoration: BoxDecoration(
  130. color: Theme.of(context).colorScheme.surfaceContainerHigh,
  131. borderRadius: BorderRadius.circular(8.r)),
  132. child: Row(
  133. children: [
  134. Expanded(child: Text(list[0].clinicName ?? "")),
  135. const Icon(Icons.arrow_drop_down_outlined)
  136. ],
  137. ),
  138. ),
  139. );
  140. }
  141. ///获取列表Widget
  142. Widget _getList(BuildContext context, List<ClinicInfo> list) {
  143. return Container(
  144. decoration: BoxDecoration(
  145. color: Theme.of(context).colorScheme.surfaceContainerHigh,
  146. borderRadius: BorderRadius.circular(8.r)),
  147. child: ListView.separated(
  148. shrinkWrap: true,
  149. itemBuilder: (ctx, index) {
  150. ClinicInfo info = list[index];
  151. return InkWell(
  152. onTap: () => _onSelectClinic(info),
  153. child: Padding(
  154. padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 14.h),
  155. child: Row(
  156. children: [
  157. Expanded(child: Text(info.clinicName ?? "")),
  158. //每行都增加一个透明的箭头,以保证高度和未点击列表前的高度一致
  159. const Icon(
  160. Icons.arrow_drop_down_outlined,
  161. color: Colors.transparent,
  162. )
  163. ],
  164. ),
  165. ),
  166. );
  167. },
  168. separatorBuilder: (_, __) => const CustomDivider(
  169. height: 0.0,
  170. ),
  171. itemCount: list.length),
  172. );
  173. }
  174. ///当选择了医院
  175. void _onSelectClinic(ClinicInfo info) async {
  176. logd("选择了医院,${info.toString()}");
  177. if (info.clinicApiUrl.isNullOrEmpty ||
  178. !(info.clinicApiUrl!.startsWith("http://") ||
  179. info.clinicApiUrl!.startsWith("https://"))) {
  180. logd("医院上传地址为空或不是以http://、https://开头");
  181. return;
  182. }
  183. String? deviceModel = await getDeviceModel();
  184. if (deviceModel.isNullOrEmpty) {
  185. logd("设备型号为空,无法上传");
  186. return;
  187. }
  188. logd("设备型号=$deviceModel");
  189. if (mounted) {
  190. _isToUpload = true;
  191. AutoRouter.of(context).popAndPush(UploadRoute(
  192. uploadList: widget.uploadList,
  193. clinicInfo: info,
  194. isFromView: widget.isFromView));
  195. }
  196. }
  197. }