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'),
),
),
]
Related
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: () {},
)),
]),
),
]
);
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,
);
},
),
),
),
],
),
I need to align bottom input field by top input field. Like:
Here is my code:
class CustomerInput extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Row(
children: <Widget>[
Expanded(
child: TextField(),
),
RaisedButton(
child: Icon(Icons.search),
onPressed: () {},
),
],
// ),
),
TextField(
enabled: false,
)
],
);
}
}
How to do it?
Following code will give exact view as you need. In second raw you will add only one TextFormField/TextField for edittext/textview.
Column(
children: <Widget>[
Row(
children: <Widget>[
Expanded(
flex: 3,
child: TextFormField(
),
),
Expanded(
flex: 1,
child: RaisedButton(
child: Icon(Icons.search),
onPressed: () {},
),
),
],
// ),
),
Row(
children: <Widget>[
Expanded(
child: TextFormField(
),
)
],
)
],
));
in this code flex property of TextField is 3 and RaisedButton is 1
"sum equality of these is 4"
TextField will expand by 3/4 or 75% and RaisedButton by 1/4 or 25%
by this concept i created new Row and putted in it
TextField its flex is equal to top one .
Container its flex is equal to Raisedbutton's flex .
and the edited code is :
`
Column(
children: <Widget>[
Row(
children: <Widget>[
Expanded(
flex: 3,
child: TextField(),
),
Expanded(
flex: 1,
child: RaisedButton(
child: Icon(Icons.search),
onPressed: () {},
),
),
],
// ),
),
Row(
children: <Widget>[
Expanded(
flex: 3,
child: TextField(
enabled: false,
),
),
Expanded(
child: Container(),
flex: 1,
)
],
)
],
))
`
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"),
),
),
],
),
),
);
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: ..
)
],
),