Flutter: Expanded with respect to parent container - flutter

I have a container with screen width*.9. Inside this container, I currently have the following:
SizedBox(
width: MediaQuery.of(context).size.width * .65,
child: Row(
children: [
Expanded(
child: TextSplitter(
wave.message,
context,
Theme.of(context).textTheme.subtitle2!,
)),
],
),
),
I would like to be able to remove the SizedBox wrapping the Row, so that the widget expands into the rest of the space in the parent container. However, currently, removing the sized box leads me to the text disappearing, and throws
RenderFlex children have non-zero flex but incoming width constraints are unbounded.
Extending this sized box leads to overflowing the parent's width, causing an error as well. Any ideas?
Thanks!

Related

Flutter: How can I limit children width of SliverChildListDelegate?

I have a Flutter project in which SliverList is used inside a CustomScrollView. The SliverList has its delegate property set to SliverChildListDelegate. The SliverChildListDelegate is populated with widgets that have their width set to 200. On building the app the widgets take the width of the whole app regardless. If these same widgets are put in a Column under SingleChildScrollView the width is respected and set to 200. How can I limit the with of the children widgets inside SliverChildListDelegate?
This looks more like a trick than an official solution. I just wrap it with Row and put that Widget in between 2 Spacers. E.g:
SliverList(
delegate: SliverChildListDelegate([
Row(
children: [
Spacer(),
Container(
height: 6,
width: 200,
color: Colors.red,
),
Spacer(),
],
),
]),
)

How to make equal width or height constraints between widget in flutter?

In iOS's native AutoLayout constraints, It very easy to make a equal width constraints between ViewA and ViewB . And these two views will get a same width always.
But in flutter ,it seems can't be achived this kindof constraints so easily?
Do I have to make a SizedBox with specific width for both WidgetA and WidgetB explicitly?
You can always put your widgets in a Row(), and wrap every widget in Expanded(). This way they will always have 50% width of the parent Row(). And if you need to constrain the width, just wrap the Row() in a SizedBox().
SizedBox(
width: 500.0,
child: Row(
children: [
Expanded(
child: Container(),
),
Expanded(
child: Container(),
),
],
),
),

Flutter SingleChildScrollView with Expanded

How do you guys solve the following Flutter layout??
I have a screen, where I have to show, as described in the picture: a logo + 3 TextFormFields + 2 buttons + a Container.
Problems:
I need to put all the widgets inside a Column and, the Column inside a SingleChildScrollView so that, when the keyboard pops up, it does not cover the TextFields.
The last widget, the Container, shall take all the remaining screen space on the bottom, but NOT taking more than the screen size. For that, My idea was to use the Expanded widget, so that the Container can expand to the bottom of the screen, but that gives an error:
The following assertion was thrown during performLayout(): RenderFlex children have non-zero flex but incoming height constraints are unbounded.
So I guess my question, in short is, how do I prevent the keyboard to cover the TextFields, while at the same time I force the Container to take all the remaining space on the bottom.
That was my attempt:
SingleChildScrollView(
child: Column(
children: [
Image.asset("assets/images/appLogo.png"),
TextFormField(),
TextFormField(),
TextFormField(),
Row(children: [TextButton(), TextButton()]),
Expanded(child: Container())
],
));
Expanded doesn't know how much size to take. Also the other children don't know their exact size.
Wrap your Image inside a container and give height & width to it. also try wrapping all textfields inside a column or container each.
SingleChildScrollView(
child: Column(
children: [
Container(
width: MediaQuery.of(context).Size.width * 0.4,
height: MediaQuery.of(context).Size.height * 0.2,
child: Image.asset("assets/images/appLogo.png"),
),
Column(
children: [
TextFormField(),
TextFormField(),
TextFormField(),]
)
Row(children: [TextButton(), TextButton()]),
Expanded(child: Container())
],
));
I hope this will work for you.

Flutter scrollable column with expanded children and inside a container

