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(),
),
),
],
),
)
Related
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,
),
),
],
),
What's the right way to animate a change in size of a Stack widget?
The first idea was to wrap the Stack with a SizeTransition:
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
SizeTransition(
sizeFactor: _height, // Just a Tween<double>(begin: 200, end: 400)
axis: Axis.vertical,
axisAlignment: -1,
child: Stack(
children: [
Positioned(
bottom: 10,
left: 10,
child: Container(width: 50, height: 50, color: Colors.blue),
),
Positioned(
top: 10,
right: 10,
child: Container(width: 50, height: 50, color: Colors.green),
),
],
),
),
],
);
}
But running the snippet causes the following error:
'package:flutter/src/rendering/stack.dart': Failed assertion: line 588 pos 12: 'size.isFinite': is not true.
So, apparently, that's not the right way to do it. Questions now is: How do you animate resizing of a Stack?
Wrapping the Stack with a SizedBox would fix the error message, but then, how do you get the size from SizedTransition to set the proper size of a SizedBox?
You can use AnimatedContainer as parent of Stack widget by providing height or width one you like to change over time.
AnimatedContainer(
color: Colors.purple,
duration: const Duration(milliseconds: 200),
height: height,
child: Stack(
children: [
Positioned(
bottom: 10,
left: 10,
child: Container(width: 50, height: 50, color: Colors.blue),
),
Positioned(
top: 10,
right: 10,
child: Container(width: 50, height: 50, color: Colors.green),
),
],
),
),
With Opacity
Without Opacity
I want to convert my container to opacity like shown above. How to do it?
Here is my code for the container:
Positioned(
bottom: 0,
child: Arc(
arcType: ArcType.CONVEX,
edge: Edge.TOP,
height: 60.0,
child: Opacity(
opacity: 1,
child: new Container(
height: 140,
width: MediaQuery.of(context).size.width,
color: Colors.white.withOpacity(0.8),
),
),
),
),
You can just wrap your Container with BackdropFilter widget so BackdropFilter widget takes one child and filter value. here u can get more info about BackdropFilter Widget
and if you wanna get wrapped your container with BackdropFilter widget you should be do this:
Positioned(
bottom: 0,
child: Arc(
arcType: ArcType.CONVEX,
edge: Edge.TOP,
height: 60.0,
child: BackdropFilter(
filter: ImageFilter.blur(sigmaX: 3, sigmaY: 5),
child: Container(
height: 140,
width: MediaQuery.of(context).size.width,
color: Colors. black.withOpacity(0),
),
),
),
),
what you need is backdropfilter:
Positioned(
bottom: 0,
child: Arc(
arcType: ArcType.CONVEX,
edge: Edge.TOP,
height: 60.0,
child: BackdropFilter(
filter: ImageFilter.blur(sigmaX: 5, sigmaY: 5),
child: Container(
height: 140,
width: MediaQuery.of(context).size.width,
color: Colors. black.withOpacity(0),
),
),
),
),
Add the line
const Color(0xFF0E3311).withOpacity(0.5)
Container(
decoration: new BoxDecoration(
color: const Color(0xFF0E3311).withOpacity(0.5),
),
),
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.
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.