Flutter icons not centred in Iconbutton and alignment does nothing - flutter

The icons need to be centred within the Icon buttons otherwise when the two are centred together in the middle of the row, they appear slightly off centre to the right.
Row(
children: <Widget>[
IconButton(
alignment: Alignment.center,
icon: Icon(Icons.arrow_left,
color: Color(0xFFF89CC0), size: 42),
onPressed: () {},
),
IconButton(
alignment: Alignment.topLeft,
icon: Icon(Icons.arrow_right,
color: Color(0xFFF89CC0), size: 42),
onPressed: () {},
},
),
],
),
I have alignment parameters set which as you can see in the screenshot are completely ignored:
How can I get them to be in the centre of their button?

The problem is that IconButtons have a default padding, so do it like this:
return Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
IconButton(
padding: EdgeInsets.all(0),
icon: Icon(Icons.arrow_left, color: Color(0xFFF89CC0), size: 42),
onPressed: () => {},
),
IconButton(
padding: EdgeInsets.all(0),
icon: Icon(Icons.arrow_right, color: Color(0xFFF89CC0), size: 42),
onPressed: () {},
),
],
);

Related

Can't move icons to end of Card Flutter

We need to move the icons to the end of the Card and with a distance of 17px between them. I tried in different ways and align, but for some reason I can not move the icons to the end. I would be grateful if you tell me how to do it right.
Scrollbar(
isAlwaysShown: true,
thickness: 2,
scrollbarOrientation: ScrollbarOrientation.left,
child: ListView.builder(
itemCount: close.length,
itemBuilder: (context, index) => Padding(
padding: const EdgeInsets.only(left: 9),
child: SizedBox(
height: 47,
child: Card(
color: constants.Colors.greyMiddle,
margin: const EdgeInsets.only(
bottom: 4,
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8)),
child: Padding(
padding:
const EdgeInsets.symmetric(horizontal: 9, vertical: 5),
child: Row(
// mainAxisAlignment: MainAxisAlignment.start,
children: [
const CircleAvatar(
backgroundColor: constants.Colors.white,
),
const SizedBox(
width: 11,
),
Text(
close[index],
style: constants.Styles.smallerLtStdTextStyleWhite,
),
IconButton(
onPressed: () {},
icon: SvgPicture.asset(constants.Assets.barPoynts)),
IconButton(
onPressed: () {},
icon: SvgPicture.asset(constants.Assets.crypto)),
IconButton(
onPressed: () {},
icon: SvgPicture.asset(
constants.Assets.barFavourites)),
],
),
),
),
),
),
),
),
You should wrap your Text widget with the Expanded widget, Which allocates the max-width to your Text so if your text gets bigger in length it will wrap it in a new line.
Expanded(
child: Text(
close[index],
style: constants.Styles.smallerLtStdTextStyleWhite,
),
),
Output:
If you use only the Spacer widget as per the other answer (which will do the work) but if your text length got bigger it will result in an overflow error.
Like below:
Also Do not use Spacer and Expanded together, as Expanded's default flex and Spacer's default flex are set to 1, flutter will allocate 'em equal amount of space and which will again cause an issue for you.
for example:
You can try putting a Spacer() before the IconButtons. So like
children: [
const CircleAvatar(
backgroundColor: constants.Colors.white,
),
const SizedBox(
width: 11,
),
Text(
close[index],
style: constants.Styles.smallerLtStdTextStyleWhite,
),
const Spacer(),
IconButton(
onPressed: () {},
icon: SvgPicture.asset(constants.Assets.barPoynts)),
IconButton(
onPressed: () {},
icon: SvgPicture.asset(constants.Assets.crypto)),
IconButton(
onPressed: () {},
icon: SvgPicture.asset(
constants.Assets.barFavourites)),
],
Spacers always take as much space as they can
{Text(close[index],
style: constants.Styles.smallerLtStdTextStyleWhite,
),}
After this, Write
Expanded(child:Container()),
And then icons. It will make space of container with empty area in Row.

