|
| 1 | +import '/imports.dart'; |
| 2 | + |
| 3 | +class LabelsPickerController extends GetxController { |
| 4 | + final labels = Get.find<LabelsController>(); |
| 5 | + final selected = RxList<LabelInfo>(); |
| 6 | + |
| 7 | + LabelsPickerController({List<String>? selected}) { |
| 8 | + selected ??= []; |
| 9 | + this.selected.value = |
| 10 | + labels.items.where((e) => selected!.contains(e.title)).toList(); |
| 11 | + } |
| 12 | + |
| 13 | + void toggle(LabelInfo info) { |
| 14 | + if (selected.contains(info)) { |
| 15 | + selected.remove(info); |
| 16 | + return; |
| 17 | + } |
| 18 | + selected.add(info); |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +class LabelsPicker extends StatelessWidget { |
| 23 | + final LabelsPickerController c; |
| 24 | + final labels = Get.find<LabelsController>(); |
| 25 | + |
| 26 | + final bool multiple; |
| 27 | + LabelsPicker({super.key, List<String>? selected, bool? multiple}) |
| 28 | + : c = Get.put(LabelsPickerController(selected: selected)), |
| 29 | + multiple = multiple ?? false; |
| 30 | + |
| 31 | + @override |
| 32 | + Widget build(BuildContext context) { |
| 33 | + return bottomSheet( |
| 34 | + context, |
| 35 | + child: Obx(() { |
| 36 | + final items = labels.items.value; |
| 37 | + |
| 38 | + return Column( |
| 39 | + children: [ |
| 40 | + TextField( |
| 41 | + decoration: InputDecoration( |
| 42 | + prefixIcon: Icon(Icons.search_outlined), |
| 43 | + label: Text(t.labels_search), |
| 44 | + hintText: t.labels_search_hint, |
| 45 | + ), |
| 46 | + ), |
| 47 | + Expanded( |
| 48 | + child: ListView.builder( |
| 49 | + itemCount: items.length, |
| 50 | + itemBuilder: (_, i) { |
| 51 | + return buildItem(items[i]); |
| 52 | + }, |
| 53 | + ), |
| 54 | + ), |
| 55 | + Padding( |
| 56 | + padding: const EdgeInsets.all(8.0), |
| 57 | + child: primaryButton( |
| 58 | + block: true, |
| 59 | + label: t.save_changes, |
| 60 | + onPressed: () => Navigator.of(context).pop(c.selected.value), |
| 61 | + ), |
| 62 | + ), |
| 63 | + ], |
| 64 | + ); |
| 65 | + }), |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + Widget buildItem(LabelInfo info) { |
| 70 | + return Obx(() { |
| 71 | + final selected = c.selected.value; |
| 72 | + |
| 73 | + return ListTile( |
| 74 | + title: Text(info.title), |
| 75 | + subtitle: Text( |
| 76 | + info.description, |
| 77 | + maxLines: 1, |
| 78 | + overflow: TextOverflow.ellipsis, |
| 79 | + ), |
| 80 | + trailing: Checkbox( |
| 81 | + value: selected.contains(info), |
| 82 | + onChanged: (next) => c.toggle(info), |
| 83 | + ), |
| 84 | + onTap: () => c.toggle(info), |
| 85 | + ); |
| 86 | + }); |
| 87 | + } |
| 88 | +} |
0 commit comments