Flutter - How to create responsive CircleAvatar widget? - flutter

I want to create a responsibility for the CircleAvatar widget, but I am not sure how.
Here is my code:
body: Container(
padding: const EdgeInsets.all(10),
width: MediaQuery.of(context).size.width,
child: Row(children: [
Expanded(
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(...),
if (_isAvatar)
Container(
padding: const EdgeInsets.all(15),
child: CircleAvatar(
maxRadius: MediaQuery.of(context).size.width -
MediaQuery.of(context).size.width +
78, // Here is a problem, how to calc width to fit on any device?
backgroundImage: NetworkImage(
_pageContent['acf']['doctor']['avatar']),
),
),
Container(...),
],
)),
])));
I want to make the widget responsive and fittable on any device or resolutio.
I am Flutter's newbie, please help!
Thanks!

You should use radius, not maxRadius. Here's the code:
CircleAvatar(
radius: MediaQuery.of(context).size.width * 0.3, //change 0.3 to any value you want
backgroundImage: NetworkImage(
_pageContent['acf']['doctor']['avatar']),
),
),

Related

Remove (what seems like) margin around widget in Container

I'm having trouble removing the vertical space around the 'ClipOval' widgets within this Column. My goal (at this moment) is to have no space between them but I can't seem to figure out how to accomplish that.
Column(
mainAxisSize: MainAxisSize.max,
children: [
ClipOval(
child: SizedBox.fromSize(
size: const Size.fromRadius(100), // Image radius
child: Image.asset('assets/images/home_logo.png', fit: BoxFit.cover),
),
),
ClipOval(
child: SizedBox.fromSize(
size: const Size.fromRadius(100), // Image radius
child: Image.asset('assets/images/home_logo.png', fit: BoxFit.cover),
),
)
],
)
I tried creating a custom clipper but it would have the image removed entirely. Here's the code I tried for the clipper. My approach here was having the top margin set to 0 in hopes of having the top margin removed.
class MyClip extends CustomClipper<Rect> {
Rect getClip(Size size) {
return Rect.fromLTWH(100, 0, 100, 100);
}
bool shouldReclip(oldClipper) {
return false;
}
}
Try using CircleAvatar with ClipRRect instead.
Sample Code
Column(
mainAxisSize: MainAxisSize.max,
children: [
CircleAvatar(
radius: 100,
child: ClipRRect(
child: Image.asset('assets/images/home_logo.png'),
),
),
CircleAvatar(
radius: 100,
child: ClipRRect(
child: Image.asset('assets/images/home_logo.png'),
),
),
],
),
Hope this help.
Explanation
On your current approach you have a Sizebox with
size: const Size.fromRadius(100) while you have a small image less than the Sizebox size
You could also try to replace BoxFit.contain to BoxFit.fill but this will stretch your image
Column(
mainAxisSize: MainAxisSize.max,
children: [
ClipOval(
child: SizedBox.fromSize(
size: const Size.fromRadius(100), // Image radius
child: Image.asset('assets/images/home_logo.png',
fit: BoxFit.fill),
),
),
ClipOval(
child: SizedBox.fromSize(
size: const Size.fromRadius(100), // Image radius
child: Image.asset('assets/images/home_logo.png',
fit: BoxFit.fill),
),
),
],
),
Have you tried changing the value of mainAxisAlignment: to mainAxisAlignment: MainAxisAlignment.start and crossAxisAlignment: CrossAxisAlignment.start

MainAxisSize.SpaceEvenly not giving equal space in the widget

This is the Code
body: Center(
child: Column(
children: [
Row(
children: [
CircleAvatar(
backgroundImage: NetworkImage(
'https://raw.githubusercontent.com/flutter/website/master/examples/layout/sizing/images/pic1.jpg'),
radius: 50,
),
SizedBox(
width: 30,
),
Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text('Post'),
Text('Followers'),
Text('Following')
],
),
)
],
),
],
),
),
Try below code hope its helpful to you,used ListTile widget also refer ListTile here, refer my answer here for same design
ListTile(
leading: CircleAvatar(
backgroundImage: NetworkImage(
'https://raw.githubusercontent.com/flutter/website/master/examples/layout/sizing/images/pic1.jpg',
),
radius: 50,
),
title: Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text('Post'),
Text('Followers'),
Text('Following'),
],
),
),
),
Your result screen like->
Container doesn't take all reaming space (give a background color to the container and see). that's the reason you don't see MainAxisAlignment.spaceEvenly,. Use a Expanded widget which take all remaining space. then you will see the effect.
Containers with no children try to be as big as possible unless the incoming constraints are unbounded, in which case they try to be as small as possible. Containers with children size themselves to their children. The width, height, and constraints arguments to the constructor override this.
Column(
children: [
Row(
children: [
CircleAvatar(
backgroundImage: NetworkImage(
'https://raw.githubusercontent.com/flutter/website/master/examples/layout/sizing/images/pic1.jpg'),
radius: 50,
),
SizedBox(
width: 30,
),
Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text('Post'),
Text('Followers'),
Text('Following')
],
),
)
],
),
],
),