Flutter- How to align buttons at particular positions in a row?

I have two buttons in the bottom of the screen in a single row. i want to keep first button in center and second button in end. how can i achieve that?
Here is my Code
new Container
(
child:Row(
children:<Widget>[
new RaisedButton(
color: Colors.red,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
onPressed: takescrshot,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Next',
style:
TextStyle(fontSize: 23, color: Colors.white),
),
),
),
new FloatingActionButton.extended(
onPressed: () {
// Add your onPressed code here!
},
label: Text('options'),
icon: Icon(Icons.menu),
backgroundColor: Colors.pink,
),
])
)
Can anyone guide me?
You need to understand how Column and Row work to position its children and how to properly use the Flexible widgets to make every child position as you want.
You have a Row there with two buttons as a children (one RaisedButton and a FloatingActionButton) but they aren't being restricted to any position, so they're both lay down on the start of the Row aligned at the left.
First, let's make it so both fit all the space available horizontally. For that, you'll need to wrap each button with an Expanded.
Right now, you'll probably have two stretched buttons using 50/50 of the space but looking ugly. If so, you'll probably want to wrap each button with a Center so they are centered within the space they have from each Expanded. If not, skip this step. Looking better now, right?
Now, we have 2 buttons evenly centered on the screen with all the space available, but we want to make sure that one is centered and the other on the right, regardless of the screen size. For that, we add a const Spacer() as the first child of the Row so it actually share "invisible" space with all the other 2 siblings;
Right now, you should have what you are looking for, and working flawlessly for all screen sizes.
TL;DR:
Container(
child: Row(
children: <Widget>[
const Spacer(),
Expanded(
child: Center(
child: RaisedButton(
color: Colors.red,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
onPressed: takescrshot,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Next',
style: TextStyle(fontSize: 23, color: Colors.white),
),
),
),
),
),
Expanded(
child: Center(
child: FloatingActionButton.extended(
onPressed: () {
// Add your onPressed code here!
},
label: Text('options'),
icon: Icon(Icons.menu),
backgroundColor: Colors.pink,
),
),
),
],
));
use Spacer widget like this:
new Container
(
child:Row(
children:<Widget>[
Spacer(),
new RaisedButton(
color: Colors.red,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
onPressed: takescrshot,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Next',
style:
TextStyle(fontSize: 23, color: Colors.white),
),
),
),
Spacer(),
new FloatingActionButton.extended(
onPressed: () {
// Add your onPressed code here!
},
label: Text('options'),
icon: Icon(Icons.menu),
backgroundColor: Colors.pink,
),
])
)
You can do it by faking a same child like the Options button at the start of the Row and making it invisible. Also need to set the Row's maninAxisAlignment to spaceBetween like this..
Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new Opacity(
opacity: 0,
child: FloatingActionButton.extended(
onPressed: null,
label: Text('options'),
icon: Icon(Icons.menu),
backgroundColor: Colors.pink,
),
),
new RaisedButton(
color: Colors.red,
shape:RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
onPressed: () {},
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Next',
style: TextStyle(fontSize: 23, color: Colors.white),
),
),
),
new FloatingActionButton.extended(
onPressed: () {
// Add your onPressed code here
},
label: Text('options'),
icon: Icon(Icons.menu),
backgroundColor: Colors.pink,
),
],
),
)

Icon rendered with offset

When I tried to use IconButtons, I noticed that they were rendered incorrectly - the icon itself was offset to the right bottom, while the clicking animation was perfectly fine. Now my question is: Is this issue related to my code or is it Flutter's fault?
This is the build method of the StatelessWidget the IconButtons are located in:
return Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
IconButton(
onPressed: () => {},
icon: Icon(
Icons.play_circle_outline,
size: 50,
),
color: Color(0xff3C6E71),
),
],
),
);
This is how it looks when the button is clicked, the Icon itself is offset to the bottom right but the clicking animation is perfectly centered how it should be.
Probably some issue with the widget padding, you can try adding a padding with the value zero:
return Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
IconButton(
onPressed: () => {},
padding: EdgeInsets.zero, // set padding to zero
icon: Icon(
Icons.play_circle_outline,
size: 50,
),
color: Color(0xff3C6E71),
),
],
),
);

