admin管理员组文章数量:1291026
I am trying to add a couple of logos to a row in a flutter widget; relevant code is below:
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
decoration: BoxDecoration(
border: Border.all(color: FbColors.grey),
borderRadius: BorderRadius.circular(300),
),
child: Image(
width: FbSizes.iconMd,
height: FbSizes.iconMd,
image: AssetImage(dark ? FbImages.googleDark : FbImages.googleLight),
),
),
const SizedBox(width: FbSizes.spaceBtwnItems),
Container(
decoration: BoxDecoration(
border: Border.all(color: FbColors.grey),
borderRadius: BorderRadius.circular(100),
),
child: Image(
width: FbSizes.iconMd,
height: FbSizes.iconMd,
image: AssetImage(dark ? FbImages.appleDark : FbImages.appleLight),
),
),
],
)
Note that the first Container
has a BorderRadius.circular(300)
call, and the second one has a BorderRadius.circular(100)
call - but they're both the same size on-screen, and neither of them is the size specified (they will be the same size in the final view - I just changed the sizes in the code to show they're not responding).
I cannot for the life of me figure out why I can't resize them. Any ideas what I'm doing wrong?
I am trying to add a couple of logos to a row in a flutter widget; relevant code is below:
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
decoration: BoxDecoration(
border: Border.all(color: FbColors.grey),
borderRadius: BorderRadius.circular(300),
),
child: Image(
width: FbSizes.iconMd,
height: FbSizes.iconMd,
image: AssetImage(dark ? FbImages.googleDark : FbImages.googleLight),
),
),
const SizedBox(width: FbSizes.spaceBtwnItems),
Container(
decoration: BoxDecoration(
border: Border.all(color: FbColors.grey),
borderRadius: BorderRadius.circular(100),
),
child: Image(
width: FbSizes.iconMd,
height: FbSizes.iconMd,
image: AssetImage(dark ? FbImages.appleDark : FbImages.appleLight),
),
),
],
)
Note that the first Container
has a BorderRadius.circular(300)
call, and the second one has a BorderRadius.circular(100)
call - but they're both the same size on-screen, and neither of them is the size specified (they will be the same size in the final view - I just changed the sizes in the code to show they're not responding).
I cannot for the life of me figure out why I can't resize them. Any ideas what I'm doing wrong?
Share Improve this question edited Feb 14 at 6:19 Mahmoud Al-shehyby 4175 bronze badges asked Feb 13 at 16:16 AndyAndy 6622 gold badges9 silver badges23 bronze badges1 Answer
Reset to default 1The Image
widget's width
and height
properties should control the size of the image, but if the parent Container
or other constraints are interfering, the image may not appear as expected.
So instead of giving the size to the image , give it to the parent Container
and ensure the Container
has a square size.
Instead of giving the Container
a fixed BorderRadius
like BorderRadius.circular(300)
to make it circular, pass shape: BoxShape.circle,
to the decoration
of the Container
, you can use fit
property in the AssetImage
fit: BoxFit.contain
to control the fit of the image in the Container
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
width: FbSizes.iconMd,
height: FbSizes.iconMd,
decoration: const BoxDecoration(
shape: BoxShape.circle,
color: Colors.grey,
),
child: Image(
image: AssetImage(dark ? FbImages.googleDark : FbImages.googleLight),
fit: BoxFit.contain,
),
),
const SizedBox(width: FbSizes.spaceBtwnItems),
Container(
width: FbSizes.iconMd,
height: FbSizes.iconMd,
decoration: const BoxDecoration(
shape: BoxShape.circle,
color: Colors.grey,
),
child: Image.asset(
image: AssetImage(dark ? FbImages.appleDark : FbImages.appleLight),
fit: BoxFit.contain,
),
),
],
)
Better Solution:
You can use the CircleAvatar
Widget and control the size of the image by change the value of the radius
property
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CircleAvatar(
radius: 40,
backgroundColor: Colors.grey,
child: Image(
image: AssetImage(dark ? FbImages.googleDark : FbImages.googleLight),
fit: BoxFit.contain,
),
),
const SizedBox(width: FbSizes.spaceBtwnItems),
CircleAvatar(
radius: 40,
backgroundColor: Colors.grey,
child: Image(
image: AssetImage(dark ? FbImages.appleDark : FbImages.appleLight),
fit: BoxFit.contain,
),
),
],
)
本文标签: flutterWhy aren39t these containers sized correctlyStack Overflow
版权声明:本文标题:flutter - Why aren't these containers sized correctly? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741517985a2383009.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论