How can I get the Positioned widget to work in a Stack (flutter)? - flutter

Is something wrong with this snippet? The positioned element isn't showing up but the others are. deviceHeight is defined elsewhere as the height of the device.
The first container holds the other two containers under it. The second container appears at the top correctly, but the third one (positioned) which should be under the second one doesn't appear. Welcome to hear any alternatives.
Align(
alignment: Alignment.bottomCenter,
child: Stack(children: <Widget>[
Container(
color: Color(bgDark),
height: deviceHeight / 100 * 40,
),
Container(color: Colors.red, height: deviceHeight / 18),
Positioned(
top: 50,
child: Container(
color: Colors.green, height: deviceHeight / 1))
]))

Adding a width to the Positioned Container made it visible.
Align(
alignment: Alignment.bottomCenter,
child: Stack(
children: <Widget>[
Container(
color: Colors.blue,
height: 300,
),
Container(
color: Colors.red,
height: 200,
),
Positioned(
top: 50,
child: Container(
color: Colors.green,
height: 100,
width:100,
),
),
],
),
);
I am not sure about the exact cause of the issue but it seems Positioned needed both height and width dimensions.

Related

Center-Align one flutter widget in column and position the rest around it

How do I align one widget of a column in the center and put the rest around it?
If I had a Container or so that holds the three input fields and the button and another one that is the box above. How do I align the Container in the middle and put the box in the remaining space without moving the center container?
I tried using Expanded or Flexible but couldnt figure out how to align the widgets in that kind of way.
You can use Column like this:
Column(
children: [
Expanded(
child: Align(
alignment: Alignment.bottomCenter,
child: Container(
height: 40,
width: 40,
color: Colors.yellow,
),
)),
Container(
color: Colors.red,
height: 100,
width: double.infinity,
),
Expanded(
child: Align(
alignment: Alignment.topCenter,
child: Container(
height: 40,
width: 40,
color: Colors.green,
),
)),
],
),

Flutter: Clip a Column with Expanded when overflow

My use case is:
There are 3 widgets in a Column: widgets 1 and 3 are fixed height, and widget 2 will expand the available content.
Container(
height: 300, // h0: dynamic height
child: Column(
children: [
Container(
color: Colors.green,
width: 100, // h1: fixed height
height: 100,
),
Expanded(
child: Container(
color: Colors.red,
width: 100, // h2: fill remain content
child: Placeholder(),
),
),
Container(
color: Colors.blue,
width: 100, // h3: fixed height
height: 100,
),
],
),
)
When h0 > h1 + h3:
Show all 3 widgets -> Passed
When h0 < h1 + h3:
Show widgets 1 and 3, the widget in Expanded will be invisible -> Passed
Clip the content if overflow -> Failed the content is not clipped and a warning Bottom overflow
I cannot use ScrollView because the content in Expanded should be flexible which ScrollView does not support behavior like that.
I can use ClipRect to clip the overflow content but the warning Bottom overflow still remains. Is there a way to dismiss the warning?
Try wrapping Column with SingleChildScrollView:
Container(
height: 300, // h0: dynamic height
child: CustomScrollView(
slivers: [
SliverFillRemaining(
hasScrollBody: false,
child: Column(
children: [
Container(
color: Colors.green,
width: 100, // h1: fixed height
height: 100,
),
Expanded(
child: Container(
color: Colors.red,
width: 100, // h2: fill remain content
child: Placeholder(),
),
),
Container(
color: Colors.blue,
width: 100, // h3: fixed height
height: 100,
),
],
),
),
],
),
),
You can try it :
Container(
height: 300,
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
color: Colors.green,
width: 100, // h1: fixed height
height: 100,
),
Flexible(
fit: FlexFit.loose,
child: Container(
color: Colors.red,
width: 100, // h2: fill remain content
child: Placeholder(),
),
),
Container(
color: Colors.blue,
width: 100, // h3: fixed height
height: 100,
),
],
),
),
),

Using Align widget in the Stack widget without it expanding

I'm trying to create a custom widget and it makes use of the Stack widget. When I wrap the Stack's children using the Positined widget, the stack hugs the children that are not positioned. When I try to use the Align widget for more precise placement the stack expands to fill all the available space.
I'm trying to avoid using any widget with a fixed size, i.e. Container or SizedBox, as the parent for the Stack widget.
Is there a way to prevent the stack from filling all the available space or work around this that archives the same result?
I'm trying to position a child to the top center of the Stack as an example and this is where I am right now:
Using the Positined widget (I have to try multiple time to get the center position and sometimes its still off):
Positioned(
top: 0,
left: 80,
child: child,
);
Using the Align widget:
Align(
alignment: Alignment.topCenter,
child: child,
);
This should be the result using the Align widget:
Align(
alignment: Alignment.topCenter,
child: child,
);
You can set Stack's alignment to topCenter, Try this:
Stack(
alignment: Alignment.topCenter, // <=== add this
children: [
Container(
color: Colors.grey,
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisSize: MainAxisSize.min, // <=== add this
children: [
Container(
color: Colors.amber,
height: 30,
width: 30,
),
Container(
color: Colors.green,
height: 30,
width: 30,
),
Container(
color: Colors.blue,
height: 30,
width: 30,
),
Container(
color: Colors.yellow,
height: 30,
width: 30,
),
],
),
),
Container(
color: Colors.red,
height: 30,
width: 30,
)
],
)
Note: make sure you set Row's mainAxisSize to min.

two widgets on top of each other

I want to put another widget on the top corner of a container, and I want to put it slightly out of the container as in the picture below.
Use Stack widget and set its clipBehavior to none:
Stack(
clipBehavior: Clip.none, // <--- important part
children: [
Container(
width: 200,
height: 100,
color: Colors.blue,
),
Positioned(
right: -10,
top: -10,
child: Container(
width: 50,
height: 20,
color: Colors.green,
),
),
],
),

How to fix "Container cuts another container" problem

If I set the height of the green container to MediaQuery.of(context).size.height/2 it's work as expected, But set the height of the green container to MediaQuery.of(context).size.height/3 causes the green container to cut the blue container.
How do I get over this?
Stack(
children: <Widget>[
Container(
height: MediaQuery.of(context).size.height/3,
color: Colors.green,
),
Positioned(
top: 80,
right: 30,
left: 30,
child: Container(
height: 200,
width: 400.0,
))
For some reason the green container is working as the "main screen" and it's positioning the rest of the Stack children into it.
So... I just wrapped the main Container into a Column (I think you could use any multi-child layout) and it works as expected.
body: Stack(
children: <Widget>[
Column(
children: [
Container(
height: MediaQuery.of(context).size.height / 3,
color: Colors.green,
),
],
),
Positioned(
top: 80,
right: 30,
left: 30,
child: Container(
color: Colors.blue,
height: 200,
width: 400.0,
),
),
],
),
);
Hope it helps you.