how to add icon at right side of RaisedButton?

How to add icon to RaisedButton at the right side
Padding(
padding: const EdgeInsets.only(top: 15.0),
child: new SizedBox(
width: double.infinity,
child: new RaisedButton(
textColor: Colors.white,
color: coloraccent,
onPressed: () {},
child: const Text('UPADATE'),
)),
),
You can just wrap your RaisedButton.icon to the Directionality widget:
Directionality(
textDirection: TextDirection.rtl,
child: RaisedButton.icon(
onPressed: () {},
color: Colors.deepPurple,
icon: Icon(
Icons.arrow_drop_down,
color: Colors.white,
),
label: Text(
"Category",
style: TextStyle(color: Colors.white),
),
),
)
You can try this out, it works fine for me.
child: RaisedButton(
onPressed: () => navigateToLogin(context),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Continue",style: TextStyle(fontSize: 20)),
Icon(Icons.navigate_next)
],
),
use RaisedButton.icon constructor
Padding(
padding: const EdgeInsets.only(top: 15.0),
child: SizedBox(
width: double.infinity,
child: RaisedButton.icon(
icon: Icon(Icons.translate),
textColor: Colors.white,
color: Colors.lightBlue,
label: const Text('UPADATE'),
onPressed: () {},
)),
),
give a child to raised button like that
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween
children: <Widget>[
Text('some text'),
Icon(Icons.home),
],
),
I tried to use RaisedButton.Icon but it was not enough for me, because I usually customize the buttons with many design styles, so I used some techniques for placing text and Icon with RaisedButton.
You can try this, It worked for me. Let me explain in the code.
// Create a Raised Button.
RaisedButton(
color: Colors.pinkAccent, // Color pinkAccent
child: Row( // Add a Row Widget for placing objects.
mainAxisAlignment: MainAxisAlignment.center, // Center the Widgets.
mainAxisSize: MainAxisSize.max, // Use all of width in RaisedButton.
children: <Widget>[
Padding(
padding: EdgeInsets.all(5.0), // Give to the text some space.
child: Text(
"Enviar",
style: TextStyle(
fontSize: 18, // 18pt in font.
color: Colors.white, // You can ommit it if you use textColor in RaisedButton.
),
),
),
Icon(
Icons.send, // Send Icon. (Papper Plane Icon)
color: Colors.white, // White Color. You can ommit it too if you use textColor property on RaisedButton.
size: 18, // 18 pt, same as text.
),
],
),
onPressed: () {}, /// For enabling the button
),
Use RaisedButton.icon:
Doc says: Create a filled button from a pair of widgets that serve as
the button's icon and label.
The icon and label are arranged in a row and padded by 12 logical
pixels at the start, and 16 at the end, with an 8 pixel gap in
between.
RaisedButton.icon(
icon: Icon(Icons. ...), // <-- Icon you want.
textColor: Colors.white,
color: Colors.lightBlue,
label: const Text('UPADATE'), // <-- Your text.
onPressed: () {},
)),
use this code. you can add your required width as per text length
Container(
height: 35.0,
width: 95,
child: RaisedButton(
elevation: 10,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(15.0),
),
),
color: Colors.white,
child: Row(
children: <Widget>[
Text('Call', style: TextStyle(color: Colors.black)),
SizedBox(width: 10),
Icon(Icons.call),
],
),
onPressed: () {},
),
);
If you still resist using RaisedButton.Icon instead of using the normal RaisedButton with Row widget as a child
Then you can swap between label and icon attributes because they both take a widget.
Like this:
RaisedButton.icon(
icon: const Text('UPADATE'),
label: Icon(Icons.translate),
textColor: Colors.white,
color: Colors.lightBlue,
onPressed: () {},
)
Here is my two buttons, Back and Next
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
RaisedButton(
color: const Color(0xFFFFB822), // Color pinkAccent
child: Row(
// Add a Row Widget for placing objects.
mainAxisAlignment: MainAxisAlignment.center, // Center the Widgets.
mainAxisSize: MainAxisSize.max, // Use all of width in RaisedButton.
children: <Widget>[
SizedBox(height: 10, width: 10),
Icon(
Icons.arrow_back_ios, // Send Icon. (Papper Plane Icon)
color: Colors.black, // White Color. You can ommit it too if you use textColor property on RaisedButton.
size: 18, // 18 pt, same as text.
),
Padding(
padding: EdgeInsets.all(5.0), // Give to the text some space.
child: Text(
"Back",
style: TextStyle(
fontWeight: FontWeight.w900,
color: Colors.black, // You can ommit it if you use textColor in RaisedButton.
),
),
),
],
),
onPressed: () {},
/// For enabling the button
),
RaisedButton(
color: const Color(0xFFFFB822), // Color pinkAccent
child: Row(
// Add a Row Widget for placing objects.
mainAxisAlignment: MainAxisAlignment.center, // Center the Widgets.
mainAxisSize: MainAxisSize.max, // Use all of width in RaisedButton.
children: <Widget>[
Padding(
padding: EdgeInsets.all(5.0), // Give to the text some space.
child: Text(
"Next",
style: TextStyle(
fontWeight: FontWeight.w900,
color: Colors.black, // You can ommit it if you use textColor in RaisedButton.
),
),
),
Icon(
Icons.arrow_forward_ios, // Send Icon. (Papper Plane Icon)
color: Colors.black, // White Color. You can ommit it too if you use textColor property on RaisedButton.
size: 18, // 18 pt, same as text.
),
],
),
onPressed: () {},
/// For enabling the button
),
],
),

