dialog_settings.dart 4.0 KB

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