Flutter Why Wrap doesn't work when surrounding rows? - flutter

Im trying to wrap some content in flutter without success.
I found i can't Wrap Rows like i do with Chips or Text widgets.
Anybody knows why?
These are three sets of Rows , each one with an Icon and a Text, that sits side by side. But in smaller screens it overflows, because there is not enough space (width).
I'd like that the last set of rows (one icon and a label) jumps to next line when the space in current line is over.
Thank you
I have tried to Wrap the Rows with Container, but didn't work.
Row(
children: <Widget>[
Wrap(
children: <Widget>[
Row(
children: <Widget>[
Text('long text 1'),
Text('an icon here'),
],
),
Row(
children: <Widget>[
Text('Anoter Label'),
Text('Anoter icon'),
],
),
// I want this row to jump to next line when there is not space in
// current line
Row(
children: <Widget>[
Text('More Text'),
Text('Moire icon'),
],
),
],
)
],
),
Flutter layout ok large screen
Overflow on smaller screens
When i'm on smaller screens, the image with caption resizes and fits well. But the labels and icons above the image overflow. So id like that the last set of icon/ label the one with $ simbol e price, jumps to a new row. I can wrap when using only text widgets and it works, but then i loose the alignment i have on rows like mainAxisAlignment: MainAxisAlignment.spaceAround

Updated: this is somehow a solution
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Expanded(
child: Wrap(
children: <Widget>[
Icon(Icons.alarm),
Text('60 Minutes'),
],
),
),
Expanded(
child: Wrap(
children: <Widget>[
Icon(Icons.shop),
Text('Lorem ipsumssssssssssssssssssssssssssssssss'),
],
),
),
Expanded(
child: Wrap(
children: <Widget>[
Icon(Icons.attach_money),
Text('Very expensive'),
],
),
)
],
);
...
You can do this
Row(
children: <Widget>[
Expanded(
child: Wrap(
children: <Widget>[
Text('long text 1'),
Text('an icon here'),
Text('Anoter Label'),
Text('Anoter icon'),
// I want this row to jump to next line when there is not space in
// current line
Text('More Text'),
Text('Moire icon'),
],
),
)
],
),
or this
Row(
children: <Widget>[
Expanded(
child: Wrap(
children: <Widget>[
Row(
children: <Widget>[
Text('long text 1'),
Text('an icon here'),
],
),
Row(
children: <Widget>[
Text('Anoter Label'),
Text('Anoter icon'),
],
),
// I want this row to jump to next line when there is not space in
// current line
Row(
children: <Widget>[
Text('More Text'),
Text('Moire icon'),
],
),
],
),
)
],
),

The width of the Row is determined by the mainAxisSize property. If
the mainAxisSize property is MainAxisSize.max, then the width of the
Row is the max width of the incoming constraints. If the mainAxisSize
property is MainAxisSize.min, then the width of the Row is the sum
of widths of the children subject to the incoming constraints.
To resolve your problem just set the mainAxisSize property to MainAxisSize.min on your Row Widget

Related

How to center only one element in a column of 5 elements in flutter

I have 5 elements in a Column, and I make them cross in start,
and I need to add another element 'Text' in the center of the column,
So How do I make my Text centered in the entire Column, and the rest elements to the right start?
In column one element wrap with Align widget
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('in start'),
Align(alignment: Alignment.center, child: Row(children: [ Icon(), Text() ] )),
Align(alignment: Alignment.center, child: Text("test")),
Text("test"),
Text("test"),
Text("test"),
],
),
Ouput:
Result
Code
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("start"),
ElevatedButton(onPressed: () {}, child: Text("ink")),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.ac_unit),
Text("Center"),
],
),
Text("text "),
],
),

Flutter how to right align last column

In Flutter, how do I make the third column fill the width? I'm trying to right-align the edit icon.
Row(
children: [
Column(
children: [ SizedBox(width: 10) ]
),
Column(
children: [ Text("example") ]
),
Column(
children: [ IconButton(icon: Icon(Icons.edit)) ],
),
],
);
All of the things I've tried such as putting it in Expanded() or IntrinsicWidth() or Column(..., crossAxisAlignment: CrossAxisAlignment.stretch) have resulted in "Cannot hit test a render box that has never been laid out."
Place Spacer() before last Column.
wrap your column with Align and then Expanded and then set Alignment:Alignment.centerRight
. this will give you the flexibility to place your widget any place you want.
see Align from Flutter Docs.
Row(
children: [
Column(children: [SizedBox(width: 10)]),
Column(children: [Text("example")]),
Expanded(
child: Align(
alignment: Alignment.centerRight,
child: Column(
children: [
IconButton(
icon: Icon(Icons.edit),
onPressed: () {},
)
],
),
),
),
],
),

Column isn't expanding

