import 'dart:collection'; import 'package:app_business/service/api.dart'; import 'package:track_common/widget.dart'; class Rule { var id = 0; var name = ''; RuleValue value = RuleValueBool(); } class RuleValue {} class RuleValueBool extends RuleValue { var value = false; } class RuleValueArrStr extends RuleValue { var value = ''; var options = []; } class SettingsController extends GetxController { final rules = HashMap().obs; @override void onInit() { final id = Get.arguments as int; Get.find() .stub .toActivityRulesQuery(IdRequest()..id = Int64(id)) .then((p0) { rules.update((val) { for (final e in p0.list) { val![e.arrId] = Rule() ..id = e.arrId ..name = e.arName ..value = e.arType == 1 ? (RuleValueBool()..value = e.isLock) : (RuleValueArrStr() ..value = e.arValue ..options = e.arValueSelectList.map((e) => e.arValueSelect).toList()); } }); }); super.onInit(); } } class DialogSettings extends GetView { const DialogSettings({super.key}); @override Widget build(BuildContext context) { return GetBuilder( init: SettingsController(), builder: (ctx) { return AlertDialog( surfaceTintColor: Colors.white, title: const Center(child: Text('规则设置')), content: Column( mainAxisSize: MainAxisSize.min, children: [ Obx(() => _list()), Padding( padding: const EdgeInsets.symmetric(horizontal: 45), child: AppButton.confirm( width: double.infinity, height: 38, child: const Text('确认'), onPressed: () {})) ], ), ); }); } Widget _list() { return Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: controller.rules.value.values.map((data) => _RuleElem(data)).toList(), ); } } class _RuleElem extends GetView { const _RuleElem(this.data); final Rule data; @override Widget build(BuildContext context) { final value = data.value; Widget? option; if (value is RuleValueBool) { option = Switch( value: value.value, activeColor: Colors.blue, onChanged: (b) { value.value = b; controller.rules.update((val) { val![data.id] = data; }); }); } if (value is RuleValueArrStr) { option = Row( mainAxisSize: MainAxisSize.min, children: value.options .map((e) => _Radio( value: e, groupValue: value.value, onChanged: (v) { if (v != null) { value.value = v; controller.rules.update((val) { val![data.id] = data; }); } })) .toList(), ); } return Row( mainAxisSize: MainAxisSize.min, children: [ SizedBox(width: 84, child: Text(data.name, textAlign: TextAlign.end)), const SizedBox(width: 22), option!, ], ); } } class _Radio extends StatelessWidget { const _Radio( {required this.value, required this.groupValue, required this.onChanged}); final String value; final String groupValue; final void Function(String?) onChanged; @override Widget build(BuildContext context) { return Row( mainAxisSize: MainAxisSize.min, children: [ Radio( activeColor: Colors.blue, value: value, groupValue: groupValue, onChanged: onChanged), Text(value), ], ); } }