dialog_settings.dart 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. ..isAppLock = e.isLock
  20. ..isAllowEditAppLock = e.toIsLockEdit
  21. ..isLockAll = e.toLock
  22. ..value = e.arType == 1
  23. ? (RuleValueBool()..value = e.arValue == 'true')
  24. : (RuleValueArrStr()
  25. ..value = e.arValue
  26. ..options = e.arValueSelectList
  27. .map((e) => RuleValueArrStrOption()
  28. ..value = e.arValueSelect
  29. ..show = e.arStrSelect)
  30. .toList());
  31. }
  32. });
  33. });
  34. super.onInit();
  35. }
  36. }
  37. class DialogSettings extends GetView<SettingsController> {
  38. const DialogSettings({super.key});
  39. @override
  40. Widget build(BuildContext context) {
  41. return GetBuilder(
  42. init: SettingsController(),
  43. builder: (ctx) {
  44. return AlertDialog(
  45. surfaceTintColor: Colors.white,
  46. title: const Center(child: Text('规则设置')),
  47. content: Column(
  48. mainAxisSize: MainAxisSize.min,
  49. children: [
  50. Obx(() => _list(context)),
  51. Padding(
  52. padding: const EdgeInsets.symmetric(horizontal: 45),
  53. child: AppButton.confirm(
  54. width: double.infinity,
  55. height: 38,
  56. child: const Text('确认'),
  57. onPressed: () {
  58. final r = controller.rules.value.values;
  59. Get.back(result: r);
  60. }))
  61. ],
  62. ),
  63. );
  64. });
  65. }
  66. Widget _list(BuildContext context) {
  67. final l = controller.rules.value.values.toList();
  68. l.sort((a, b) => a.idx.compareTo(b.idx));
  69. return SizedBox(
  70. height: context.height * 0.6,
  71. width: 470,
  72. child: ListView(
  73. children: l.map((data) => _RuleElem(data)).toList(),
  74. ));
  75. }
  76. }
  77. class _RuleElem extends GetView<SettingsController> {
  78. const _RuleElem(this.data);
  79. final Rule data;
  80. @override
  81. Widget build(BuildContext context) {
  82. final value = data.value;
  83. Widget? option;
  84. if (value is RuleValueBool) {
  85. option = Switch(
  86. value: value.value,
  87. activeColor: Colors.blue,
  88. onChanged: data.isLockAll
  89. ? null
  90. : (b) {
  91. value.value = b;
  92. controller.rules.update((val) {
  93. val![data.id] = data;
  94. });
  95. });
  96. }
  97. if (value is RuleValueArrStr) {
  98. option = Row(
  99. mainAxisSize: MainAxisSize.min,
  100. children: value.options
  101. .map((e) => _Radio(
  102. value: e.value,
  103. show: e.show,
  104. groupValue: value.value,
  105. onChanged: data.isLockAll
  106. ? null
  107. : (v) {
  108. if (v != null) {
  109. value.value = v;
  110. controller.rules.update((val) {
  111. val![data.id] = data;
  112. });
  113. }
  114. }))
  115. .toList(),
  116. );
  117. }
  118. return Row(
  119. mainAxisSize: MainAxisSize.min,
  120. children: [
  121. SizedBox(width: 110, child: Text(data.name, textAlign: TextAlign.end)),
  122. const SizedBox(width: 22),
  123. IconButton(
  124. onPressed: !data.isAllowEditAppLock || data.isLockAll
  125. ? null
  126. : () {
  127. controller.rules.update((val) {
  128. val![data.id]!.isAppLock = !val[data.id]!.isAppLock;
  129. });
  130. },
  131. icon: Icon(
  132. data.isAppLock ? Icons.lock_outline : Icons.lock_open_outlined,
  133. color: data.isAppLock ? Colors.deepOrange : Colors.amber,
  134. )),
  135. const SizedBox(width: 12),
  136. option!,
  137. ],
  138. );
  139. }
  140. }
  141. class _Radio extends StatelessWidget {
  142. const _Radio(
  143. {required this.value,
  144. required this.show,
  145. required this.groupValue,
  146. required this.onChanged});
  147. final String value;
  148. final String show;
  149. final String groupValue;
  150. final void Function(String?)? onChanged;
  151. @override
  152. Widget build(BuildContext context) {
  153. return Row(
  154. mainAxisSize: MainAxisSize.min,
  155. children: [
  156. Radio(
  157. activeColor: Colors.blue,
  158. value: value,
  159. groupValue: groupValue,
  160. onChanged: onChanged),
  161. Text(show),
  162. ],
  163. );
  164. }
  165. }