How to avoid a stacked widget being clipped by an expanded widget? - flutter

When a Stack widget is inside a Column and next to an Expanded widget, the Stack widget's overflowed items are clipped by the Expanded widget (see below). How can this be fixed? Why does the leading widget not have a higher Z-index value than the next widget?
Code:
final imageUrl =
'https://images.unsplash.com/photo-1603415526960-f7e0328c63b1?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1650&q=80';
final bgUrl = 'https://images.unsplash.com/32/Mc8kW4x9Q3aRR3RkP5Im_IMG_4417.jpg?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=2850&q=80';
return Scaffold(
body: Column(
children: [
Stack(
clipBehavior: Clip.none,
children: [
Container(
height: 120,
decoration: BoxDecoration(
boxShadow: [
BoxShadow(color: Colors.black87.withOpacity(0.5), spreadRadius: 1, blurRadius: 4)
],
color: Colors.black87,
),
),
Positioned(
bottom: -60,
left: 0,
right: 0,
child: Center(
child: Container(
height: 120,
width: 120,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(60.0),
boxShadow: [
BoxShadow(
color: Colors.black87.withOpacity(0.3),
spreadRadius: 1,
blurRadius: 4,
offset: Offset(0, 1))
],
image: DecorationImage(
image: NetworkImage(imageUrl),
fit: BoxFit.cover,
),
),
),
),
)
],
),
Expanded(
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(bgUrl),
fit: BoxFit.cover
)
),
),
)
],
)
);
}

you need to rearrange your widgets a little bit, put Stack outside the column:
return Scaffold(
body: Stack(children:[
Column(
children: [
Container(
height: 120,
decoration: BoxDecoration(
boxShadow: [
BoxShadow(color: Colors.black87.withOpacity(0.5), spreadRadius: 1, blurRadius: 4)
],
color: Colors.black87,
),
),
Expanded(
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(bgUrl),
fit: BoxFit.cover
)
),
),
)
],
),
Positioned(
top: 60,
left: 0,
right: 0,
child: Center(
child: Container(
height: 120,
width: 120,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(60.0),
boxShadow: [
BoxShadow(
color: Colors.black87.withOpacity(0.3),
spreadRadius: 1,
blurRadius: 4,
offset: Offset(0, 1))
],
image: DecorationImage(
image: NetworkImage(imageUrl),
fit: BoxFit.cover,
),
),
),
),
),
],),
);
}

Related

How do I create a selection avatar? in Flutter

I'm creating an app where the user can choose up to avatars, my question is how do I make the avatars have the following state: when selected, the selected avatar should display in a container.
below is an image of the avatar when selected.enter image description here
I'm creating an app where the user can choose up to avatars, my question is how do I make the avatars have the following state: when selected, the selected avatar should display in a container.
below is an image of the avatar when selected.enter image description here
Code:
//avatar selection
Center(
child: Container(
height: 130,
width: 130,
decoration: BoxDecoration(
color: Colors.grey[100],
borderRadius:
BorderRadius.circular(30),
boxShadow: const [
BoxShadow(
blurRadius: 5.0,
color: Colors.grey),
],
),
child: Image.asset(
'assets/images/purple_male.png',
fit: BoxFit.scaleDown,
)),
),
const SizedBox(height: 50),
Padding(
padding: const EdgeInsets.all(1),
child: Row(
children: [
Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(
50),
boxShadow: const [
BoxShadow(
blurRadius: 5.0,
color: Color.fromARGB(
255,
209,
209,
209)),
],
),
height: 90,
width: 90,
child: Image.asset(
'assets/images/purple_male2.png',
fit: BoxFit.scaleDown,
),
),
const SizedBox(width: 20),
Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(
50),
boxShadow: const [
BoxShadow(
blurRadius: 5.0,
color: Color.fromARGB(
255,
209,
209,
209)),
],
),
height: 90,
width: 90,
child: Image.asset(
'assets/images/purple_male.png',
fit: BoxFit.scaleDown,
),
),
],
),
),
),
),
);

Flutter Stack is not Getting Full Height

