How can I display buttons side-by-side in Flutter? - flutter

I would like to display both of my buttons next to each other horizontally. So far I can only display them from top to bottom.
With the following code, what would I have to change ?
new Container(
child: new Column(
children: <Widget>[
new RaisedButton(
child: new Text("LogIn"),
color: Colors.blueAccent[600],
onPressed: null,
),
new RaisedButton(
child: new Text("SignUp"),
color: Colors.blueAccent[600],
onPressed: null,
),
],
),
),

Column is for items vertically arranged (hence a column), you are looking for Row. Just replace Column with Row, the rest of the code is fine. You can also use an Expanded if you want to fill all the available space.

Wrap(
children: <Widget>[
RaisedButton(
...
),
RaisedButton(
...
),
RaisedButton(
...
),
],
),

If you have a column with text in it, and you want two buttons below that text right next to eachother you can use ButtonTheme.bar
Below is some of Flutter's starting code with it. You could plug it in to the starter to see it in action:
Just paste this after the second new text (the one with $counter in it)
new ButtonTheme.bar(
child: new ButtonBar(
alignment: MainAxisAlignment.center,
children: <Widget>[
new RaisedButton(
onPressed: _incrementCounter,
child: new Icon(Icons.add),
color: Colors.green,
),
new RaisedButton(
onPressed: _decrementCounter,
child: new Icon(Icons.remove),
color: Colors.red,
),
],
),
),

child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"Rahul Srivastava",
),
Text(
"Varanasi, India",
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
FlatButton(
onPressed: () {
var player = AudioCache();
player.play('abc.mp3');
},
child: Text('Play')),
FlatButton(
onPressed: () {
var player = AudioCache();
player.play('abc.mp3');
},
child: Text('Pause')),
],
)
],
),

Do something like this
new Column(children: <Widget>[
new Button(
...
...
),
new Button(
...
...
)
])

Just replace Column with that Row in your code It will work.
Although I want to add some extra stuff here suppose that you have Column wrapped inside a row then how will you achieve that displaying of buttons side by side?
Simple just change Column to row n row to column as follows:
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
child: ...
),
child: ..
),
child: ..
)
],
),

Related

How do I make Flutter table stretch vertically

I am new to Flutter and I cannot make my table take all the available space vertically.
Here is the code:
home: Scaffold(appBar: AppBar(),
body: Table(
children: [
TableRow(
children: [
TextButton(onPressed: () {}, child: Text('1')),
TextButton(onPressed: () {}, child: Text('2')),
]
)
]
)
)
This is what I see right now:
Filling the space vertically is not as straight forward and not a default behaviour as it is for the horizontal part. It's because you usually don't want that behaviour: most of the time you want the content itself to determine the space vertically and make a view scrollable if it's too much content (and therefore not enough space available). There are also cases for horizontally scrollable elements, but thats special behaviour then.
If you insist on forcing to fill the space vertically, you need to do that manually. In this case, you have to determine the available space and set the heights of your elements. Best way to determine the available space is making use of the LayoutBuilder widget which gives you that information and updates its subtree if this information changes (since it's a builder widget).
Here you can see a rough and simplified solution using this approach and your example (I enabled the borders so its also visible:
LayoutBuilder(
builder: (context, constraints) => Table(
border: TableBorder.all(),
children: [
TableRow(
children: [
SizedBox(
height: constraints.maxHeight / 2,
child: Align(
child: ElevatedButton(
onPressed: () {},
child: Text('1'),
),
),
),
SizedBox(
height: constraints.maxHeight / 2,
child: Align(
child: ElevatedButton(
onPressed: () {},
child: Text('2'),
),
),
),
],
),
TableRow(
children: [
SizedBox(
height: constraints.maxHeight / 2,
child: Align(
child: ElevatedButton(
onPressed: () {},
child: Text('3'),
),
),
),
SizedBox(
height: constraints.maxHeight / 2,
child: Align(
child: ElevatedButton(
onPressed: () {},
child: Text('4'),
),
),
),
],
),
],
),
),
I ended up giving up on using Table and instead used Column and Row widgets. Here is my new code:
Column(
children: [
Expanded(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(child: TextButton(
child: Text('1'),
onPressed: () {},
)),
Expanded(child: TextButton(
child: Text('2'),
onPressed: () {},
)),
]),
),
]
);

