如何显示用户图像?
How can I displaying user image?
我正在显示用户头像,我有 3 个选项可以显示。第一个是当前选取的图像,它是从相机或库中选取的,如果有图像就会显示出来。然后用户可以保护它,这样当用户重新启动应用程序时它也会显示,在这种情况下我会显示保存的图像。然后是我的第三个选项,如果没有保存图片并且此时也没有选择图片,那么我将显示默认图像,这在我的资产中。这是我的问题,我该怎么做,因为如果用户删除当前保存的图片,那么如果用户删除当前保存的图片,那么如果他改回“未拍摄照片”,那么默认图片将再次显示。但是我如何在我的代码中实现它?图片作为文件保存在存储中,也作为 url 在 firestore 中保存
这是我的圆圈头像,用于显示保存图像和保存前选择的图像。
Container(
child: Center(
child:
CircleAvatar(
radius: 70,
foregroundImage: _pickedImage!=null? FileImage(_pickedImage): NetworkImage ( userData.url),
),
),
decoration: new BoxDecoration(
shape: BoxShape.circle,
这是我的方法:
onPressed: () async {
if (_formKey.currentState.validate()) {
final ref= FirebaseStorage.instance.ref().child('user_profile_pictures').child(user.uid+'.jpg');
await ref.putFile(_pickedImage);
final url = await ref.getDownloadURL();
_curenturl= url;
String authError =
await DatbaseService(uid: user.uid).updateUserData(
_currentusername ?? userData.user,
_currentfullname ?? userData.fullname,
_currentemail ?? userData.email,
_currentpassword ?? userData.password,
_curenturl ??userData.url,
);
if (authError != null) {
这是我将当前 url 和 userData.url
设置为 null 的方法
onTap: () {
DatbaseService(uid: user.uid).updateUserData(
_currentusername ?? userData.user,
_currentfullname ?? userData.fullname,
_currentemail ?? userData.email,
_currentpassword ?? userData.password,
_curenturl =null,
);
Navigator.pop(context);
},
正如您所看到的,我再次将 url 设置为空。我这样做是为了不再保存图片。然后我想显示我资产中的默认图像。我试图用那张图片更新我当前的 url b 但 url 是一个字符串,我的图片是资产图像。
你可以试试我的代码:
ClipRRect(
borderRadius: BorderRadius.circular(60),
child: Container(
height: 100,
width: 100,
decoration: BoxDecoration(
color: Colors.grey.shade300,
),
child: image != null
? Image.file(
image,
fit: BoxFit.cover,
)
: networkImg != null && networkImg != ''
? Image.network(
networkImg,
fit: BoxFit.cover,
)
: Image.asset(
// Your image path is here when image is no available.
),
),
),
请告诉我它是否有效。
我正在显示用户头像,我有 3 个选项可以显示。第一个是当前选取的图像,它是从相机或库中选取的,如果有图像就会显示出来。然后用户可以保护它,这样当用户重新启动应用程序时它也会显示,在这种情况下我会显示保存的图像。然后是我的第三个选项,如果没有保存图片并且此时也没有选择图片,那么我将显示默认图像,这在我的资产中。这是我的问题,我该怎么做,因为如果用户删除当前保存的图片,那么如果用户删除当前保存的图片,那么如果他改回“未拍摄照片”,那么默认图片将再次显示。但是我如何在我的代码中实现它?图片作为文件保存在存储中,也作为 url 在 firestore 中保存 这是我的圆圈头像,用于显示保存图像和保存前选择的图像。
Container(
child: Center(
child:
CircleAvatar(
radius: 70,
foregroundImage: _pickedImage!=null? FileImage(_pickedImage): NetworkImage ( userData.url),
),
),
decoration: new BoxDecoration(
shape: BoxShape.circle,
这是我的方法:
onPressed: () async {
if (_formKey.currentState.validate()) {
final ref= FirebaseStorage.instance.ref().child('user_profile_pictures').child(user.uid+'.jpg');
await ref.putFile(_pickedImage);
final url = await ref.getDownloadURL();
_curenturl= url;
String authError =
await DatbaseService(uid: user.uid).updateUserData(
_currentusername ?? userData.user,
_currentfullname ?? userData.fullname,
_currentemail ?? userData.email,
_currentpassword ?? userData.password,
_curenturl ??userData.url,
);
if (authError != null) {
这是我将当前 url 和 userData.url
设置为 null 的方法 onTap: () {
DatbaseService(uid: user.uid).updateUserData(
_currentusername ?? userData.user,
_currentfullname ?? userData.fullname,
_currentemail ?? userData.email,
_currentpassword ?? userData.password,
_curenturl =null,
);
Navigator.pop(context);
},
正如您所看到的,我再次将 url 设置为空。我这样做是为了不再保存图片。然后我想显示我资产中的默认图像。我试图用那张图片更新我当前的 url b 但 url 是一个字符串,我的图片是资产图像。
你可以试试我的代码:
ClipRRect(
borderRadius: BorderRadius.circular(60),
child: Container(
height: 100,
width: 100,
decoration: BoxDecoration(
color: Colors.grey.shade300,
),
child: image != null
? Image.file(
image,
fit: BoxFit.cover,
)
: networkImg != null && networkImg != ''
? Image.network(
networkImg,
fit: BoxFit.cover,
)
: Image.asset(
// Your image path is here when image is no available.
),
),
),
请告诉我它是否有效。