Can't move icons to end of Card Flutter - 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.

Related

how to remove the space between the lines(rows) of a wrap widget (flutter)?

This is my code for wrap widget which is having multiple material button representing tags, i don't know why there is a vertical space between each new row in the wrap widget
Container(
width: double.infinity,
child: Wrap(
children: [
MaterialButton(
color: Colors.black,
minWidth: 1,
height: 25,
padding: EdgeInsets.zero,
onPressed: () {},
child: Text(
"#software",
style: TextStyle(color: defaultColor),
),
),
MaterialButton(
minWidth: 1,
height: 25,
padding: EdgeInsets.zero,
onPressed: () {},
child: Text(
"#software",
style: TextStyle(color: defaultColor),
),
),
MaterialButton(
minWidth: 1,
height: 25,
padding: EdgeInsets.zero,
onPressed: () {},
child: Text(
"#software",
style: TextStyle(color: defaultColor),
),
),
MaterialButton(
minWidth: 1,
height: 25,
padding: EdgeInsets.zero,
onPressed: () {},
child: Text(
"#software",
style: TextStyle(color: defaultColor),
),
),
MaterialButton(
minWidth: 1,
height: 25,
padding: EdgeInsets.zero,
onPressed: () {},
child: Text(
"#software",
style: TextStyle(color: defaultColor),
),
),
MaterialButton(
minWidth: 1,
height: 25,
padding: EdgeInsets.zero,
onPressed: () {},
child: Text(
"#software",
style: TextStyle(color: defaultColor),
),
),
],
),
),
this is a picture describing the vertical space between the line , i reached a solution by set the running space to negative values and that worked for me, but i feel like its a bad practiceenter image description here
Don't use MaterialButton directly. Use one of its descendants: ElevatedButton, TextButton or OutlinedButton.
In your case, TextButton is the closet to what you want to achieve, so you can use that as a starting point and then further customize its style.
Or, if you just want to allow clicking on the blue text, use GestureDetector or InkWell instead of using buttons.
you can use two property here for controlling the space
spacing
runSpacing
please test it and share with us about the result.
In your case I would use more common buttons such as
https://www.notion.so/BUTTONS-IN-FLUTTER-f596d17ba17f418381b30ed140cd9239
and I would create a generalized button sending as parameters what I need from each button, so as not to generate more unnecessary code, you could also use column or row with mainAxisAlignment: MainAxisAlignment.spaceEvenly,
to align your buttons.

How to make a following button design in the flutter

thanks for opening the question, I am learning the flutter and I am learning about the button, I want to make the following design in the flutter
but am not able to find any widget which can be used, there is something the icon in the elevated button which I have used but I am not able to achieve the design.
using the elevatedButton.icon
return SizedBox(
width: double.infinity,
height: 48,
child: ElevatedButton.icon(
icon:Icon(
prefixIcon, // passing from the main widget
),
onPressed: () => {},
label: Text(text),
style: ElevatedButton.styleFrom(
primary: backgroundColor
),
),
);
}
}
Try to below answer hope its helpful to you
Container(
width: 250,
height: 50,
margin: const EdgeInsets.all(5),
child: ElevatedButton(
onPressed: () {},
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Icon(Icons.login), //use your google image here
Text('Sign in with Google '),
Opacity(
opacity: 0,
child: Icon(Icons.login),
)
],
),
style: ElevatedButton.styleFrom(
primary: Colors.red,
),
),
),
Your Button look like this->
you can use this to achieve your desired widget:
Container(
width: 256,
height: 48,
margin: const EdgeInsets.all(8),
child: ElevatedButton(
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: const <Widget>[
Icon(Icons.login), // your google image
Expanded(child: Center(child: Text('Sign in with Google'))),
],
),
style: ElevatedButton.styleFrom(primary: Colors.red),
onPressed: () {},
),
);

Flutter FlatButton and Icon introducing unwanted padding