Space Multiple Images Horizontally (Flutter)

I'm trying to space 5 small images horizontally in flutter, but I can't seem to find a simple answer. Maybe I'm just stressed that I can't learn by asking questions... Here's what I have:
body: Center(
child: Row(
children: <Widget> [
Expanded(
child: FlatButton(
onPressed: null,
child: Image.asset('assets/button1.png'),
),
),
],
),
))));
Where would I place the next button code? Thank you for understanding!
Row(
children: <Widget> [
Expanded(
child: FImage.asset('assets/image1.png'),
),
Expanded(
child: Image.asset('assets/image2.png'),
),
Expanded(
child:Image.asset('assets/image3.png'),
Expanded(
child: Image.asset('assets/image4.png'),
),
Expanded(
child: Image.asset('assets/image5.png'),
),
]
That is one way to render 5 images horizontally. I don't know why you had these FlatButton() widgets there.
But remember that Row() widget in general puts widgets horizontally in a row.
There is also the Column() widget whereas it puts its' children widgets vertically, like being in a column.
I think this is what you are looking for
Row(
children: <Widget> [
Expanded(
child: FlatButton(
onPressed: null,
child: Image.asset('assets/button1.png'),
   ),
),
Expanded(
child: FlatButton(
onPressed: null,
child: Image.asset('assets/button1.png'),
   ),
),
Expanded(
child: FlatButton(
onPressed: null,
child: Image.asset('assets/button1.png'),
   ),
),
Expanded(
child: FlatButton(
onPressed: null,
child: Image.asset('assets/button1.png'),
   ),
),
Expanded(
child: FlatButton(
onPressed: null,
child: Image.asset('assets/button1.png'),
   ),
),
]

Flutter - Put the expand arrow at the end of the last line of the text

Basically I want to achieve the similar thing as this post Flutter: How to hide or show more text within certain length. However, I need to replace "show more" with an arrow and the arrow should be at the end of the last line of the collapsed text (not below it). I checked https://pub.dev/packages/expand_widget and https://pub.dev/packages/expandable these two packages, but they didn't seem to satisfy my requirements.
Could anyone help me with it? Thanks!
I think it should work for you!
Row(
children: [
Expanded(
child: Text("your text or widget"),
),
Icon(Icons.keyboard_arrow_down)
],
);
Just have an arrorw_down icon. When clicked change a boolean in setState(). In the widget tree just check if it's true, show something, else don't.
Switch between arrow_up/down depending on the boolean.
Expanded(
flex: 2,
child: InkWell(
onTap: () {
setState(() {
isBubbleExpanded = !isBubbleExpanded; // should expand?
});
},
child: Padding(
padding: EdgeInsets.all(3),
child: Column(
children: <Widget>[
Row(
mainAxisAlignment:
MainAxisAlignment.spaceEvenly,
children: <Widget>[
Center(
child: Icon(isBubbleExpanded
? Icons.keyboard_arrow_up
: Icons.keyboard_arrow_down),
),
],
),
],
),
),
),
));
}
Then somewhere else in the tree:
if (isBubbleExpanded)
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Expanded(
flex: 2,
child: Padding(
padding: EdgeInsets.all(2),
child: Center(),
),
),
Expanded(
flex: 2,
child: Center(
child: IconButton(
icon: const Icon(CupertinoIcons.info),
onPressed: () {
Navigator.of(context).pushNamed(
SomeScreen.routeName,
arguments: widget.document,
);
},
),
),
),
],
),

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

Is there a way to align a widget to the far right of a row in Flutter?