Flutter: persistentFooterButtons with equally dynamic size widgets

I want to create a persistentFooterButtons with 3 FlatButton.icon
to equally take the whole width of the Screen, and customize itself with different screen sizes, i tried many approaches like Wrap,Expanded, i couldn't make it work, i even tried
final screenSize = MediaQuery.of(context).size;
Container(
width: screenSize.width /3 ,
child: FlatButton.icon(
onPressed: null,
icon: Icon(Icons.search),
label: Text("search")),
),
but the text always overflows on the right side of the screen.
So, Any ideas how this might go?
**Edit**
i found this approach which is very helpful
persistentFooterButtons: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
FlatButton(
onPressed: () {},
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new Icon(Icons.lightbulb_outline),
new Text('Idea'),
],
),
),
FlatButton(
onPressed: () {},
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new Icon(Icons.lightbulb_outline),
new Text('Idea'),
],
),
),
FlatButton(
onPressed: () {},
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new Icon(Icons.lightbulb_outline),
new Text('Idea'),
],
),
),
],
),
],
The only problem is that mainAxisAlignment property doesn't make any changes, so the three buttons are sided together
See here
Any help?
For create a persistentFooterButtons you need to use bottomNavigationBar.
And For create 3 flatButton with equal size, you need to use flex attribute inside Expanded Widget.
Scaffold(
body: Container(
color: Colors.white,
child: Center(child: Text("Flutter"),),
),
bottomNavigationBar: new Container(
padding: EdgeInsets.all(0.0),
child: Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Expanded(
flex: 1,
child: FlatButton.icon(
onPressed: () {
},
icon: Icon(Icons.search),
label: Text("Search"),
),
),
Expanded(
flex: 1,
child: FlatButton.icon(
onPressed: () {
},
icon: Icon(Icons.search),
label: Text("Search"),
),
),
Expanded(
flex: 1,
child: FlatButton.icon(
onPressed: () {
},
icon: Icon(Icons.search),
label: Text("Search"),
),
),
],
),
),
);