Closed xp664221812 closed 3 years ago
class SetAvatarLogic extends GetxController { final state = SetAvatarState(); File selectedFile;
Future getImage(bool isTakingPhoto) async { Navigator.pop(Get.context); var image = await ImagePicker.pickImage( source: isTakingPhoto ? ImageSource.camera : ImageSource.gallery); if (image != null) { selectedFile = File(image.path); } return image; } }
class SetAvatarPageextends StatelessWidget { final SetAvatarLogic logic = Get.put(SetAvatarLogic()); .......... //下面这段肯定是错的,我不知道该怎么写 child: Obx(() { return Image( image: logic.selectedFile == null ? AssetImage("assets/tab_session_pressed.png") : FileImage(logic.selectedFile)); }),```
class SetAvatarLogic extends GetxController {
final state = SetAvatarState();
Rx<File> selectedFile;
Future getImage(bool isTakingPhoto) async {
Navigator.pop(Get.context);
var image = await ImagePicker.pickImage(
source: isTakingPhoto ? ImageSource.camera : ImageSource.gallery);
if (image != null) {
selectedFile = File(image.path).obs;
}
return image;
}
}
class SetAvatarPageextends StatelessWidget {
final SetAvatarLogic logic = Get.put(SetAvatarLogic());
..........
//下面这段肯定是错的,我不知道该怎么写
child: Obx(() {
return Image(
image: logic.selectedFile.value == null
? AssetImage("assets/tab_session_pressed.png")
: FileImage(logic.selectedFile));
}),```
你试试这个代码,你运行上面你自己写的的代码应该会报错的,因为Obx方法里面必须含有响应式变量
关于Getx你知道怎么使用,可以看看这篇文章:https://juejin.cn/post/6924104248275763208
好像还是不行,你这写样直接报错了,我把asset里面的路径也改成rxstring了,不报错了,但是选了没有更新image
好像还是不行,你这写样直接报错了,我把asset里面的路径也改成rxstring了,不报错了,但是选了没有更新image
你的代码问题是Obx刷新方法里面没用到响应式变量,我直接对着你不完整的代码块上改,跑不起很正常
好像还是不行,你这写样直接报错了,我把asset里面的路径也改成rxstring了,不报错了,但是选了没有更新image
FileImage(logic.selectedFile.value))
这行少写了 .value
实体刷新需要这样写
好像还是不行,你这写样直接报错了,我把asset里面的路径也改成rxstring了,不报错了,但是选了没有更新image
FileImage(logic.selectedFile.value))
这行少写了 .value
class SetAvatarLogic extends GetxController { final state = SetAvatarState(); File selectedFile;
Future getImage(bool isTakingPhoto) async { Navigator.pop(Get.context); var image = await ImagePicker.pickImage( source: isTakingPhoto ? ImageSource.camera : ImageSource.gallery); if (image != null) { selectedFile = File(image.path); } return image; } }
class SetAvatarPageextends StatelessWidget { final SetAvatarLogic logic = Get.put(SetAvatarLogic()); .......... //下面这段肯定是错的,我不知道该怎么写 child: Obx(() { return Image( image: logic.selectedFile == null ? AssetImage("assets/tab_session_pressed.png") : FileImage(logic.selectedFile)); }),```