如何添加Image.file作为Circle化身的背景图像



在我的应用程序中,用户无法拾取图片并将其放入应用程序,而我需要放置的图像不是circleavatar的背景图像。我通过这个软件包构建了一个屏幕:image_picker,手机可以在这个屏幕上拾取图像,但我无法将图像放在圆圈化身的背景中。这是我的代码:

File _image;
final picker = ImagePicker();
Future getImage() async {
final pickedFile = await picker.getImage(source: ImageSource.camera);

setState(() {
if (pickedFile != null) {
_image = File(pickedFile.path);
} else {
print('No image selected.');
}
});
}

和构建方法:

_image == null ? CircleAvatar(
radius: 60,
backgroundColor: Colors.yellow,
child: Text(IfUserProfile.firstname[0],),
): CircleAvatar(
radius: 60,
backgroundColor: Colors.yellow,
child: Image.file(_image),),
SizedBox(
height: 4,
),
GestureDetector(
onTap: getImage,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [Icon(Icons.add_a_photo,color: Colors.white,size: 18,),
Text('   Aggiungi un immagine profilo',style: TextStyle(fontSize: 10,color: Colors.white),),
]),
),

CircleAvatar具有backgroundImage属性。试试下面的

CircleAvatar(
..
backgroundImage: FileImage(_image),
),

CircleAvatar有一个属性就是为了这个。尝试:

CircleAvatar(
...
backgroundImage: ...,
);

最新更新