I want to overlay container to 1st one container in Stack. But when doing, it works but when i give to height first one it just take this height and second container just stucks and didn't take full height.
I want this but "THIS IS MY TEXT" container should take full height but it won't?
But when i use fit : StackFit.expand. it does work what i want but first one got behind it.
here's my code
double topHeightPadding = MediaQuery.of(context).padding.top;
double heightForStack = (MediaQuery.of(context).size.height - 90);
return Scaffold(
body: Stack(
fit : StackFit.expand,
children: <Widget>[
Container(
height: topHeightPadding + 120,
decoration: const BoxDecoration(
color: Color(0xFF141D38),
),
child: const Center(
child: Text(
"SOMETEXT.COM",
style: TextStyle(color: Colors.white),
textScaleFactor: 1.4,
)),
),
Positioned(
top: topHeightPadding + 90,
left: 0.1,
right: 0.1,
bottom: 0,
child: Container(
width: MediaQuery.of(context).size.width,
height: heightForStack,
decoration: const BoxDecoration(
// color: Colors.red,
color: Color(0xFFFFFFFF),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(35),
topRight: Radius.circular(35)),
boxShadow: [
BoxShadow(
color: Color(0xE6808080),
spreadRadius: 5,
blurRadius: 5,
offset: Offset(0, 2),
),
],
),
child: const Center(
child: Text('THIS IS MY TEXT')
),
))
],
),
);
Any help would be Appreciated.
Answering Own Question : https://stackoverflow.com/help/self-answer
I just got how to do it by Adding one more container with height double.infinity.
Code:
double topHeightPadding = MediaQuery.of(context).padding.top;
double heightForStack = (MediaQuery.of(context).size.height - 90);
return Scaffold(
body: Stack(
// fit : StackFit.expand,
children: <Widget>[
Container(height: double.infinity,),
Container(
height: topHeightPadding + 120,
decoration: const BoxDecoration(
color: Color(0xFF141D38),
),
child: const Center(
child: Text(
"SOMETEXT.COM",
style: TextStyle(color: Colors.white),
textScaleFactor: 1.4,
)),
),
Positioned(
top: topHeightPadding + 90,
left: 0.1,
right: 0.1,
bottom: 0,
child: Container(
width: MediaQuery.of(context).size.width,
height: heightForStack,
decoration: const BoxDecoration(
// color: Colors.red,
color: Color(0xFFFFFFFF),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(35),
topRight: Radius.circular(35)),
boxShadow: [
BoxShadow(
color: Color(0xE6808080),
spreadRadius: 5,
blurRadius: 5,
offset: Offset(0, 2),
),
],
),
child: const Center(
child: Text('THIS IS MY TEXT')
),
))
],
),
);

Flutter place an object from a stack on top of container outside that stack

Long story short I have a stack, in which I have an image that is position out of the stack.
body: Column(
children: [
Stack(
clipBehavior: Clip.none,
alignment: Alignment.center,
children: [
_positionedImage(),
],
),
_bodyContainer(),
],
container:
Widget _bodyContainer() {
return Container(
width: MediaQuery.of(context).size.width,
height: 200,
decoration: BoxDecoration(
color: itsyBackground,
borderRadius: BorderRadius.only(topLeft: Radius.circular(20), topRight: Radius.circular(20)),
boxShadow: [BoxShadow(offset: Offset(0, 0), color: Colors.green, spreadRadius: 1, blurRadius: 1),],
),
);
}
How can I make so that picture is on top of the container?
the approach that you used is wrong here.. You don't need a column here. You can simply render a stack an the bottom most widget should be placed first in the stack children.
https://medium.com/flutter-community/a-deep-dive-into-stack-in-flutter-3264619b3a77
An Example:
Stack(
clipBehavior: Clip.none,
alignment: Alignment.center,
children: [
Container(
width: MediaQuery.of(context).size.width,
height: 200,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(20)),
boxShadow: [
BoxShadow(
offset: Offset(0, 0),
color: Colors.green,
spreadRadius: 1,
blurRadius: 1),
],
),
),
Positioned(
top: -10,
child: Image.network(
'https://static.toiimg.com/thumb/msid-31346158,width-748,height-
499,resizemode=4,imgsize-114461/.jpg',
width: 100,
),
),
],
),

