首页 > 解决方案 > 个人资料图片不会保持保存并正确适配

问题描述

我正在制作个人资料页面,但我不明白为什么我选择的图片没有保存并正确放置在现场。我使用此代码上传图片

Future _getImage() async {
    final pickedFile = await picker.getImage(source: ImageSource.gallery);

    setState(() {
      _image = File(pickedFile.path);
    });
  }
    Future uploadPic(BuildContext context) async {
        String fileName = basename(_image.path);
        StorageReference firebaseStorageRef =
            FirebaseStorage.instance.ref().child(fileName);
        StorageUploadTask uploadTask = firebaseStorageRef.putFile(_image);
        StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
        setState(() {
          print("Profile Picture uploaded");
          Scaffold.of(context).showSnackBar(SnackBar(
            content: Text('Profile Picture Uploaded'),
          ));
        });
      }

但我不知道我怎么能在这里调用函数来自动保存

child: Container(
                  height: 84,
                  width: 84,

                  //profilepic
                  child: GestureDetector(
                    onTap: _getImage,
                    child: CircleAvatar(
                        radius: 100,
                        child: ClipOval(
                          child: (_image != null)
                              ? Image.file(
                                  _image,
                                  fit: BoxFit.fill,
                                )
                              : Image.network(
                                  'https://icon-library.com/images/add-image-icon/add-image-icon-14.jpg',
                                  fit: BoxFit.fill,
                                ),
                        )),
                  ),
                ),

当我选择一张照片时,它不合适,看起来像这样 在此处输入图像描述

标签: flutter

解决方案


我重新阅读了你的问题,为了调用你的uploadPic()函数,使onTap函数像这样:

onTap: (){
  uploadPic(context);
}

至于其余的代码,我需要知道 CircleAvatar 长什么样,_getImage 是什么。


推荐阅读