I am struggling with making the column scrollable:
Container(
height: double.infinity,
width: double.infinity,
color: Colors.blue,
child: Column(
children: [
Expanded(flex: 2, child...),
Expanded(flex: 5, child...)
]
)
)
I tried wrapping all the widget inside a single child scroll view, I tried a list view and I tried combing layout builder but nothing works
When you use expanded you should have a limited space (height in column and width in row), in your case your height will fit the space in screen.
If you change the column to the scrollview even change it directly to the ListView or swap it with the SingleChildScrollView, you will have unlimited space and you can't use expanded widget, because it wants to fill all possible space, and it is unlimited in scrollview

Flutter: Expanded vs Flexible

I've used both Expanded and Flexible widgets and they both seem to work the same.
What is the difference between Expanded and Flexible?
Scaffold(
appBar: AppBar(),
body: Column(
children: <Widget>[
Row(
children: <Widget>[
buildExpanded(),
buildFlexible(),
],
),
Row(
children: <Widget>[
buildExpanded(),
buildExpanded(),
],
),
Row(
children: <Widget>[
buildFlexible(),
buildFlexible(),
],
),
],
),
);
Expanded is just a shorthand for Flexible
Using Expanded this way:
Expanded(
child: Foo(),
);
is strictly equivalent to:
Flexible(
fit: FlexFit.tight,
child: Foo(),
);
You may want to use Flexible over Expanded when you want a different fit, useful in some responsive layouts.
The difference between FlexFit.tight and FlexFit.loose is that loose will allow its child to have a maximum size while tight forces that child to fill all the available space.
Widget under Flexible are by default WRAP_CONTENT although you can change it using parameter fit.
Widget under Expanded is MATCH_PARENT you can change it using flex.
Expanded - it is Flexible with set fit
class Expanded extends Flexible {
const Expanded({
Key key,
int flex = 1,
#required Widget child,
}) : super(
key: key,
flex: flex,
fit: FlexFit.tight,
child: child
);
}
You may use Flexible to resize the widgets in rows and columns. It's mainly used to adjust the space of the different child widgets while keeping the relation with their parent widgets.
Meanwhile, Expanded changes the constraints sent to the children of rows and columns; it helps to fill the available spaces there. Therefore, when you wrap your child in an Expanded widget it fills up the empty spaces.
Providing these videos from the Flutter's Official YouTube channel just to help out people, who might look for this in the upcoming future...
Expanded:
Flexible:
Expanded() is nothing more than Flexible() with
Flexible (fit: FlexFit.tight) = Expanded()
but, Flexible uses fit :FlexFit.loose by default.
FlexFit.tight = Wants to fit tight into parent taking as much space as possible.
FlexFit.loose = Wants to fit loose into parent taking as little space as possible for itself.
Expanded changes the constraints of a child widget so it fills any empty space. Expanded widget is a specialised Flexible widget with a set fit - Flexible(fit: FlexFit.tight. Expanded widgets also have a flex property.
Flexible makes the child widget flexible and resizable. You can add the flex or fit property to adjust the size and spacing.
Flexible fit properties include:
FlexFit.loose - The widget’s preferred size is used. (Default)
FlexFit.tight - Forces the widget to fill all of its extra space.
Flexible default will share the available space of the parent widget, but will NOT force the child to fit the space.
Expanded will share the available space of the parent widget, and force the child widget to change its width/height to fill the available space.
In fact, Expanded extends Flexible, which is a Flexible with FlexFit.tight. See the official document.
Here is a Container widget and three Flexible Widgets(flex = 1, fit = FlexFit.loose) in a row. We can see that the three flexible widgets share the same maxWidth (1/3 of the available screen width), and the blue one wants bigger than it, and the others want smaller. But as we can see, the blue guy has maxWidth as its width and the other widgets' width just fit their content.
Here is the code of the image above up:
Row(
mainAxisSize: MainAxisSize.max,
children: [
Container(
color: Colors.teal,
child: Text(
'Container Text ',
)),
Flexible(
child: Container(
color: Colors.blue,
child: Text(' Text.Flexible Text.Flexible Text.Flexible.')),
),
Flexible(
child: Container(
color: Colors.yellow, child: Text('Flexible Text.')),
),
Flexible(
child: Container(
color: Colors.lightGreen, child: Text('Flexible.')),
),
],
)