dialog_settings.dart 3.9 KB

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