dialog_settings.dart 4.0 KB

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