How to size an icon according to parent in flutter

Issue

I want to size an icon inside a container to be the size of that container so that it would not be small in larger devices due to hard coding the size value. I was trying something like this

Container(
  child: Icon(
    Icons.beach_access,
    size: double.infinity,
  )
)

Solution

If you want the size of the icon to meet the ends of its Container parent, you can place it in a FittedBox

Container(
  child: FittedBox(
     child: Icon(
        Icons.beach_access,
          ),
        ),
      ),

You can change the fit property of the FittedBox to adjust some sizes and change alignment.

https://api.flutter.dev/flutter/widgets/FittedBox-class.html

Answered By – Texv

Answer Checked By – Mary Flores (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.