Image is not fitted when used in leading of ListTile

The asset image is 200x200px, but when it used in leading of ListTile it's not fitted as axpected.
The rendered image looks this way. How to display it proportioanlly 90x90 as it's defined in Image height and width properties?
Card(
key: ValueKey(favorites[index]),
margin: EdgeInsets.all(20),
child: ListTile(
leading: kNoImage,
title: Text(favorites[index]),
subtitle: Text(favorites[index]),
trailing: IconButton(
icon: Icon(Icons.close),
onPressed: () {
debugPrint('entry deleted');
},
),
),
);
const kNoImage = Image(
image: AssetImage('assets/no-user-image.png'),
height: 90,
width: 90,
fit: BoxFit.cover,
);
Change the value of fit to something like BoxFit.scaleDown or BoxFit.contain to get the desired result. See the BoxFit docs to see what these values mean.
The max height for leading widget for your case is ≈56px.
So, if you want to explicitly want to keep it 90, I'd suggest you to make your own ListTile with Row() and Column() which is pretty easy.
This is what I found in ListTile(),
final Offset baseDensity = visualDensity.baseSizeAdjustment;
if (isOneLine)
return (isDense ? 48.0 : 56.0) + baseDensity.dy;
if (isTwoLine)
return (isDense ? 64.0 : 72.0) + baseDensity.dy;
return (isDense ? 76.0 : 88.0) + baseDensity.dy;
I prefer not to use ListTile because of it default behavior. Therefor, I prefer
on dartPad or on Gist
Custom ListTile
Card(
elevation: 8,
child: Container(
width: double.infinity,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(defaultBorderRadiusCircular),
),
padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 6),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
SizedBox(
width: 90.0,
height: 90.0,
child: ClipRRect(
borderRadius:
BorderRadius.circular(defaultBorderRadiusCircular),
child: Image.asset(
'imagePath',
width: 90,
height: 90,
fit: BoxFit.cover,
),
),
),
const SizedBox(
width: 24,
),
Column(
children: [
const Text(
'Danau Beretan',
),
const Text(
'Indonesia',
),
],
),
],
),
Row(
children: [
Icon(Icons.star_half_outlined),
Text("4.5"),
],
)
],
),
),
),

Google Icon(Logo) for Sign_in flutter

I'm trying to add google logo for sign in with google by downloading from https://developers.google.com/identity/branding-guidelines. But when I use it in flutter app, some lines and dots are appearing around logo.
GestureDetector(
child: Container(
padding: EdgeInsets.only(right: _size.height * 0.016),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(15.0),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
// decoration: BoxDecoration(color: Colors.blue),
child: Image.asset(
'assets/images/google_icon/btn_google_light_normal.9.png',
fit: BoxFit.fitWidth,
),
),
SizedBox(
width: 5.0,
),
Text('Sign-in with Google')
],
),
),
),
which results in
enter image description here
or
enter image description here
How to remove glitches around the G logo?
i think there may be problem in the format of image you are using try out this code
Container(
width:300,
height:80,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
// decoration: BoxDecoration(color: Colors.blue),
child:
Image.network(
'http://pngimg.com/uploads/google/google_PNG19635.png',
fit:BoxFit.cover
)
),
SizedBox(
width: 5.0,
),
Text('Sign-in with Google')
],
),
)
You can use an open-source background remover tool such as remove.bg/upload to remove everything around the logo. Then, you can just do the same thing: Image.network('<your path>', height: 40, width: 40) or any size.
Perhaps this is not the most elegant solution, but I just cropped the original image to get rid of the lines.
Before:
After:

Image is overflowing when I rotated it

I'm new to Flutter and ran into this slight problem. So I've created a Card widget and placed an image in it. Originally the image was horizontal, so I rotated it to be vertical. However after doing so, the image overflowed out of my card. Any help would be much appreciated. Thanks :)
See image here
Card(
color: Colors.green,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text("My Boat"),
Transform.rotate(
angle: -.9,
child: Image(
image: AssetImage('images/boat.png',),
height: 140,
),
)
],
)),
you have two choices
first, remove its height
Card(
color: Colors.green,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text("My Boat"),
Transform.rotate(
angle: -.9,
child: Image(
image: AssetImage('images/boat.png',),
),
)
],
)),
second, you can wrap it in Expanded like
Card(
color: Colors.green,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text("My Boat"),
Expanded(
child: Transform.rotate(
angle: -.9,
child: Image(
image: AssetImage('images/boat.png',),
),
)
)
],
)),