history_page.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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/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/pages/history/vm/history_view_model.dart';
  8. import 'package:eitc_erm_dental_flutter/pages/history/widget/history_item_view.dart';
  9. import 'package:eitc_erm_dental_flutter/widget/empty_widget.dart';
  10. import 'package:eitc_erm_dental_flutter/widget/loading_widget.dart';
  11. import 'package:eitc_erm_dental_flutter/widget/main_button.dart';
  12. import 'package:flutter/material.dart';
  13. import 'package:flutter_riverpod/flutter_riverpod.dart';
  14. import 'package:flutter_screenutil/flutter_screenutil.dart';
  15. ///历史记录页面
  16. @RoutePage(name: "historyRoute")
  17. class HistoryPage extends ConsumerStatefulWidget {
  18. const HistoryPage({super.key});
  19. @override
  20. ConsumerState createState() => _HistoryPageState();
  21. }
  22. class _HistoryPageState extends ConsumerState<HistoryPage> {
  23. //照片列表
  24. final List<HistoryItemInfo> _photoList = [];
  25. //视频列表
  26. final List<HistoryItemInfo> _videoList = [];
  27. @override
  28. void initState() {
  29. super.initState();
  30. }
  31. @override
  32. Widget build(BuildContext context) {
  33. return PopScope(
  34. canPop: false,
  35. onPopInvokedWithResult: _onPop,
  36. child: Scaffold(
  37. appBar: _getAppBar(context),
  38. body: Padding(
  39. padding: EdgeInsets.symmetric(horizontal: 16.w),
  40. child: Column(
  41. children: [
  42. _getSelectedCount(context),
  43. Expanded(child: _getListWidget(context)),
  44. _getBottomButtons(context),
  45. SizedBox(
  46. height: 5.h,
  47. )
  48. ],
  49. ),
  50. ),
  51. ));
  52. }
  53. ///pop拦截
  54. void _onPop(bool didPop, _) {
  55. if (didPop) {
  56. return;
  57. }
  58. if (ref.read(historySelectModeProvider)) {
  59. _setSelectMode(false);
  60. return;
  61. }
  62. Navigator.pop(context);
  63. }
  64. ///获取appbar
  65. AppBar _getAppBar(BuildContext context) {
  66. return AppBar(
  67. centerTitle: true,
  68. forceMaterialTransparency: true,
  69. title: Text(getS().history),
  70. leading: IconButton(
  71. onPressed: () {
  72. Navigator.pop(context);
  73. },
  74. icon: Icon(
  75. Platform.isIOS ? Icons.arrow_back_ios_new : Icons.arrow_back)),
  76. actions: [
  77. PopupMenuButton(
  78. icon: const Icon(Icons.menu),
  79. //弹出菜单背景色
  80. color: const Color(0xFF383838),
  81. //背景形状
  82. shape: RoundedRectangleBorder(
  83. borderRadius: BorderRadius.circular(8.r)),
  84. itemBuilder: (ctx) {
  85. return [
  86. PopupMenuItem(
  87. child: SizedBox(
  88. width: double.infinity,
  89. child: Text(
  90. getS().byTime,
  91. textAlign: TextAlign.center,
  92. style: const TextStyle(color: Colors.white),
  93. ),
  94. ),
  95. onTap: () => _updateListType(0),
  96. ),
  97. PopupMenuItem(
  98. child: SizedBox(
  99. width: double.infinity,
  100. child: Text(
  101. getS().byCategory,
  102. textAlign: TextAlign.center,
  103. style: const TextStyle(color: Colors.white),
  104. ),
  105. ),
  106. onTap: () => _updateListType(1),
  107. )
  108. ];
  109. }),
  110. ],
  111. );
  112. }
  113. ///更新列表类型
  114. void _updateListType(int type) {
  115. if (ref.read(historyViewTypeProvider) == type) {
  116. return;
  117. }
  118. ref.read(historyViewTypeProvider.notifier).setType(type);
  119. }
  120. ///获取已选择数量widget
  121. Widget _getSelectedCount(BuildContext context) {
  122. return Consumer(builder: (ctx, ref, _) {
  123. bool isSelectedMode = ref.watch(historySelectModeProvider);
  124. Color primaryColor = context.primaryColor;
  125. //如果不用Visibility的方式,当进入选择模式时,GridView会滚到最上面
  126. return Visibility(
  127. visible: isSelectedMode,
  128. child: Row(
  129. children: [
  130. Consumer(builder: (_, tref, __) {
  131. int count = tref.watch(historySelectedCountProvider);
  132. return Text(getS().hasSelectCountItem(count));
  133. }),
  134. SizedBox(
  135. width: 6.w,
  136. ),
  137. ElevatedButton(
  138. onPressed: () => _setSelectMode(false),
  139. style: ButtonStyle(
  140. minimumSize: const WidgetStatePropertyAll(Size.zero),
  141. padding: WidgetStatePropertyAll(EdgeInsets.symmetric(
  142. horizontal: 15.w, vertical: 4.h)),
  143. elevation: const WidgetStatePropertyAll(0.0),
  144. backgroundColor:
  145. const WidgetStatePropertyAll(Color(0xFFCEDCF0))),
  146. child: Row(
  147. children: [
  148. Text(
  149. getS().cancel,
  150. style: TextStyle(color: primaryColor, fontSize: 12.sp),
  151. ),
  152. SizedBox(
  153. width: 5.w,
  154. ),
  155. Icon(Icons.close, size: 18.r, color: primaryColor)
  156. ],
  157. )),
  158. const Spacer(),
  159. SizedBox(
  160. width: 25.w,
  161. child: Consumer(builder: (_, cref, __) {
  162. int count = cref.watch(historySelectedCountProvider);
  163. return Checkbox(
  164. value: count ==
  165. (cref
  166. .read(historyListProvider)
  167. .value
  168. ?.getMergedList(false)
  169. .length ??
  170. 0),
  171. shape: const CircleBorder(),
  172. onChanged: (bo) => cref
  173. .read(historyListProvider.notifier)
  174. .selectAll(bo ?? false));
  175. })),
  176. Text(getS().selectAll),
  177. SizedBox(
  178. width: 5.w,
  179. ),
  180. ],
  181. ));
  182. });
  183. }
  184. Widget _getListWidget(BuildContext context) {
  185. return Consumer(builder: (ctx, ref, _) {
  186. AsyncValue<HistoryListData> value = ref.watch(historyListProvider);
  187. int viewType = ref.watch(historyViewTypeProvider);
  188. return switch (value) {
  189. AsyncData<HistoryListData>(value: var data) => data.isEmpty
  190. ? EmptyWidget(
  191. text: getS().noHistories,
  192. )
  193. : (viewType == 0 ? _getTimeList(data) : _getTypeList(ctx, data)),
  194. _ => LoadingWidget(
  195. text: getS().loadingHistories,
  196. )
  197. };
  198. });
  199. }
  200. ///获取按时间列表
  201. Widget _getTimeList(HistoryListData data) {
  202. return _getGridView(data.getMergedList(true));
  203. }
  204. ///获取按类型列表
  205. Widget _getTypeList(BuildContext context, HistoryListData data) {
  206. return SingleChildScrollView(
  207. child: Column(
  208. crossAxisAlignment: CrossAxisAlignment.start,
  209. children: [
  210. Text(
  211. getS().photo,
  212. style: context.titleMedium,
  213. ),
  214. SizedBox(
  215. height: 10.h,
  216. ),
  217. _getGridView(data.photoList, shinkWarp: true, scrollable: false),
  218. SizedBox(
  219. height: 10.h,
  220. ),
  221. Text(
  222. getS().video,
  223. style: context.titleMedium,
  224. ),
  225. SizedBox(
  226. height: 10.h,
  227. ),
  228. _getGridView(data.videoList, shinkWarp: true, scrollable: false)
  229. ],
  230. ),
  231. );
  232. }
  233. ///获取GridView
  234. Widget _getGridView(List<HistoryItemInfo> list,
  235. {bool shinkWarp = false, bool scrollable = true}) {
  236. return GridView.builder(
  237. shrinkWrap: shinkWarp,
  238. physics: scrollable
  239. ? const ClampingScrollPhysics()
  240. : const NeverScrollableScrollPhysics(),
  241. gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  242. crossAxisCount: 2,
  243. childAspectRatio: 164.0 / 120.0,
  244. mainAxisSpacing: 8.r,
  245. crossAxisSpacing: 15.r),
  246. itemBuilder: (ctx, index) {
  247. HistoryItemInfo item = list[index];
  248. return GestureDetector(
  249. child: HistoryItemView(
  250. index: index,
  251. name: item.name,
  252. path: item.path,
  253. type: item.type,
  254. ),
  255. onTap: () => _onTap(ctx, item),
  256. onLongPress: () => _onLongPress(ctx, item),
  257. );
  258. },
  259. itemCount: list.length,
  260. );
  261. }
  262. ///点击
  263. void _onTap(BuildContext context, HistoryItemInfo info) async {
  264. if (ref.read(historySelectModeProvider)) {
  265. ref.read(historyListProvider.notifier).select(info, !info.isSelected);
  266. return;
  267. }
  268. await context.pushRoute(info.type == 0
  269. ? PhotoViewRoute(info: info)
  270. : VideoPlayerRoute(info: info));
  271. }
  272. ///长按
  273. void _onLongPress(BuildContext context, HistoryItemInfo info) {
  274. if (ref.read(historySelectModeProvider)) {
  275. return;
  276. }
  277. _setSelectMode(true);
  278. ref.read(historyListProvider.notifier).select(info, true);
  279. }
  280. ///设置选择模式
  281. void _setSelectMode(bool isSelectMode) {
  282. ref.read(historySelectModeProvider.notifier).setSelectMode(isSelectMode);
  283. }
  284. ///获取选择的条目
  285. List<HistoryItemInfo> _getSelectedItems() {
  286. return ref.read(historyListProvider.notifier).getSelectedItems();
  287. }
  288. ///获取底部按钮
  289. Widget _getBottomButtons(BuildContext context) {
  290. bool isSelectedMode = ref.watch(historySelectModeProvider);
  291. //如果不用Visibility的方式,当进入选择模式时,GridView会滚到最上面
  292. return Visibility(
  293. visible: isSelectedMode,
  294. child: Row(
  295. children: [
  296. Expanded(
  297. child: MainButton(
  298. text: getS().upload, onPressed: () => _onUpload(context)),
  299. ),
  300. SizedBox(
  301. width: 15.w,
  302. ),
  303. Expanded(
  304. child: MainButton(
  305. text: getS().delete,
  306. onPressed: () => _onDelete(context),
  307. isOutlined: true,
  308. )),
  309. ],
  310. ));
  311. }
  312. ///上传
  313. void _onUpload(BuildContext context) async {
  314. if (!await checkInternetWifi()) {
  315. return;
  316. }
  317. List<HistoryItemInfo> list = _getSelectedItems();
  318. if (list.isEmpty) {
  319. showToast(text: getS().pleaseSelectToUpload);
  320. return;
  321. }
  322. if (context.mounted) {
  323. context.pushRoute(
  324. UploadSelectClinicRoute(uploadList: list, isFromView: false));
  325. }
  326. }
  327. ///删除
  328. void _onDelete(BuildContext context) async {
  329. List<HistoryItemInfo> list = _getSelectedItems();
  330. if (list.isEmpty) {
  331. showToast(text: getS().pleaseSelectToDelete);
  332. return;
  333. }
  334. bool? bo = await showDeleteAlertDialog(
  335. context, getS().deleteMultiHint(list.length));
  336. if (bo == null || !bo) {
  337. return;
  338. }
  339. bool result = await ref.read(historyListProvider.notifier).delete(list);
  340. showToast(text: result ? getS().deleteSuccess : getS().deleteFailed);
  341. }
  342. }