Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically size Icons in Flutter to be as big as possible

Tags:

flutter

At the moment, I am using the following bit of code :

   body: new Container(             child: new Column(                 crossAxisAlignment: CrossAxisAlignment.stretch,                 mainAxisAlignment: MainAxisAlignment.spaceEvenly,                 children: <Widget>[               new MaterialButton(                 height: 120.0,                 child: new Column(                   children: <Widget>[                     new Icon(                       Icons.av_timer,                       size: 100.0,                     ),                     new Text('TIMER'),                   ],                 ),                 onPressed: null,               ),               new MaterialButton(                 height: 120.0,                 child: new Column(                   children: <Widget>[                     new Icon(Icons.alarm_add, size: 100.0),                     new Text('ALARM'),                   ],                 ),                 onPressed: null,               )             ]))); 

However, I have had to "hardcode" the Icon size to 100.0 and the height of the MaterialButton to 120.0.

I would like the MaterialButton to take as much space as possible (50% of the screen for each), and I would like the Icons to "fit nicely" in that space, ideally along with the Text scaling as well.

I haven't found the way to do that in Flutter yet. If it's not a good idea to try to do this (I would like it to use the entire space of the screen on any device) please let me know why ?

like image 279
Jackpap Avatar asked Aug 22 '17 11:08

Jackpap


People also ask

How do I make my icons bigger on Flutter?

You can increase the size of Icon to a required value by assigning the size property with specific double value.

What is the default size for icons for Flutter?

Icons occupy a square with width and height equal to size. Defaults to the current IconTheme size, if any. If there is no IconTheme, or it does not specify an explicit size, then it defaults to 24.0.

How do I change the icon on pressed on Flutter?

To change the icon button color on press, add a highlightColor property to the IconButton widget. Inside the highlightColor assign the color of your choice.


2 Answers

You can use LayoutBuilder to dynamically get the parent size during build.

A working example :

fit icon

void main() {   runApp(new MaterialApp(     home: new TestIcon(),   )); }  class TestIcon extends StatelessWidget {   @override   Widget build(BuildContext context) {     return new Container(       color: Colors.white,       child: new LayoutBuilder(builder: (context, constraint) {         return new Icon(Icons.access_alarms, size: constraint.biggest.height);       }),     );   } } 
like image 129
Rémi Rousselet Avatar answered Sep 22 '22 04:09

Rémi Rousselet


Screenshot:

enter image description here


Use FittedBox:

SizedBox.fromSize(   size: Size.fromRadius(200),   child: FittedBox(     child: Icon(Icons.add),   ), ) 
like image 26
CopsOnRoad Avatar answered Sep 22 '22 04:09

CopsOnRoad