I'm trying to make a widget on the very bottom of the screen (above the bottomnavbar).
I added MainAxisAlignment.spaceBetween to the column, but it seems like the Column in the SingleChildScrollView is shrinking to fit. How can I make _BottomOne all the way on the bottom?
Column(children: [
_StayOnTopWidget(),
Expanded(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
_FirstOne(),
_BottomOne(),
],
),
),
)
]),
Expanded widget should be the parent of _BottomOne, which will take remaining space will align itself at the bottom
Column(children: [
_StayOnTopWidget(),
SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
_FirstOne(),
Expanded(
child:
_BottomOne(),
),
],
),
)
]),

How to use space between and Text in Row?

I am trying to use space between to separate two Text widgets but none of mainAxisAlignment options works.
Screenshot of code below.
In the picture, you can see Text2 and Text3 are glued together I want them separated. First child in main Row needs to be expanded 1. Second (the problematic one) needs to be like expanded 0.
Container(
color: Colors.blue,
child: Row(
children: [
Expanded(
child: Container(
color: Colors.orange,
child: Text('Text1'),
),
flex: 1,
),
Column(
children: [
Row(
children: [Text('Text2'), Text('Text3')],
),
Text('Long text Long text Long text'),
],
)
],
),
)
so I realized you used Expanded widget for the first child but not for the second. Also, you need to add mainAxisAlignment: MainAxisAlignment.spaceBetween to the Row widget. Below is a complete code for what you want to achieve.
Container(
color: Colors.blue,
child: Row(
children: [
Expanded(
child: Container(
color: Colors.orange,
child: Text('Text1'),
),
flex: 1,
),
Expanded(
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text('Text2'),
Text('Text3')
],
),
Text('Long text Long text Long text'),
],
),
)
],
),
)
Use mainAxisAlignment to spaceBetween
Row(
mainAxisAlignment:MainAxisAlignment.spaceBetween,
children: [Text('Text2'), Text('Text3')],
),
Use Spacer() between two Text.
Text("Text2"),
Spacer(),
Text("Text3")

How to center only one element in a row of 2 elements in flutter

In my layout I have two Widgets in a row, one text and the an icon.
As shown below, assume that * refers to the icon, and using "-" to represent the row:
----------------------------
Text *
----------------------------
How do I make my text centered in the entire row, and the icon to the right end ?
The main thing you need to notice is that if you don't want to use Stack you should provide same width-consuming widgets both on left and right.
I would do this with either Expanded and Padding
Row(
children: <Widget>[
Expanded(
child: Padding(
padding: const EdgeInsets.only(left: 32.0),
child: Text(
"Text",
textAlign: TextAlign.center,
),
),
),
Icon(Icons.add, size: 32.0,)
],
)
or with Row's mainAxisAlignment and a SizedBox
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
const SizedBox(width: 32.0),
Text("Text"),
Icon(Icons.add, size: 32.0)
],
)
or with Expanded and a SizedBox on the left instead of the padding :). The padding or extra container on the left is so that the textAlign will truly center the text in the row taking into account the space taken up by the Icon.
Simply use Expanded and Spacer widgets on left/right side and put your widget inside the Expanded:
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Spacer(),
Text('Text'),
Expanded(
child: Text('*'),
),
),
],
),
Result:
1. Using Row (not very flexible though)
Row(
children: <Widget>[
Expanded(child: Center(child: Text('Center'))),
Text("#"),
],
)
2. Using Stack (flexible)
IntrinsicHeight(
child: Stack(
children: [
Align(child: Text('Center')),
Positioned(right: 0, child: Text('#')),
],
),
)
I found it necessary to use a Stack to get Text to be truly centered:
Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Stack(
alignment: Alignment.center,
children: [
Align(
alignment: Alignment.center,
child: Text('Text'),
),
Align(
alignment: Alignment.centerRight,
child: Text('*'),
)
],
),
],
);
For flexibility reasons I recommend to use Expanded widgets:
Row(
children: [
Expanded(flex: 1, child: Container()),
Expanded(
flex: 8,
child: Text('Your Text',
textAlign: TextAlign.center,
),
),
Expanded(flex: 1, child: IconButton(...))
],
)
For my case, I solved my centering problem by adjusting the flex property of the Spacer widget -
Row(children: [
Spacer(flex: 3),//3 was for my case, It might be changed for your widgets
Text('Center'),
Spacer(flex: 1),//1 was also for my case, It might be changed on for your widgets
Text("*")]);
Add another icon in the start and set the opacity to zero.
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
CloseButton(
onPressed: () => Navigator.of(context).pop(),
),
Text('text',),
Opacity(
opacity: 0,
child: Icon(Icons.close),
),
],
)
This worked for me without Stack. But it is the other way around. "Text" goes to left "*" goes to center:
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: Text("text",
style: Theme.of(context).textTheme.caption),
),
Text("*"),
Spacer(),
],
)