I have a row in Flutter with two widgets. I'm trying to keep the first widget centered in the middle of the screen and the second widget forced to the far right of the screen.
I've tried using Spacer(). This results in the app returning a blank screen.
I've also tried using Expanded. This sends the second widget off the screen completely.
Trying mainAxisAlignment: MainAxisAlignment.spaceBetween did not seem to have any effect.
#override
Widget build(BuildContext context) {
return new Container(
height: MediaQuery.of(context).size.height,
child: SingleChildScrollView(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
new Container(
child: new GestureDetector(
onTap: () {
FocusScope.of(context).requestFocus(new FocusNode());
},
child: Column(
children: <Widget>[
SizedBox(height: 40.0),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Column(
children: <Widget>[
new Container(
child: Row(
mainAxisAlignment:MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(),
Container(
child: Text(
'Profile',
textAlign: TextAlign.center,
style: TextStyle(
fontFamily: 'Lato',
color: Colors.white,
fontSize: 50.0,
fontWeight: FontWeight.w700,
),
),
),
Container(
child: IconButton(
icon: Icon(
Icons.settings,
color: Colors.white,
size: 30.0,
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => OnBoarding()),
);
}),
),
]),
),
),
],
),
],
),
You can use a Row with an Expanded child that contains a Stack. Centre your text with Center and position the icon with Positioned, like so:
[...]
child: Column(
children: <Widget>[
SizedBox(height: 40.0),
Row(
children: <Widget>[
Expanded(
child: Stack(
children: [
Center(
child: Text(...),
),
),
Positioned(
right: 8,
child: IconButton(...),
[...]
Simply just add Spacer() between your main text and the Icon you want to the far right.
For example:
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {},
),
Text(
'Random test text',
style: TextStyle(color: Colors.white),
),
Spacer(),
IconButton(
icon: Icon(Icons.more_vert_rounded),
color: Colors.white,
onPressed: () {},
),
],
)
I hope this helps you. And I hope the format of the code is readable. Still getting a hang of stackoverflow comments
I did this and worked in my project
child:Row(
**crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.end,**
children: [
Icon(MaterialCommunityIcons.comment_outline),
Text("0"),
Icon(Icons.favorite_border),
Text("0"),
],
),
I needed align Icons and Text at right in Row widget
Container(
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.end
children: [ // put your widget list and be happy ]
)
)
enter image description here
Use a row with this following structure:
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(),
Container(
child: Text(
'Profile',
textAlign: TextAlign.center,
style: TextStyle(
fontFamily: 'Lato',
color: Colors.white,
fontSize: 50.0,
fontWeight: FontWeight.w700,
),
),
),
Container(
child: IconButton(
icon: Icon(
Icons.settings,
color: Colors.white,
size: 30.0,
),
),
),
]
),
what will happen is that the spaceBetween property will divide available space equally between the widgets in the row, so I put an empty Container, and this will force the Text widget to be in the middle of the row and the IconButton in the far end as you desire.
I noticed in your code snippet that you have a Column with a single Row which again contains a single Column , you should eliminate this redundancy to optimize your code and make it easier to debug:
Have you tried setting the mainAxisAlignment of the row to mainAxisAlignment.center?
Imo the easiest way to do this is to create a row with 2 children: The first child is an Expanded Row with all your "main" widgets. The second child is your widget which must be aligned to the end.
Row(
children: [
Expanded(
child: Row(
children: [...mainWidgets...],
),
),
...endWidget...
],
),
Note that Expanded is space-greedy. If you use e.g. MainAxisAlignment.center for your Expanded Row, then your children are drawn across the whole avialable width. If this is not to your liking, Id suggest to wrap the Expanded-Row (not Expanded) inside a Container with "constraints: BoxConstraints(maxWidth: 500)". Obviously Expanded shouldnt be Constrained.
Achieve using Expanded & Align like below inside Row:
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
...some other widgets,
Expanded(
//Expanded will help you to cover remaining space of Row
flex: 1,
child: Align(
//Align with alignment.centerRight property will move the child to righteous inside Expanded
alignment: Alignment.centerRight,
child: const Icon(Icons.arrow_back),
),
),
],
),
Note, the Expanded has to be just inside the Row children: <Widget>[],
othewise suppose you've put the GestureDetector inside Row children: <Widget>[], and inside GestureDetector you have put your Expanded, then it won't
work.
Hope this would help many one.