I'm trying to control the size of the FlatButton with an Icon in it. But it seems the Icon is breaking the button.
return Container(width: 180,height:30,
child: Row(
children: [
FlatButton(onPressed: () => _selectedDateDailyChart > 0 ? _adjustDate(-1) : null,
child: Container(
color:Colors.red,
width: 20,
child: Icon(Icons.arrow_back_ios),
),
padding: EdgeInsets.zero
),
Container(child: Text(currentDate, style: Theme.of(context).textTheme.headline3.apply(fontSizeDelta: -2)),),
FlatButton(onPressed: () => _selectedDateDailyChart > 0 ? _adjustDate(-1) : null,
child: Container(
width: 20,
child: Icon(Icons.arrow_forward_ios),
),
padding: EdgeInsets.zero
),
],),
Here is what it looks like:
You may want put an InkWell widget around you Icon insted, same result, 0 padding.
you just have to use Expanded Widget like this "to make a balance between the two button wrap the other Button with expanded too"
child: Container(
width: 180,
height: 30,
child: Row(
children: [
Expanded(
child: FlatButton(
onPressed: () => _selectedDateDailyChart > 0
? _adjustDate(-1)
: null,
child: Container(
color: Colors.red,
width: 20,
child: Icon(Icons.arrow_back_ios),
),
padding: EdgeInsets.zero),
),
Container(
child: Text(currentDate,
style: Theme.of(context)
.textTheme
.headline3
.apply(fontSizeDelta: -2)),
),
FlatButton(
onPressed: () =>
_selectedDateDailyChart > 0 ? _adjustDate(-1) : null,
child: Container(
width: 20,
child: Icon(Icons.arrow_forward_ios),
),
padding: EdgeInsets.zero),
],
),
),

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

How to make button width match parent?

I want to know that how can I set a width to match parent layout width
new Container(
width: 200.0,
padding: const EdgeInsets.only(top: 16.0),
child: new RaisedButton(
child: new Text(
"Submit",
style: new TextStyle(
color: Colors.white,
)
),
colorBrightness: Brightness.dark,
onPressed: () {
_loginAttempt(context);
},
color: Colors.blue,
),
),
I know about little bit on Expanded widget but Expanded expands view to both direction, i dont know how to do it.
Update:
With Flutter 2.0 RaisedButton is deprecated and replaced by ElevatedButton. you can use minimumSize like this:
ElevatedButton(
style: ElevatedButton.styleFrom(
minimumSize: Size.fromHeight(40), // fromHeight use double.infinity as width and 40 is the height
),
onPressed: () {},
child: Text('Text Of Button'),
)
Old answer for Flutter less than 2.0:
The correct solution would be to use the SizedBox.expand widget, which enforces its child to match its parent's size.
SizedBox.expand(
child: RaisedButton(...),
)
There are many alternatives, which allows for more or less customization:
SizedBox(
width: double.infinity,
// height: double.infinity,
child: RaisedButton(...),
)
or using a ConstrainedBox
ConstrainedBox(
constraints: const BoxConstraints(minWidth: double.infinity),
child: RaisedButton(...),
)
Container(
width: double.infinity,
child: RaisedButton(...),
),
After some research, I found out some solution, and thanks to #Günter Zöchbauer,
I used column instead of Container and
set the property to column CrossAxisAlignment.stretch to Fill match parent of Button
new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
new RaisedButton(
child: new Text(
"Submit",
style: new TextStyle(
color: Colors.white,
)
),
colorBrightness: Brightness.dark,
onPressed: () {
_loginAttempt(context);
},
color: Colors.blue,
),
],
),
The size attribute can be provided using ButtonTheme with minWidth: double.infinity
ButtonTheme(
minWidth: double.infinity,
child: MaterialButton(
onPressed: () {},
child: Text('Raised Button'),
),
),
or after https://github.com/flutter/flutter/pull/19416 landed
MaterialButton(
onPressed: () {},
child: SizedBox.expand(
width: double.infinity,
child: Text('Raised Button'),
),
),
The simplest way is to use a FlatButton wrapped inside a Container, The button by default takes the size of its parent and so assign a desired width to the Container.
Container(
color: Colors.transparent,
width: MediaQuery.of(context).size.width,
height: 60,
child: FlatButton(
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0),
),
onPressed: () {},
color: Colors.red[300],
child: Text(
"Button",
style: TextStyle(
color: Colors.black,
fontFamily: 'Raleway',
fontSize: 22.0,
),
),
),
)
Output:
You can set match parent of the widget by
1) set width to double.infinity :
new Container(
width: double.infinity,
padding: const EdgeInsets.only(top: 16.0),
child: new RaisedButton(
child: new Text(
"Submit",
style: new TextStyle(
color: Colors.white,
)
),
colorBrightness: Brightness.dark,
onPressed: () {
_loginAttempt(context);
},
color: Colors.blue,
),
),
2) Use MediaQuery:
new Container(
width: MediaQuery.of(context).size.width,
padding: const EdgeInsets.only(top: 16.0),
child: new RaisedButton(
child: new Text(
"Submit",
style: new TextStyle(
color: Colors.white,
)
),
colorBrightness: Brightness.dark,
onPressed: () {
_loginAttempt(context);
},
color: Colors.blue,
),
),
#Mohit Suthar,
Found one of the best solution for match parent to width as well as height as below
new Expanded(
child: new Container(
padding: EdgeInsets.all(16.0),
margin: EdgeInsets.all(16.0),
decoration: new BoxDecoration(
color: Colors.white,
borderRadius:
const BorderRadius.all(const Radius.circular(8.0)),
border: new Border.all(color: Colors.black, width: 1.0)),
child: new Text("TejaDroid")),
),
Here you can check that the Expanded Controller acquire whole remain width and height.
For match_parent you can use
SizedBox(
width: double.infinity, // match_parent
child: RaisedButton(...)
)
For any particular value you can use
SizedBox(
width: 100, // specific value
child: RaisedButton(...)
)
The simplest way to give match-parent width or height in the given code above.
...
width: double.infinity,
height: double.infinity,
...
There are many ways to make full width button. But I think you should understand the concept of making full width widgets in different scenarios:
When you are using nested widgets then it is hard to identify width of parent widget. Simply you can't specify width in nested widgets. So you should use either Expanded or Column with CrossAxisAlignment as Strech.
In other cases, you can use media query width or double.infinity.
Here are some examples for Nested widgets and single widget:
First:
Expanded( // This will work for nested widgets and will take width of first parent widget.
child: MaterialButton(
onPressed: () {},
child: const Text("Button Text"),
color: Colors.indigo,
textColor: Colors.white,
)
)
Second:
Column( // This will not work if parent widget Row.
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
MaterialButton(
onPressed: () {},
child: const Text("Button Text"),
color: Colors.indigo,
textColor: Colors.white,
)
]
)
Third:
ButtonTheme( // if use media query, then will not work for nested widgets.
minWidth: double.infinity, //Or use 'width: MediaQuery.of(context).size.width'
child: MaterialButton(
onPressed: () {},
child: const Text("Button Text"),
color: Colors.indigo,
textColor: Colors.white,
)
)
Forth:
SizedBox( // if use media query, then will not work for nested widgets.
width: double.infinity, //Or use 'width: MediaQuery.of(context).size.width'
child: MaterialButton(
onPressed: () {},
child: const Text("Button Text"),
color: Colors.indigo,
textColor: Colors.white,
)
)
Fifth:
Container( // if use media query, then will not work for nested widgets.
width: double.infinity, //Or use 'width: MediaQuery.of(context).size.width'
child: MaterialButton(
onPressed: () {},
child: const Text("Button Text"),
color: Colors.indigo,
textColor: Colors.white,
)
)
From my point of view, recommended will be the First one. Also you can change MaterialButton to ElevatedButton or TextButton or RaisedButton (Depreciated) or any other widget.
Cheers!
you can do that with MaterialButton
MaterialButton(
padding: EdgeInsets.all(12.0),
minWidth: double.infinity,
onPressed: () {},
child: Text("Btn"),
)
You can set the fixedSize.width of the ButtonStyle to a very large number, like double.maxFinite. You can also use Size.fromWidth() constructor if you don't want to specify the height:
ElevatedButton(
child: const Text('Button'),
style: ElevatedButton.styleFrom(
fixedSize: const Size.fromWidth(double.maxFinite),
),
),
Live Demo
The most basic approach is using Container by define its width to infinite. See below example of code
Container(
width: double.infinity,
child:FlatButton(
onPressed: () {
//your action here
},
child: Text("Button"),
)
)
OutlineButton(
onPressed: () {
logInButtonPressed(context);
},
child: Container(
width: MediaQuery.of(context).size.width / 2,
child: Text(
“Log in”,
textAlign: TextAlign.center,
),
),
)
Something like this works for me.
The Following code work for me
ButtonTheme(
minWidth: double.infinity,
child: RaisedButton(child: Text("Click!!", style: TextStyle(color: Colors.white),), color: Colors.pink, onPressed: () {}))
This is working for me in a self contained widget.
Widget signinButton() {
return ButtonTheme(
minWidth: double.infinity,
child: new FlatButton(
onPressed: () {},
color: Colors.green[400],
textColor: Colors.white,
child: Text('Flat Button'),
),
);
}
// It can then be used in a class that contains a widget tree.
This is working for me.
SizedBox(
width: double.maxFinite,
child: RaisedButton(
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
child: new Text("Button 2"),
color: Colors.lightBlueAccent,
onPressed: () => debugPrint("Button 2"),
),
),
RaisedButton(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [Text('Submit')],
)
)
It works for me.
With Flutter 2.0 RaisedButton is deprecated and replaced by ElevatedButton.
minimumSize property of ElevatedButton widget exactly does that.
Example code:
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.green,
onPrimary: Colors.white,
shadowColor: Colors.greenAccent,
elevation: 3,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0)),
minimumSize: Size(100, 40), //////// HERE
),
onPressed: () {},
child: Text('MyButton'),
)
new SizedBox(
width: 100.0,
child: new RaisedButton(...),
)
Using a ListTile also works as well, since a list fills the entire width:
ListTile(
title: new RaisedButton(...),
),
This one worked for me
width: MediaQuery.of(context).size.width-100,
Wrap your (child widget having a fixed width) with a center widget. This will center your widget:
Center(child:Container(width:250,child:TextButton(child:Text("Button Name),),)
TextButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.blue),
fixedSize: MaterialStateProperty.all(
Size(double.maxFinite, 50.0),
),
),
onPressed: () {},
child: Text('Upgrade to Premium'),
),
TRY
Flexible(
child: Row(
children: [
ElevatedButton(
onPressed: () {},
child: Text("love you}"),
),
],
),
),