|
| 1 | +import 'package:flutter_colorpicker/flutter_colorpicker.dart'; |
| 2 | + |
| 3 | +import '/imports.dart'; |
| 4 | + |
| 5 | +class LabelEditorController extends GetxController { |
| 6 | + final _api = Get.find<ApiService>(); |
| 7 | + final _labels = Get.find<LabelsController>(); |
| 8 | + |
| 9 | + final loading = false.obs; |
| 10 | + final edit = Rxn<LabelInfo>(); |
| 11 | + final formKey = GlobalKey<FormState>(); |
| 12 | + final title = TextEditingController(); |
| 13 | + final description = TextEditingController(); |
| 14 | + final color = Colors.green.obs; |
| 15 | + final show_on_sidebar = false.obs; |
| 16 | + |
| 17 | + LabelEditorController({LabelInfo? edit}) { |
| 18 | + if (edit != null) this.edit.value = edit; |
| 19 | + } |
| 20 | + |
| 21 | + Future<void> showColorPicker() async { |
| 22 | + final result = await Get.dialog<Color?>(AlertDialog( |
| 23 | + titlePadding: const EdgeInsets.all(0), |
| 24 | + contentPadding: const EdgeInsets.all(0), |
| 25 | + content: SingleChildScrollView( |
| 26 | + child: MaterialPicker( |
| 27 | + pickerColor: color.value, |
| 28 | + onColorChanged: (color) {}, |
| 29 | + // enableLabel: _enableLabel, |
| 30 | + // portraitOnly: _portraitOnly, |
| 31 | + ), |
| 32 | + ), |
| 33 | + )); |
| 34 | + print(result); |
| 35 | + } |
| 36 | + |
| 37 | + Future<void> submit() async { |
| 38 | + if (!formKey.currentState!.validate()) { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + try { |
| 43 | + loading.value = true; |
| 44 | + |
| 45 | + final result = await _api.createLabel( |
| 46 | + title: title.text, |
| 47 | + description: description.text, |
| 48 | + color: color.value.toHexString(), |
| 49 | + show_on_sidebar: show_on_sidebar.value, |
| 50 | + ); |
| 51 | + |
| 52 | + _labels.items.insert(0, result.getOrThrow()); |
| 53 | + title.text = ''; |
| 54 | + description.text = ''; |
| 55 | + } finally { |
| 56 | + loading.value = false; |
| 57 | + } |
| 58 | + // Navigator.of(context).pop(c.selected.value) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +class LabelEditor extends StatelessWidget { |
| 63 | + final LabelEditorController controller; |
| 64 | + final labels = Get.find<LabelsController>(); |
| 65 | + |
| 66 | + LabelEditor({ |
| 67 | + super.key, |
| 68 | + LabelInfo? edit, |
| 69 | + }) : controller = Get.put(LabelEditorController(edit: edit)); |
| 70 | + |
| 71 | + @override |
| 72 | + Widget build(BuildContext context) { |
| 73 | + return bottomSheet( |
| 74 | + context, |
| 75 | + child: Padding( |
| 76 | + padding: const EdgeInsets.all(8.0), |
| 77 | + child: Obx(() { |
| 78 | + final loading = controller.loading.value; |
| 79 | + |
| 80 | + return Form( |
| 81 | + key: controller.formKey, |
| 82 | + child: ListView( |
| 83 | + shrinkWrap: true, |
| 84 | + children: [ |
| 85 | + TextFormField( |
| 86 | + enabled: !loading, |
| 87 | + controller: controller.title, |
| 88 | + decoration: InputDecoration( |
| 89 | + label: Text(t.labels_editor_title), |
| 90 | + hintText: t.labels_editor_title_hint, |
| 91 | + ), |
| 92 | + validator: (value) { |
| 93 | + if (value == null || value.length < 3) { |
| 94 | + return t.labels_editor_title_invalid; |
| 95 | + } |
| 96 | + return null; |
| 97 | + }, |
| 98 | + ), |
| 99 | + Padding( |
| 100 | + padding: const EdgeInsets.only(top: 8), |
| 101 | + ), |
| 102 | + TextFormField( |
| 103 | + enabled: !loading, |
| 104 | + controller: controller.description, |
| 105 | + decoration: InputDecoration( |
| 106 | + label: Text(t.labels_editor_description), |
| 107 | + hintText: t.labels_editor_description_hint, |
| 108 | + ), |
| 109 | + minLines: 1, |
| 110 | + maxLines: 5, |
| 111 | + ), |
| 112 | + Padding( |
| 113 | + padding: const EdgeInsets.only(top: 8), |
| 114 | + ), |
| 115 | + Obx(() { |
| 116 | + final color = controller.color.value; |
| 117 | + |
| 118 | + return Row( |
| 119 | + children: [ |
| 120 | + SizedBox( |
| 121 | + width: 40, |
| 122 | + height: 40, |
| 123 | + child: InkWell( |
| 124 | + onTap: controller.showColorPicker, |
| 125 | + child: Container( |
| 126 | + decoration: BoxDecoration( |
| 127 | + border: Border.all( |
| 128 | + color: context.theme.colorScheme.outlineVariant, |
| 129 | + ), |
| 130 | + color: color, |
| 131 | + borderRadius: BorderRadius.circular(4), |
| 132 | + ), |
| 133 | + ), |
| 134 | + ), |
| 135 | + ), |
| 136 | + ], |
| 137 | + ); |
| 138 | + }), |
| 139 | + Padding( |
| 140 | + padding: const EdgeInsets.only(top: 8), |
| 141 | + ), |
| 142 | + Obx(() { |
| 143 | + final show_on_sidebar = controller.show_on_sidebar.value; |
| 144 | + return Row( |
| 145 | + spacing: 8, |
| 146 | + children: [ |
| 147 | + Switch( |
| 148 | + value: show_on_sidebar, |
| 149 | + onChanged: (next) => |
| 150 | + controller.show_on_sidebar.value = next, |
| 151 | + ), |
| 152 | + Text(t.labels_editor_show_on_sidebar), |
| 153 | + ], |
| 154 | + ); |
| 155 | + }), |
| 156 | + primaryButton( |
| 157 | + block: true, |
| 158 | + label: t.save_changes, |
| 159 | + onPressed: controller.submit, |
| 160 | + loading: loading, |
| 161 | + ), |
| 162 | + ], |
| 163 | + ), |
| 164 | + ); |
| 165 | + }), |
| 166 | + ), |
| 167 | + ); |
| 168 | + } |
| 169 | +} |
0 commit comments