Container shrinks to the size of his child - flutter

Why is my Container shrinks to the size of his child?
By the way, the container doesn't shrink if he is in slivers
Container(
height: 100,
decoration: BoxDecoration(
color: Colors.blue[50],
shape: BoxShape.circle,
boxShadow: [
BoxShadow(
color: Colors.black,
blurRadius: 10.0,
offset: Offset(3.0, 10.0),
// spreadRadius: 2.0,
)
],
),
child: Icon(Icons.location_on,
color: Color(0XFF0D47A1), size: 60.0),
),

The way that widgets size themselves is actually quite complicated, and I don't think I can do a better job explaining than flutter's documentation: Layouts in Flutter. Different widget types size themselves differently depending on their children and their parents. So when a container is in a SliverList it might size itself differently than if it isn't.
However, the alignment property of the Container defines how the container sizes itself:
If non-null, the container will expand to fill its parent and position its child within itself according to the given value. If the incoming constraints are unbounded, then the child will be shrink-wrapped instead.
You could alternatively wrap the child in a Center or Alignment widget.

Related

Where are the elevation colors in Flutter material you?

I want my TextField widget to be elevated like Card or NavigationBar widgets. I'm using Material 3 (You).
I've tried using surface color from colors like:
Theme.of(context).colorScheme.surface
but it appears to be the same color as
Theme.of(context).colorScheme.background
You can wrap your text_form_field widget with Card widget and give it elevation as well as boxShadow. It will work for you.
Card(
child: //Your_text_form_field,
elevation: 10,
decoration: BoxDecoration(
boxShadow: [
new BoxShadow(
color: Colors.red,
blurRadius: 20.0,
),
],
),

Text Overflow inside container creating design such as messenger messages in which text is inside container in which height and width not mentioned

I want container to maintain its height and width according to the text without the text overflowing such as if there is small text width is small but if text is long then width takes up size as possible and rest goes to next Line. Please help
Container(
padding: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
child: Text(
message.text,
style: TextStyle(
color: message.isSender
? Colors.white
: Theme.of(context).textTheme.bodyText1?.color,
),
),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(40),
color: KprimaryColor.withOpacity(message.isSender ? 1 : 0.1)),
),
Set width to your container, I think it should fix the problem
You have to Wrap it with Expanded or Flexible it will stretch the width and height
Note: Flexible must be used under the following Widgets Row,Column,Stack.

Why boxShadow can change the background color of the container?

I'm adding a BoxShadow to a Container so I add property BoxShadow and choose the color. But I found I changed the background color of the container at the same time. I can't find the reason. Though I have gotten that I can avoid this by adding property color in decoration, but I am really puzzle about it. Below is the code segment.
decoration: BoxDecoration(
//when I remove this line, background color will change with the BoxShadow color
color: StatisticColor.white,
boxShadow: [
BoxShadow(
color: StatisticColor.grey.withOpacity(0.2),
spreadRadius: 3,
blurRadius: 3,
offset: Offset(0, 0), // changes position of shadow
),
],
border: Border.all(
color: StatisticColor.grey,
),
borderRadius: BorderRadius.circular(4.0),
),
Adding BoxShadow to a Container without any color property defined will change how the Container looks like. By default, the color of a Container is set to transparent. BoxShadow adds a shadow effect on the edges of the Container. Offsetting it will make the BoxShadow move its x and y-position. That is why you see that the background color of the Container change when adding BoxShadow. Also, I would recommend using Card. It is easier to add shadows to a widget using it and it adds a small BorderRadius to the widget. Here is an example:
Card(
elevation:5, //Optional, this intensifies the shadow applied
child: MyWidget()
)

Flutter Transparent Container and BoxShadow

I am stuck on this. I am trying to learn Flutter. While I was doing my custom project I hit this wall. I know this can be solved with AppBar, maybe. I want to learn how to draw custom shadow or at least alter BoxShadow.
When I try to add a box shadow to my row which wrapped with container I get this result;
shadow
Code looks like this;
class HeaderContents extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
boxShadow: [BoxShadow(offset: Offset(0, 2), blurRadius: 5)]),
height: MediaQuery.of(context).size.height * 0.1,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
As you can guess I am just trying to get elevation result to the bottom. Thanks everyone.
Add the SpreaRadius and increase the OffsetY. Change all the value to get the desired need.
BoxDecoration(
color: Colors.white,
boxShadow: [
BoxShadow(
color: Colors.blue.withOpacity(0.1),
spreadRadius: 5,
blurRadius: 20,
offset: Offset(
0, 10), // changes position of shadow
),
],
),
It seems it is just my retardness on assigning color. On main file I assigned both dark and bright colors for testing out dark/light mode functionality.
Changing this,
color: Theme.of(context).primaryColor
to this,
color: Theme.of(context).primaryColorDark
fixed the problem.

Flutter Container not wrapping to size of container when alignment property is used

I have a container A and a container B such that container A contains container B.
I expect the the container A to wrap itself to the size of the child ie. container B as a container is defined in Flutter doc.
It works as expected and the container A size wraps just to fit the Container B. Very well
However, when alignment property is used on the container A, the Container A extends full height without honouring the height of the container B.
Here is my code :
Container(
alignment: Alignment.bottomLeft,
child : Container(child: SizedBox(width:30, height: 300,), color: Colors.green,),
width: 60,
color: Colors.blue,
)
How can I use alignment property of the container expecting normal behaviour?
According to chat conversation this is the required outcome
Container(
color: Colors.red,
child: Row(
children: <Widget>[
Container(
height: 300,
width: 30,
color: Colors.green,
)
],
),
)
I have a second question on this regard..if you can answer. What if i
reverse my case and would want to have my child align to right and
have a breather space to its left?
Add mainAxisAlignment: MainAxisAlignment.end inside your row