英文:
Flutter GETX: How to update Get.defaultDialog(...) data from the controller
问题
I can provide the translated code part as requested:
一个按钮打开带有GETX(Get.defaultDialog)的警报对话框,并且我在对话框中有一个图像选择器按钮,使用Image.File(...),但当我从库中选择图像时,图像不会更新,除非我关闭对话框并打开另一个。
我希望在使用GETX选择图像后更新Image.File。
我尝试在控制器函数中使用update(),但没有更新,我还尝试在对话框中使用GetxBuilder,但出现错误。
第一个文件
imageController controller = Get.put(imageController());
alertAddProduct() {
Get.defaultDialog(
title: "添加产品",
middleText: "添加产品",
//radius: 20,
barrierDismissible: false,
content: Column(
children: [
Container(
child: controller.imagePath != null ? Image.file(controller.imagePath,errorBuilder: (context, error, stackTrace) {
return const Text("Bruh");
},): const Text("选择图像")
),
ElevatedButton(
onPressed: () {
controller.getImage();
},
child: const Text("选择图像"),
)
]
),
actions: [
ElevatedButton(
onPressed: () {
// 正在处理
},
child: const Text("是")),
ElevatedButton(
onPressed: () {
Get.back();
},
child: const Text("否"),
),
],
);
}
和控制器文件
import 'dart:io';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
class imageController extends GetxController {
// 忽略:prefer_typing_uninitialized_variables
var imagePath;
void getImage() async {
final ImagePicker picker = ImagePicker();
final XFile? image = await picker.pickImage(
source: ImageSource.gallery,
);
if (image != null) {
print("----------------------------" + image.path);
imagePath = File(image.path);
update();
} else {
print("---------------------------- 请选择图像");
}
}
}
和主文件
floatingActionButton: FloatingActionButton(
elevation: 5,
onPressed: (() {
alertAddProduct();
}),
child: const Icon(Icons.add),
),
英文:
A button opens alert dialog with GETX (Get.defaultDialog) and I have an image picker button with Image.File(...) in the dialog and when I pick the image from the gallery the image doesnt get updated only if I leave the dialog and open another one
I want to update the Image.File after selecting the image with GETX
I tried update(); in the controller function didnt update and I tired the GetxBuilder in the dialog but I get an error
first file
imageController controller = Get.put(imageController());
alertAddProduct() {
Get.defaultDialog(
title: "Add product",
middleText: "Add product",
//radius: 20,
barrierDismissible: false,
content: Column(
children: [
Container(
child: controller.imagePath != null ? Image.file(controller.imagePath,errorBuilder: (context, error, stackTrace) {
return const Text("Bruh");
},): const Text("Pick image")
),
ElevatedButton(
onPressed: () {
controller.getImage();
},
child: const Text("Select image"),
)
]
),
actions: [
ElevatedButton(
onPressed: () {
// working on it
},
child: const Text("Yes")),
ElevatedButton(
onPressed: () {
Get.back();
},
child: const Text("No"),
),
],);
}
and the controller file
import 'dart:io';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
class imageController extends GetxController {
// ignore: prefer_typing_uninitialized_variables
var imagePath;
void getImage() async {
final ImagePicker picker = ImagePicker();
final XFile? image = await picker.pickImage(
source: ImageSource.gallery,
);
if (image != null) {
print("----------------------------" + image.path);
imagePath = File(image.path);
update();
} else {
print("---------------------------- Please select image");
}
}
}
and the main file
floatingActionButton: FloatingActionButton(
elevation: 5,
onPressed: (() {
alertAddProduct();
}),
child: const Icon(Icons.add),
),
答案1
得分: 1
如果您尝试在对话框的**update();**方法中进行任何更新,那么不会起作用,因为据我所知,这个方法只能更新上下文,而不能更新任何类型的覆盖物,所以如果您想要在选择对话框中的“显示”后更新图像,您需要重新构建它,以便它可以反映您的图像。
showDialog(
context: context,
builder: (context) {
String contentText = "对话框内容";
return StatefulBuilder(
builder: (context, setState) {
return AlertDialog(
title: Text("对话框标题"),
content: Text(contentText),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.pop(context),
child: Text("取消"),
),
TextButton(
onPressed: () {
setState(() {
contentText = "更改后的对话框内容";
});
},
child: Text("更改"),
),
],
);
},
);
},
);
英文:
In case You are trying to make any updates in dialog update(); method will not work because till I know this method can update the context only not any kind of overlay,
so if you want to update the image after selecting Show in the dialog you have to rebuild it so it can reflect your image.
showDialog(
context: context,
builder: (context) {
String contentText = "Content of Dialog";
return StatefulBuilder(
builder: (context, setState) {
return AlertDialog(
title: Text("Title of Dialog"),
content: Text(contentText),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.pop(context),
child: Text("Cancel"),
),
TextButton(
onPressed: () {
setState(() {
contentText = "Changed Content of Dialog";
});
},
child: Text("Change"),
),
],
);
},
);
},
);
答案2
得分: 0
将您的imagePath变量更改为obs,并将UI包装在OBX中。
例如:
在Controller中
var imagePath = ''.obs;
....
imagePath.value = File(image.path);
// update ---> remove update line
在UI中用Obx包装,例如:
....
Obx(()=>Container(
child:
//Add string instead of null on comparing
controller.imagePath.value != '' ?
Image.file(controller.imagePath.value,errorBuilder:
(context, error, stackTrace) {
return const Text("Bruh");},) : const Text("Pick
image")
)),
....
英文:
change you imagePath variable to obs and wrap the UI with OBX
eg:
In the Controller
var imagePath = ''.obs ;
....
imagePath.value = File(image.path);
// update ---> remove update line
In the UI wrap with Obx eg:
....
Obx(()=>Container(
child:
//Add string instead of null on comparing
controller.imagePath.value != '' ?
Image.file(controller.imagePath.value,errorBuilder:
(context, error, stackTrace) {
return const Text("Bruh");},) : const Text("Pick
image")
)),
....
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论