dialog_settings.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import 'dart:collection';
  2. import 'package:app_business/service/api.dart';
  3. import 'package:track_common/widget.dart';
  4. class SettingsController extends GetxController {
  5. final rules = HashMap<int, Rule>().obs;
  6. @override
  7. void onInit() {
  8. final id = Get.arguments as int;
  9. Get.find<ApiService>()
  10. .stub
  11. .toActivityRulesQuery(IdRequest()..id = Int64(id))
  12. .then((p0) {
  13. rules.update((val) {
  14. for (final (i, e) in p0.list.indexed) {
  15. val![e.arrId] = Rule()
  16. ..id = e.arrId
  17. ..idx = i
  18. ..name = e.arName
  19. ..isLock = !e.isLock
  20. ..value = e.arType == 1
  21. ? (RuleValueBool()..value = e.arValue == 'true')
  22. : (RuleValueArrStr()
  23. ..value = e.arValue
  24. ..options = e.arValueSelectList
  25. .map((e) => RuleValueArrStrOption()
  26. ..value = e.arValueSelect
  27. ..show = e.arStrSelect)
  28. .toList());
  29. }
  30. });
  31. });
  32. super.onInit();
  33. }
  34. }
  35. class DialogSettings extends GetView<SettingsController> {
  36. const DialogSettings({super.key});
  37. @override
  38. Widget build(BuildContext context) {
  39. return GetBuilder(
  40. init: SettingsController(),
  41. builder: (ctx) {
  42. return AlertDialog(
  43. surfaceTintColor: Colors.white,
  44. title: const Center(child: Text('规则设置')),
  45. content: Column(
  46. mainAxisSize: MainAxisSize.min,
  47. children: [
  48. Obx(() => _list(context)),
  49. Padding(
  50. padding: const EdgeInsets.symmetric(horizontal: 45),
  51. child: AppButton.confirm(
  52. width: double.infinity,
  53. height: 38,
  54. child: const Text('确认'),
  55. onPressed: () {
  56. final r = controller.rules.value.values;
  57. Get.back(result: r);
  58. }))
  59. ],
  60. ),
  61. );
  62. });
  63. }
  64. Widget _list(BuildContext context) {
  65. final l = controller.rules.value.values.toList();
  66. l.sort((a, b) => a.idx.compareTo(b.idx));
  67. return SizedBox(
  68. height: context.height * 0.6,
  69. width: 420,
  70. child: ListView(
  71. children: l.map((data) => _RuleElem(data)).toList(),
  72. ));
  73. }
  74. }
  75. class _RuleElem extends GetView<SettingsController> {
  76. const _RuleElem(this.data);
  77. final Rule data;
  78. @override
  79. Widget build(BuildContext context) {
  80. final value = data.value;
  81. Widget? option;
  82. if (value is RuleValueBool) {
  83. option = Switch(
  84. value: value.value,
  85. activeColor: Colors.blue,
  86. onChanged: data.isLock
  87. ? null
  88. : (b) {
  89. value.value = b;
  90. controller.rules.update((val) {
  91. val![data.id] = data;
  92. });
  93. });
  94. }
  95. if (value is RuleValueArrStr) {
  96. option = Row(
  97. mainAxisSize: MainAxisSize.min,
  98. children: value.options
  99. .map((e) => _Radio(
  100. value: e.value,
  101. show: e.show,
  102. groupValue: value.value,
  103. onChanged: data.isLock
  104. ? null
  105. : (v) {
  106. if (v != null) {
  107. value.value = v;
  108. controller.rules.update((val) {
  109. val![data.id] = data;
  110. });
  111. }
  112. }))
  113. .toList(),
  114. );
  115. }
  116. return Row(
  117. mainAxisSize: MainAxisSize.min,
  118. children: [
  119. SizedBox(width: 110, child: Text(data.name, textAlign: TextAlign.end)),
  120. const SizedBox(width: 22),
  121. option!,
  122. ],
  123. );
  124. }
  125. }
  126. class _Radio extends StatelessWidget {
  127. const _Radio(
  128. {required this.value,
  129. required this.show,
  130. required this.groupValue,
  131. required this.onChanged});
  132. final String value;
  133. final String show;
  134. final String groupValue;
  135. final void Function(String?)? onChanged;
  136. @override
  137. Widget build(BuildContext context) {
  138. return Row(
  139. mainAxisSize: MainAxisSize.min,
  140. children: [
  141. Radio(
  142. activeColor: Colors.blue,
  143. value: value,
  144. groupValue: groupValue,
  145. onChanged: onChanged),
  146. Text(show),
  147. ],
  148. );
  149. }
  150. }