Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give a CircleAvatar an image from assets

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 type ImageProvider.

How do I give an assets image to ImageProvider?

like image 234
Suragch Avatar asked Jan 21 '19 22:01

Suragch


People also ask

How do I change an image in an asset?

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.

How do you add an image to a circle avatar in flutter?

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 .

How do you use CircleAvatar in flutter?

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.


2 Answers

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'),
);
like image 102
diegoveloper Avatar answered Oct 23 '22 10:10

diegoveloper


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:

enter image description here

like image 2
Jitesh Mohite Avatar answered Oct 23 '22 11:10

Jitesh Mohite