I am trying to retrieve bunch of images from an api. I want the images to be displayed in Circular form so I am using CircleAvatar
Widget, but I keep getting im
You will need to use NetworkImage
, AssetImage
, FileImage
, MemoryImage
or something similar. You can't directly use Image.network
, Image.asset
or similar due to how Flutter architects its image classes.
An example:
CircleAvatar(
radius: 100.0,
backgroundImage: NetworkImage(...),
)
backgroundImage
in CircleAvatar
expects to receive an ImageProvider
as a parameter. However, Image.network
and others don't directly extend the ImageProvider
class, they merely extend the StatefulWidget
class, even though they use NetworkImage
inside. That is why you see in the other answers that ClipOval
or ClipRRect
is being used. These classes accept a Widget
and so they're not as particular as CircleAvatar
and similar classes are.
Instead of these workarounds, therefore, for CircleAvatar
, you should use NetworkImage
and similar classes and for widgets that expect just a Widget
, you can use Image.network
and similar.
Another reason not to use clips is that they can be more expensive than changing the border radii directly: https://flutter.dev/docs/perf/rendering/best-practices
Came here as I also had the issue with CirclAvatar and AppBar, where the image would stretch in height. Instead of a radius, as I'm not able to provide it with the component I'm using, I just wrapped the image with a colum when used in appbar. This makes the image not stretch in height. This way I can always control size from the outside too
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(height: 40, width: 40, child: ProfileBadge(),),
],
)
If you want to show your image covering whole width of circle avatar then you can use it like this way. And if your image didnot load then it will show default person icon.
CircleAvatar(
child: ClipOval(
child: Center(
child: _image == null
? Icon(
Icons.person,
color: Colors.grey.shade700,
size: 100,
)
: Image.file(
_image,
fit: BoxFit.cover,
width: MediaQuery.of(context).size.width,
),
),
),
radius: 50,
backgroundColor: Colors.grey.shade300,
),
simply use backgroundImage
inside CircleAvatar
CircleAvatar(
backgroundImage: AssetImage("assets/images/dia.jpg",),
),
this work for me
CircleAvatar(
child: ClipRRect(
borderRadius: new BorderRadius.circular(100.0),
child:Image.network("https://www.rd.com/wp-content/uploads/2017/09/01-shutterstock_476340928-Irina-Bg-1024x683.jpg"),
),),
this worked for me
Transform.scale(
scale: 0.6,
child: ClipOval(
child: Image.network(
<IMAGE URL HERE>,
),
),
)