Flutter: Clip a Column with Expanded when overflow - flutter

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

Related

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

Container does not get max width inside a Wrap widget?

I have a Container widget inside a Wrap widget, and I want the Container to have the maximum width inside the Wrap, but when I use width: double.infinity for the width of the Container, it throws an error. How can I achieve my target? The code is below:
Container(
width: 150,
color: Colors.grey.shade300,
child: Wrap(
direction: Axis.vertical,
children: [
Text("Hi there!"),
Text("This is a very long long string to show!"),
Container(
color: Colors.teal,
height: 5,
width: double.infinity,
),
Text("The end."),
],
),
),
Use a row to get full width
Container(
width: 150,
color: Colors.grey.shade300,
child: Wrap(
direction: Axis.vertical,
children: [
Text("Hi there!"),
Text("This is a very long long string to show!"),
Row(
children: [
Container(
color: Colors.teal,
height: 5,
),
]),
Text("The end."),
],
),
),
You've define the top level width, To have the same width, you can just put it inside the container instead of adding widgets. like
Container(
width: 150, //from here
color: Colors.grey.shade300,
child: Wrap(
direction: Axis.vertical,
children: [
Text("Hi there!"),
Text("This is a very long long string to show!"),
Container(
color: Colors.teal,
height: 5,
width: 150, //to here
),
Text("The end."),
],
),
),

Flutter Container on top of rows

I made a bunch of rows. Now I want a Container on top of these rows. How can I do this. I tried it with the stack widget ,but it just stacked everything on top of each other and I had no control over the positioning.
How can I draw a Container on top of a bunch of rows.
Thank you for your help.
You can check the below code, though your asking is little bit confusing. You need to use Positioned and set some value at top and left.
Scaffold(
body: Stack(
children: [
Positioned(
top:0 ,
left: 0,
child: Container(
height: 100,
width: 100,
color: Colors.red,
),
),
Positioned(
top:100 ,
left: 0,
child: Container(
height: 100,
width: 100,
color: Colors.red,
child: Row(),
),
),
Positioned(
top:200 ,
left: 0,
child: Container(
height: 100,
width: 100,
color: Colors.red,
child: Row(),
),
),
Positioned(
top:300 ,
left: 0,
child: Container(
height: 100,
width: 100,
color: Colors.red,
child: Row(),
),
),
],
),
)

How can I get the Positioned widget to work in a Stack (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.

How to put view above the list view?

I have a listview which is equally divided into two parts(act as a background) How can i put a view above it( 100*100 w*h )?(This box should center align & top of root)
here is my tried code -
#override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: <Widget>[
Container(
width: ((context.findRenderObject() as RenderBox).size.width),
height: ((context.findRenderObject() as RenderBox).size.height)/2,
color: darkGreen,
),
// SizedBox(width: 10,),
Container(
width: ((context.findRenderObject() as RenderBox).size.width),
height: ((context.findRenderObject() as RenderBox).size.height)/2,
color: Colors.grey,
),
],
),
);
}
}
First of all, you don't need this line:
((context.findRenderObject() as RenderBox).size.width)
you can replace it with:
double.infinity
and it will take the maximum width the parent Widget can allow.
as for your question, you can wrap your ListView in a Stack:
Container(
width: double.infinity,
height: double.infinity,
child: Stack(
children: <Widget>[
ListView(
children: <Widget>[
Container(
width: double.infinity,
height:
((context.findRenderObject() as RenderBox).size.height) / 2,
color: darkGreen,
),
// SizedBox(width: 10,),
Container(
width: double.infinity,
height:
((context.findRenderObject() as RenderBox).size.height) / 2,
color: Colors.grey,
),
],
),
//place your desired widget here
],
),
),