xdd666t / getx_template

Used to generate the template code of GetX framework | Flutter GetX模板代码生成(一个有用的IDEA插件)
Apache License 2.0
298 stars 67 forks source link

第一次用get,请教个问题,怎么显示从相册或相机选择的图片 #3

Closed xp664221812 closed 3 years ago

xp664221812 commented 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)); }),```

xdd666t commented 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

xp664221812 commented 3 years ago

好像还是不行,你这写样直接报错了,我把asset里面的路径也改成rxstring了,不报错了,但是选了没有更新image

xdd666t commented 3 years ago

好像还是不行,你这写样直接报错了,我把asset里面的路径也改成rxstring了,不报错了,但是选了没有更新image

你的代码问题是Obx刷新方法里面没用到响应式变量,我直接对着你不完整的代码块上改,跑不起很正常

xdd666t commented 3 years ago

好像还是不行,你这写样直接报错了,我把asset里面的路径也改成rxstring了,不报错了,但是选了没有更新image

FileImage(logic.selectedFile.value))

这行少写了 .value

实体刷新需要这样写 image

xdd666t commented 3 years ago

好像还是不行,你这写样直接报错了,我把asset里面的路径也改成rxstring了,不报错了,但是选了没有更新image

FileImage(logic.selectedFile.value))

这行少写了 .value