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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

The 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