Ensuring consistent spacing with custom Icon font in Flutter - flutter

I am trying to use this custom weather font in my Flutter app, but I am having some trouble with the spacing.
It seems as if the icons are rendering outside of their containers, which is causing some layout problems.
This code creates the following
Container(
color: Colors.amber,
child: Icon(
IconData(0xf05c),
size: 100,
),
);
As you can see the icon is not centered in its bounding box.
Here is an example of another icon:
What is the best approach for making it so all the icons take up the same space, and are centered in their boxes?

I ran the above code using a regular built in Icons instead and the container automatically centered the icon
Container(
color: Colors.amber,
child: Icon(
Icons.access_alarm,
size: 100,
),
),
Where did you get those 2 images? I am wondering if those 2 custom images may be what is messing up the alignment on that Container parent. Could those 2 images have any extra padding built into the image itself that may be throwing it off?

The alignment of Container's parent container sometimes affects the internal alignment of Container, which can be ensured by adding alignment attributes to Container.
Container(
alignment:Alignment.center
color: Colors.amber,
child: Icon(
IconData(0xf05c),
size: 100,
),
);

Related

Flutter: IconButton() Being Partially Rendered Outside It's Container()

I'm trying to up my game when it comes to my Flutter layout / sizing knowledge and this post is something that I recently observed that confuses me.
I have a Container() that has an IconButton() having an icon from the stock Flutter icon set with a size that is passed as a parameter to the function.
Here is the code:
Widget buttonGallery(double size) {
return Container(
color: Colors.yellow,
child: IconButton(
icon: Icon(
Icons.photo,
size: size,
color: Colors.blue,
),
onPressed: (){},
},
),
);
}
With the size set to 75, I am noticing that the IconButton() is being partially rendered outside of its parent Container(). (This behavior is visible as I explicitly set the Container() color to yellow to be able to see the parent client area). See pic below:
Here are my questions:
Isn't the parent widget, which in this is a Container(), supposed to envelop it's child widget meaning that the child shouldn't overflow outside of it's parent? (I know that there's the OverflowBox() widget that supposed to allow this behavior, but I don't think that's the case here.)
In the case where the child is too big to fit its parent's client area, isn't Flutter supposed to clip the child and show those yellow hazzard lines indicating this scenario?
Having dealt a lot with Container(), I was under the impression that it was supposed to fit itself to the dimensions of its child. I'm confused why it's not doing this now.
All help/suggestions greatly appreciated.
/Joselito
The IconButton widget sizes itself according to the property iconSize. It defaults to 24, and that's the size being passed to your container.
This should do it for you:
Widget buttonGallery(double size) {
return Container(
color: Colors.yellow,
child: IconButton(
iconSize: size,
icon: Icon(
Icons.photo,
size: size,
color: Colors.blue,
),
onPressed: (){},
},
),
);
}

How to create Flutter Custom App Bar Floating Style with Radius Corners?

I need to customize the App bar like below, with rounded style and it floats on top of all other widgets:
Its similar to Google Map app. What would be the best way to achieve this customization?
Thanks for any advice or guides to approach this...
You can use Stack widget to display a widget on top of another widget (page content in your example). So use Stack to place search / app bar on top of main app content and customize as your needs.
You can check this youtube video to learn more about Stack widget.
Here's sample code, you can place inside Scaffold.
Stack(
children: <Widget>[
// this will be filled entire screen
Positioned.fill(child: Image.network('https://i.imgur.com/SJGDZUp.png')),
// this will be placed top of the screen with 15 points distance
Positioned(
top: 15,
left: 15,
right: 15,
// using SafeArea to avoid device frames (for example notch in iPhones)
child: SafeArea(
child: Container(
height: 50,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(4),
color: Colors.black,
),
child: Text('Search...', style: TextStyle(color: Colors.white)),
),
),
),
],
),
DISCLAIMER: I haven't tested the code, so, it may contain syntax issues or typos

Connect a row of circles to a chain in Flutter

I have a rather easy problem, but which I cannot seem to solve efficiently.
So I have a row of a changing number of Containers with a centered Circle, for which I used a circle icon from the Font Awesome package. And what I want to do is to connect the sides of these circles so that they form a chain.
I though of creating a custom icon, a circle with a line to the side long enough to reach the next circle.
Another option would be to use Stack and manually place a line between the cirlces, but because the number of cicles is changing, I fear the that the line will overflow the circle boundries.
Any of ou have an idea how to solve this efficiently?
Edit:
So here is a picture of what I want it to be like:
Picture of the Chain
My Code right now is just
Row(
children: <Widget>[
Container(
child: Center( child: CircleIcon ),
),
Container(
child: Center( child: CircleIcon ),
), ...
You can just use Container for this. Really simple with Container.
Container{
height: 50, // give any height you need
width: 50, // give the same as height,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: Colors.black
),
),
}

How to fill color in the icon in flutter

I am new in flutter. I am using multiple theme(i.e. dark mode) in the app. So, When we use icon in different theme, automatically take background color according to the theme. I want background color of theme but not inside the icon.
Example:
I am using youtube's icon in dark theme so look like below,
But i want to like below,
I am using
Icon(
FontAwesomeIcons.youtube,
color: Colors.red
)
So how to fill the color white in this icon ? (Or also you can suggest me to do that in proper as well as better way to implement)
(so, i can use white filled icon in every theme)
You can use a Stack to place a filled Container under your icon like so:
Stack(children: <Widget>[
Positioned.fill(
child: Container(
margin: EdgeInsets.all(5), // Modify this till it fills the color properly
color: Colors.white, // Color
),
),
Icon(
FontAwesomeIcons.youtube, // Icon
color: Colors.red,
),
),
])
Since its a container, you can also modify its shape in case there are random icon shapes that a normal square does not help in :P
I tried filling the icon play_circle_filled with green using this on DartPad and it gave me this:
I also faced the same issue. I think there could be a modification in Sidak's solution. Instead of using a container in the background, we could use the same Icon of the desired color in the background using the stack widget.
Stack(children: <Widget>[
Positioned.fill(
child: Container(
margin: EdgeInsets.all(5), // Modify this till it fills the color properly
color: Colors.white, // Color
),
),
Icon(
FontAwesomeIcons.youtube, // Icon
color: Colors.red,
),
),
])

How to size an icon according to parent in flutter

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,
)
)
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