I'm doing some testing with the CircleAvatar. I know that the background image would normally be obtained from the network, which is what the documentation shows:
CircleAvatar(
backgroundImage: NetworkImage(userAvatarUrl),
)
However, for testing purposes I just want to use asset images. I can't do this
leading: CircleAvatar(
backgroundImage: Image.asset('assets/horse.png'),
)
because as the error says
The argument type
Image
can't be assigned to the parameter typeImageProvider
.
How do I give an assets image to ImageProvider
?
If you're viewing an asset and it already has an image set, it's easy to change to a new image or even remove the image: Click the “Attachments” > “Remove Attachment”. Once the attachment is removed, you can upload a new attachment and set it as the asset image.
An avatar usually has a picture. To set the picture, you can use backgroundImage property which is of type ImageProvider . The below example uses NetworkImage to load the image. But you can also use other classes that extend ImageProvider such as MemoryImage , FileImage , and ResizeImage .
This property applies a background image to the CircleAvatar widget. child: The child property takes the widget to be placed below the CircleAvatar widget inside the widget tree or the widget to be displayed inside the circle. foregroundColor: This property holds the Color class (final) as the parameter value.
Use child property from CircleAvatar:
CircleAvatar(
child: Image.asset('assets/horse.png'),
);
or if you want to use the backgroundImage property use the asset provider.
CircleAvatar(
backgroundImage: AssetImage('assets/horse.png'),
);
Use avatar_view
/// 1. Circular AvatarView Without Border
AvatarView(
radius: 60,
borderColor: Colors.yellow,
isOnlyText: false,
text: Text('C', style: TextStyle(color: Colors.white, fontSize: 50),),
avatarType: AvatarType.CIRCLE,
backgroundColor: Colors.red,
imagePath: "assets/image_1.png",
placeHolder: Container(
child: Icon(Icons.person, size: 50,),
),
errorWidget: Container(
child: Icon(Icons.error, size: 50,),
),
),
SizedBox(height: 16,),
/// 2. Circular AvatarView With Border
AvatarView(
radius: 60,
borderWidth: 8,
borderColor: Colors.yellow,
avatarType: AvatarType.CIRCLE,
backgroundColor: Colors.red,
imagePath: "assets/image_1.png",
placeHolder: Container(
child: Icon(Icons.person, size: 50,),
),
errorWidget: Container(
child: Icon(Icons.error, size: 50,),
),
),
Output:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With