| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- import 'dart:collection';
- import 'package:app_business/service/api.dart';
- import 'package:track_common/widget.dart';
- class SettingsController extends GetxController {
- final rules = HashMap<int, Rule>().obs;
- @override
- void onInit() {
- final id = Get.arguments as int;
- Get.find<ApiService>()
- .stub
- .toActivityRulesQuery(IdRequest()..id = Int64(id))
- .then((p0) {
- rules.update((val) {
- for (final (i, e) in p0.list.indexed) {
- val![e.arrId] = Rule()
- ..id = e.arrId
- ..idx = i
- ..name = e.arName
- ..isLock = !e.isLock
- ..value = e.arType == 1
- ? (RuleValueBool()..value = e.arValue == 'true')
- : (RuleValueArrStr()
- ..value = e.arValue
- ..options = e.arValueSelectList
- .map((e) => RuleValueArrStrOption()
- ..value = e.arValueSelect
- ..show = e.arStrSelect)
- .toList());
- }
- });
- });
- super.onInit();
- }
- }
- class DialogSettings extends GetView<SettingsController> {
- 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(context)),
- Padding(
- padding: const EdgeInsets.symmetric(horizontal: 45),
- child: AppButton.confirm(
- width: double.infinity,
- height: 38,
- child: const Text('确认'),
- onPressed: () {
- final r = controller.rules.value.values;
- Get.back(result: r);
- }))
- ],
- ),
- );
- });
- }
- Widget _list(BuildContext context) {
- final l = controller.rules.value.values.toList();
- l.sort((a, b) => a.idx.compareTo(b.idx));
- return SizedBox(
- height: context.height * 0.6,
- width: 420,
- child: ListView(
- children: l.map((data) => _RuleElem(data)).toList(),
- ));
- }
- }
- class _RuleElem extends GetView<SettingsController> {
- 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: data.isLock
- ? null
- : (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.value,
- show: e.show,
- groupValue: value.value,
- onChanged: data.isLock
- ? null
- : (v) {
- if (v != null) {
- value.value = v;
- controller.rules.update((val) {
- val![data.id] = data;
- });
- }
- }))
- .toList(),
- );
- }
- return Row(
- mainAxisSize: MainAxisSize.min,
- children: [
- SizedBox(width: 110, child: Text(data.name, textAlign: TextAlign.end)),
- const SizedBox(width: 22),
- option!,
- ],
- );
- }
- }
- class _Radio extends StatelessWidget {
- const _Radio(
- {required this.value,
- required this.show,
- required this.groupValue,
- required this.onChanged});
- final String value;
- final String show;
- 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(show),
- ],
- );
- }
- }
|