|
1 | | -import 'dart:convert'; |
2 | | -import 'dart:io'; |
3 | 1 | import 'dart:math' as math; |
4 | 2 | import 'package:flutter/material.dart'; |
5 | | -import 'package:file_picker/file_picker.dart'; |
6 | 3 | import 'package:flutter/services.dart'; |
7 | 4 | import 'package:uuid/v4.dart'; |
8 | 5 | import 'package:wox/api/wox_api.dart'; |
9 | 6 | import 'package:wox/components/wox_ai_model_selector_view.dart'; |
10 | 7 | import 'package:wox/components/wox_button.dart'; |
11 | 8 | import 'package:wox/components/wox_dropdown_button.dart'; |
12 | 9 | import 'package:wox/components/wox_hotkey_recorder_view.dart'; |
13 | | -import 'package:wox/components/wox_image_view.dart'; |
| 10 | +import 'package:wox/components/wox_image_selector.dart'; |
14 | 11 | import 'package:wox/components/wox_textfield.dart'; |
15 | 12 | import 'package:wox/components/wox_checkbox.dart'; |
16 | 13 | import 'package:wox/components/wox_checkbox_tile.dart'; |
@@ -152,124 +149,20 @@ class _WoxSettingPluginTableUpdateState extends State<WoxSettingPluginTableUpdat |
152 | 149 | return Get.find<WoxSettingController>().tr(key); |
153 | 150 | } |
154 | 151 |
|
155 | | - Widget _buildWoxImageEditor(PluginSettingValueTableColumn column) { |
156 | | - WoxImage? currentImage; |
157 | | - dynamic imgJson = getValue(column.key); |
158 | | - |
| 152 | + WoxImage getWoxImageValue(String key) { |
| 153 | + final imgJson = getValue(key); |
| 154 | + if (imgJson is WoxImage) { |
| 155 | + return imgJson; |
| 156 | + } |
159 | 157 | if (imgJson is String && imgJson == "") { |
160 | | - currentImage = WoxImage(imageType: WoxImageTypeEnum.WOX_IMAGE_TYPE_EMOJI.code, imageData: "🤖"); |
161 | | - } else if (imgJson is WoxImage) { |
162 | | - currentImage = imgJson; |
163 | | - } else { |
164 | | - //image sholuld be WoxImage map |
165 | | - try { |
166 | | - currentImage = WoxImage.fromJson(imgJson); |
167 | | - } catch (e) { |
168 | | - currentImage = WoxImage(imageType: WoxImageTypeEnum.WOX_IMAGE_TYPE_EMOJI.code, imageData: "🤖"); |
169 | | - } |
| 158 | + return WoxImage(imageType: WoxImageTypeEnum.WOX_IMAGE_TYPE_EMOJI.code, imageData: "🤖"); |
170 | 159 | } |
171 | 160 |
|
172 | | - return Row( |
173 | | - crossAxisAlignment: CrossAxisAlignment.center, |
174 | | - children: [ |
175 | | - Container( |
176 | | - width: 80, |
177 | | - height: 80, |
178 | | - decoration: BoxDecoration(border: Border.all(color: getThemeSubTextColor().withAlpha(76)), borderRadius: BorderRadius.circular(8)), |
179 | | - child: ClipRRect( |
180 | | - borderRadius: BorderRadius.circular(8), |
181 | | - child: Center( |
182 | | - // Center the preview content (especially emoji) in the 80x80 box |
183 | | - child: WoxImageView(woxImage: currentImage, width: 80, height: 80), |
184 | | - ), |
185 | | - ), |
186 | | - ), |
187 | | - const SizedBox(width: 16), |
188 | | - Wrap( |
189 | | - spacing: 8, |
190 | | - runSpacing: 8, |
191 | | - children: [ |
192 | | - WoxButton.secondary( |
193 | | - text: tr('ui_image_editor_emoji'), |
194 | | - icon: Icon(Icons.emoji_emotions_outlined, size: 14, color: getThemeTextColor()), |
195 | | - padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8), |
196 | | - onPressed: () async { |
197 | | - final emojiResult = await _showEmojiPicker(context); |
198 | | - if (emojiResult != null && emojiResult.isNotEmpty) { |
199 | | - final newImage = WoxImage(imageType: WoxImageTypeEnum.WOX_IMAGE_TYPE_EMOJI.code, imageData: emojiResult); |
200 | | - updateValue(column.key, newImage); |
201 | | - setState(() {}); |
202 | | - } |
203 | | - }, |
204 | | - ), |
205 | | - WoxButton.secondary( |
206 | | - text: tr('ui_image_editor_upload_image'), |
207 | | - icon: Icon(Icons.file_upload_outlined, size: 14, color: getThemeTextColor()), |
208 | | - padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8), |
209 | | - onPressed: () async { |
210 | | - final result = await FilePicker.platform.pickFiles(type: FileType.image, allowMultiple: false); |
211 | | - |
212 | | - if (result != null && result.files.isNotEmpty && result.files.first.path != null) { |
213 | | - final filePath = result.files.first.path!; |
214 | | - final file = File(filePath); |
215 | | - if (await file.exists()) { |
216 | | - final bytes = await file.readAsBytes(); |
217 | | - final base64Image = base64Encode(bytes); |
218 | | - |
219 | | - final newImage = WoxImage(imageType: WoxImageTypeEnum.WOX_IMAGE_TYPE_BASE64.code, imageData: "data:image/png;base64,$base64Image"); |
220 | | - |
221 | | - updateValue(column.key, newImage); |
222 | | - setState(() {}); |
223 | | - } |
224 | | - } |
225 | | - }, |
226 | | - ), |
227 | | - ], |
228 | | - ), |
229 | | - ], |
230 | | - ); |
231 | | - } |
232 | | - |
233 | | - Future<String?> _showEmojiPicker(BuildContext context) async { |
234 | | - final commonEmojis = ["🤖", "👨", "👩", "🧠", "💡", "🔍", "📊", "📈", "📝", "🛠", "⚙️", "🧩", "🎮", "🎯", "🏆", "🎨", "🎭", "🎬", "📱", "💻"]; |
235 | | - |
236 | | - String? selectedEmoji; |
237 | | - |
238 | | - await showDialog( |
239 | | - context: context, |
240 | | - builder: (context) { |
241 | | - return AlertDialog( |
242 | | - title: Text(tr('ui_select_emoji')), |
243 | | - content: SizedBox( |
244 | | - width: 300, |
245 | | - height: 200, |
246 | | - child: GridView.builder( |
247 | | - gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 5, childAspectRatio: 1), |
248 | | - itemCount: commonEmojis.length, |
249 | | - itemBuilder: (context, index) { |
250 | | - return InkWell( |
251 | | - onTap: () { |
252 | | - selectedEmoji = commonEmojis[index]; |
253 | | - Navigator.pop(context); |
254 | | - }, |
255 | | - child: Center(child: Text(commonEmojis[index], style: const TextStyle(fontSize: 24))), |
256 | | - ); |
257 | | - }, |
258 | | - ), |
259 | | - ), |
260 | | - actions: [ |
261 | | - WoxButton.secondary( |
262 | | - text: tr('ui_cancel'), |
263 | | - onPressed: () { |
264 | | - Navigator.pop(context); |
265 | | - }, |
266 | | - ), |
267 | | - ], |
268 | | - ); |
269 | | - }, |
270 | | - ); |
271 | | - |
272 | | - return selectedEmoji; |
| 161 | + try { |
| 162 | + return WoxImage.fromJson(imgJson); |
| 163 | + } catch (e) { |
| 164 | + return WoxImage(imageType: WoxImageTypeEnum.WOX_IMAGE_TYPE_EMOJI.code, imageData: "🤖"); |
| 165 | + } |
273 | 166 | } |
274 | 167 |
|
275 | 168 | // Load all tools list |
@@ -509,7 +402,15 @@ class _WoxSettingPluginTableUpdateState extends State<WoxSettingPluginTableUpdat |
509 | 402 | ), |
510 | 403 | ); |
511 | 404 | case PluginSettingValueType.pluginSettingValueTableColumnTypeWoxImage: |
512 | | - return Expanded(child: _buildWoxImageEditor(column)); |
| 405 | + return Expanded( |
| 406 | + child: WoxImageSelector( |
| 407 | + value: getWoxImageValue(column.key), |
| 408 | + onChanged: (newImage) { |
| 409 | + updateValue(column.key, newImage); |
| 410 | + setState(() {}); |
| 411 | + }, |
| 412 | + ), |
| 413 | + ); |
513 | 414 | case PluginSettingValueType.pluginSettingValueTableColumnTypeTextList: |
514 | 415 | var columnValues = getValue(column.key); |
515 | 416 | return Expanded( |
|
0 commit comments