How to add text on an Image in flutter?

Here's the code for an image with border,
Container(
child: Image.network(
"https://drive.google.com/uc?export=view&id=139Wu8bbLz5ubRECzdEmZof8crfGhx0oA", height: 140,fit: BoxFit.cover),
decoration: BoxDecoration(
border: Border.all(color: Colors.black12, width: 1),
),
),
How do I add text to it in such a way that I get an output like in the given image ?
you can use Stack, here is a sample:
Stack(
children: [
Container(
child: Image.network(
"https://drive.google.com/ucexport=view&id=1GgUaQNmNaY2h2oRqgBECQZzJ4I6MnV8G",
height: 140,
fit: BoxFit.cover,
),
decoration: BoxDecoration(
borderRadius: const BorderRadius.all(Radius.circular(7)),
border: Border.all(color: Colors.black12, width: 1),
),
),
Positioned(
child: Text(
'Vegetables',
style: TextStyle(
fontSize: 42,
shadows: <Shadow>[
Shadow(
offset: Offset(1.0, 5.0),
blurRadius: 2.0,
color: Color.fromARGB(255, 0, 0, 0),
),
],
),
),
top: 45,
left: 20,
)
],
)
Instead of having image as a child of Container, you can use image:ImageDecoration and have the Text as the child:
Container(
child: Text('Vegetables'),
decoration: BoxDecoration(
image: const DecorationImage(
image: NetworkImage(
'https://drive.google.com/ucexport=view&id=1GgUaQNmNaY2h2oRqgBECQZzJ4I6MnV8G'),
fit: BoxFit.cover,
),
border: Border.all(color: Colors.black12, width: 1),
),
),
Try below code hope its help to you
Container(
height: 100,
width: 200,
child: Text(
'data',
style: TextStyle(
color: Colors.white,
fontSize: 20,
),
),
decoration: BoxDecoration(
image: const DecorationImage(
image: NetworkImage(
'https://drive.google.com/uc?export=view&id=1GgUaQNmNaY2h2oRqgBECQZzJ4I6MnV8G'),
fit: BoxFit.cover,
),
border: Border.all(color: Colors.black12, width: 1),
),
),
Your result screen
You can use Stack Widget to make beautiful designed widget

image from top() and box with text below the image

I need to do something like the image attached
Here is what I've tried:
Container(
height: 300.0,
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.fitWidth,
alignment: FractionalOffset.center,
image: CachedNetworkImageProvider(image_url),
)
),
alignment: Alignment.bottomCenter,
child: Container(
margin: const EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
width: MediaQuery.of(context).size.width * 0.92,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: AutoSizeText(
my_text,
style: TextStyle(fontSize: 19),
maxLines: 1,
textAlign: TextAlign.center
)
),
decoration: BoxDecoration(
color: Colors.white,
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.8),
spreadRadius: 1,
blurRadius: 3,
offset: Offset(1, 1),
),
],
)
)
)
The problems of this code are:
If the image is not high enough, a white bar appears on top of the screen between the app bar and image
The position of the box with the text depends on the height of the container: I need the box is always half inside and half outside the image
You have to use a stack widget for it, but make sure you use BoxFit and as Cover which will fill the entire height of your container.
Stack(
alignment: Alignment.topCenter,
children: <Widget>[
Container(
height: 300.0,
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.cover,
alignment: FractionalOffset.center,
image: AssetImage('assets/images/image.jpg'),
)),
),
Container(
margin: EdgeInsets.only(top: 280),
width: MediaQuery.of(context).size.width * 0.92,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text("jitesh",
style: TextStyle(fontSize: 19),
maxLines: 1,
textAlign: TextAlign.center)),
decoration: BoxDecoration(
color: Colors.white,
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.8),
spreadRadius: 1,
blurRadius: 3,
offset: Offset(1, 1),
),
],
),
)
],
),
For more info
https://medium.com/flutterworld/flutter-text-over-image-bb045a129bae