dict_manage_page.dart 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import 'package:chicken_farm/components/vb_app_bar.dart';
  2. import 'package:chicken_farm/core/config/default_dicts.dart';
  3. import 'package:chicken_farm/modes/system/dict.dart';
  4. import 'package:chicken_farm/stores/dict_stroe.dart';
  5. import 'package:flutter/material.dart';
  6. class DictManagePage extends StatefulWidget {
  7. const DictManagePage({super.key});
  8. @override
  9. State<DictManagePage> createState() => _DictManagePageState();
  10. }
  11. class _DictManagePageState extends State<DictManagePage> {
  12. final DictStore _dictStore = DictStore();
  13. late Future<Map<String, List<DictDataModel>>> _dictsFuture;
  14. final Set<String> _expandedTypes = <String>{};
  15. @override
  16. void initState() {
  17. super.initState();
  18. _loadDicts();
  19. }
  20. void _loadDicts() {
  21. setState(() {
  22. _dictsFuture = _dictStore.getAllOfflineDicts();
  23. });
  24. }
  25. @override
  26. Widget build(BuildContext context) {
  27. return Scaffold(
  28. appBar: VberAppBar(title: '字典维护'),
  29. body: FutureBuilder<Map<String, List<DictDataModel>>>(
  30. future: _dictsFuture,
  31. builder: (context, snapshot) {
  32. if (snapshot.connectionState == ConnectionState.waiting) {
  33. return const Center(child: CircularProgressIndicator());
  34. }
  35. if (snapshot.hasError) {
  36. return Center(child: Text('加载失败: ${snapshot.error}'));
  37. }
  38. final dicts = snapshot.data ?? {};
  39. if (dicts.isEmpty) {
  40. return const Center(
  41. child: Column(
  42. mainAxisAlignment: MainAxisAlignment.center,
  43. children: [
  44. Icon(Icons.info, size: 48, color: Colors.grey),
  45. SizedBox(height: 16),
  46. Text('暂无字典数据', style: TextStyle(fontSize: 16)),
  47. ],
  48. ),
  49. );
  50. }
  51. return ListView.builder(
  52. itemCount: dicts.length,
  53. itemBuilder: (context, index) {
  54. final dictType = dicts.keys.elementAt(index);
  55. final dictItems = dicts[dictType] ?? [];
  56. final dictName = DefaultDicts.getDictName(dictType);
  57. return Card(
  58. margin: const EdgeInsets.fromLTRB(16.0, 8.0, 16.0, 8.0),
  59. child: Theme(
  60. data: Theme.of(
  61. context,
  62. ).copyWith(dividerColor: Colors.transparent),
  63. child: ExpansionTile(
  64. key: Key(dictType),
  65. initiallyExpanded: _expandedTypes.contains(dictType),
  66. onExpansionChanged: (expanded) {
  67. setState(() {
  68. if (expanded) {
  69. _expandedTypes.add(dictType);
  70. } else {
  71. _expandedTypes.remove(dictType);
  72. }
  73. });
  74. },
  75. title: Text('$dictName (${dictItems.length})'),
  76. children: [
  77. if (dictItems.isEmpty)
  78. const ListTile(title: Text('该类型下暂无数据'))
  79. else
  80. ...dictItems.map(
  81. (item) => ListTile(
  82. title: Text(item.dictLabel),
  83. subtitle: Text('值: ${item.dictValue}'),
  84. trailing: Row(
  85. mainAxisSize: MainAxisSize.min,
  86. children: [
  87. IconButton(
  88. icon: const Icon(Icons.edit, size: 18),
  89. onPressed: () =>
  90. _editDictItem(dictType, item),
  91. ),
  92. IconButton(
  93. icon: const Icon(Icons.delete, size: 18),
  94. onPressed: () =>
  95. _deleteDictItem(dictType, item),
  96. ),
  97. ],
  98. ),
  99. ),
  100. ),
  101. ListTile(
  102. leading: const Icon(Icons.add, color: Colors.blue),
  103. title: const Text('添加新项目'),
  104. onTap: () => _addNewDictItem(dictType),
  105. ),
  106. ],
  107. ),
  108. ),
  109. );
  110. },
  111. );
  112. },
  113. ),
  114. );
  115. }
  116. void _addNewDictItem(String dictType) async {
  117. final labelController = TextEditingController();
  118. final valueController = TextEditingController();
  119. final sortController = TextEditingController(text: '0');
  120. final result = await showDialog<bool>(
  121. context: context,
  122. builder: (context) => AlertDialog(
  123. title: const Text('添加字典项'),
  124. content: Column(
  125. mainAxisSize: MainAxisSize.min,
  126. children: [
  127. TextField(
  128. controller: labelController,
  129. decoration: const InputDecoration(labelText: '标签'),
  130. ),
  131. TextField(
  132. controller: valueController,
  133. decoration: const InputDecoration(labelText: '值'),
  134. ),
  135. TextField(
  136. controller: sortController,
  137. decoration: const InputDecoration(labelText: '排序'),
  138. keyboardType: TextInputType.number,
  139. ),
  140. ],
  141. ),
  142. actions: [
  143. TextButton(
  144. onPressed: () => Navigator.of(context).pop(false),
  145. child: const Text('取消'),
  146. ),
  147. TextButton(
  148. onPressed: () => Navigator.of(context).pop(true),
  149. child: const Text('确定'),
  150. ),
  151. ],
  152. ),
  153. );
  154. if (result == true) {
  155. final dictItems = (await _dictStore.getDictByType(dictType)) ?? [];
  156. // 生成唯一的dictCode
  157. int maxCode = 0;
  158. for (final item in dictItems) {
  159. if (item.dictCode > maxCode) {
  160. maxCode = item.dictCode;
  161. }
  162. }
  163. final newItem = DictDataModel(
  164. dictCode: maxCode + 1,
  165. dictSort: int.tryParse(sortController.text) ?? 0,
  166. dictLabel: labelController.text,
  167. dictValue: valueController.text,
  168. dictType: dictType,
  169. );
  170. try {
  171. await _dictStore.addDictItem(dictType, newItem);
  172. _loadDicts(); // 重新加载数据
  173. } catch (e) {
  174. if (mounted) {
  175. ScaffoldMessenger.of(
  176. context,
  177. ).showSnackBar(SnackBar(content: Text('操作失败: $e')));
  178. }
  179. }
  180. }
  181. }
  182. void _editDictItem(String dictType, DictDataModel item) async {
  183. final labelController = TextEditingController(text: item.dictLabel);
  184. final valueController = TextEditingController(text: item.dictValue);
  185. final sortController = TextEditingController(
  186. text: item.dictSort.toString(),
  187. );
  188. final result = await showDialog<bool>(
  189. context: context,
  190. builder: (context) => AlertDialog(
  191. title: const Text('编辑字典项'),
  192. content: Column(
  193. mainAxisSize: MainAxisSize.min,
  194. children: [
  195. TextField(
  196. controller: labelController,
  197. decoration: const InputDecoration(labelText: '标签'),
  198. ),
  199. TextField(
  200. controller: valueController,
  201. decoration: const InputDecoration(labelText: '值'),
  202. ),
  203. TextField(
  204. controller: sortController,
  205. decoration: const InputDecoration(labelText: '排序'),
  206. keyboardType: TextInputType.number,
  207. ),
  208. ],
  209. ),
  210. actions: [
  211. TextButton(
  212. onPressed: () => Navigator.of(context).pop(false),
  213. child: const Text('取消'),
  214. ),
  215. TextButton(
  216. onPressed: () => Navigator.of(context).pop(true),
  217. child: const Text('确定'),
  218. ),
  219. ],
  220. ),
  221. );
  222. if (result == true) {
  223. final updatedItem = DictDataModel(
  224. dictCode: item.dictCode,
  225. dictSort: int.tryParse(sortController.text) ?? item.dictSort,
  226. dictLabel: labelController.text,
  227. dictValue: valueController.text,
  228. dictType: dictType,
  229. );
  230. try {
  231. await _dictStore.updateDictItem(dictType, updatedItem);
  232. _loadDicts(); // 重新加载数据
  233. } catch (e) {
  234. if (mounted) {
  235. ScaffoldMessenger.of(
  236. context,
  237. ).showSnackBar(SnackBar(content: Text('操作失败: $e')));
  238. }
  239. }
  240. }
  241. }
  242. void _deleteDictItem(String dictType, DictDataModel item) async {
  243. final confirm = await showDialog<bool>(
  244. context: context,
  245. builder: (context) => AlertDialog(
  246. title: const Text('确认删除'),
  247. content: Text('确定要删除 "${item.dictLabel}" 吗?'),
  248. actions: [
  249. TextButton(
  250. onPressed: () => Navigator.of(context).pop(false),
  251. child: const Text('取消'),
  252. ),
  253. TextButton(
  254. onPressed: () => Navigator.of(context).pop(true),
  255. child: const Text('确定'),
  256. ),
  257. ],
  258. ),
  259. );
  260. if (confirm == true) {
  261. try {
  262. await _dictStore.removeDictItem(dictType, item.dictCode);
  263. _loadDicts(); // 重新加载数据
  264. } catch (e) {
  265. if (mounted) {
  266. ScaffoldMessenger.of(
  267. context,
  268. ).showSnackBar(SnackBar(content: Text('删除失败: $e')));
  269. }
  270. }
  271. }
  272. }
  273. }