I need a Container to be the size of its contents. It is currently the correct width but the height is too much because the Container is inside an Expanded, therefore the Container takes the height of the Expanded. Is it possible to fix this?
It doesn't help if you set the height of the container.
The page with the Expanded:
Column(
children: <Widget>[
Expanded(
child: Container(
child: Column(
children: <Widget>[
Expanded(
child: Container(
color: Colors.blue,
child: SmallContainers(),
),
flex: 2),
Expanded(
child: Container(
color: Colors.red,
child: OtherContainers(),
),
flex: 3),
],
),
),
flex: 1),
],
),
The page with the smaller Containers that I need its height smaller:
Container(
color: Colors.green,
child: Padding(
padding: EdgeInsets.all(12.0),
child: Text("Different content"),
),
),
You can try use SizedBox
SizedBox(
width: 200.0,
height: 300.0,
child: Container(
color: Colors.green,
child: Padding(
padding: EdgeInsets.all(12.0),
child: Text("Different content"),
),
),
)
Instead of wrapping small containers with Expanded, wrap it with IntrinsicHeight widget. This widget sizes its child to the child's intrinsic height.
Sample code below:
IntrinsicHeight(
child: Container(
color: Colors.blue,
child: SmallContainers(),
),
),
Hope this answers your question.
Related
I'm making Container box with Flexible funtion but it doesn't work flexible rate (3:7).
Could you explain why it doesn't work?
Codes are below.
body: Container(
child: Row(
children: [
Flexible(child: Container(
color: Colors.lightGreenAccent,
child: Text("Hi, Lynn",
style: TextStyle(fontSize: 30),
),
), flex: 3,),
Flexible(child: Container(
color: Colors.blue,
child: const MyStatefulWidget()
), flex: 7,)
],
),
)
When you use Flexible it still makes children smaller than the flex when there is enough space. I believe you want to use Expanded. Try replacing it with that
You can use Expanded widget instead of Flexible
Container(
child: Row(
children: [
Expanded(
child: Container(
color: Colors.lightGreenAccent,
child: Text("Hi, Lynn", style: TextStyle(fontSize: 30),),
),
flex: 3,
),
Expanded(
child: Container(
color: Colors.blue,
child: Text("Hi, Lynn",style: TextStyle(fontSize: 30),)
),
flex: 7,
)
],
),
)
I created two container in a row and want it in center of the screen so i used padding for it, but they more are close to each other, i want to add space between them. here is the snap of output.
Code:
Padding(
padding: EdgeInsets.fromLTRB(40, 250, 30, 0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new Flexible(
flex: 2,
child: Container(
decoration: BoxDecoration(
color: Colors.pink,
borderRadius: BorderRadius.all(Radius.circular(3))),
height: 50,
width: 190,
child: Center(
child: Column(
children: <Widget>[
FlatButton(onPressed: (){}, child: Text("in",style:TextStyle(fontSize: 20))),
],
),
),
),
),
new Flexible(
flex: 2,
child: Container(
decoration: BoxDecoration(
color: Color(int.parse(presentcolor)),
borderRadius: BorderRadius.all(Radius.circular(3))),
height: 50,
width: 190,
child: Center(
child: Column(
children: <Widget>[
FlatButton(onPressed: (){}, child: Text("out",style:TextStyle(fontSize: 20))),
],
),
),
),
),
Use a Spacer or a SizedBox between 2 widgets.
Other option is to set mainAxisAlignment of Row to MainAxisAlignment.spaceBetween.
I started using the Gap package for adding spaces between widgets in rows or columns. Basically you just specify how big the space should be, so an 8px gap would be Gap(8).
Row(
children: [
widget1(),
Gap(8),
widget2(),
]);
The syntax is easy to use and there are special widgets like the MaxGap or SliverGap for specific use cases.
for centering this you can use Center() widget instead of padding and for putting space between the two you can add margin for the first one from the left. the code will be like this:
Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new Flexible(
flex: 2,
child: Container(
margin: EdgeInsets.only(right: 20),
decoration: BoxDecoration(
color: Colors.pink,
borderRadius: BorderRadius.all(Radius.circular(3))),
height: 50,
width: 190,
child: Center(
child: Column(
children: <Widget>[
FlatButton(onPressed: (){}, child: Text("in",style:TextStyle(fontSize: 20))),
],
),
),
),
),
new Flexible(
flex: 2,
child: Container(
decoration: BoxDecoration(
color: Color(int.parse(presentcolor)),
borderRadius: BorderRadius.all(Radius.circular(3))),
height: 50,
width: 190,
child: Center(
child: Column(
children: <Widget>[
FlatButton(onPressed: (){}, child: Text("out",style:TextStyle(fontSize: 20))),
],
),
),
),
),
you can change righ margin to the suitable value for you.
This is the easiest way to add "Text" containing 4 spaces or or however many spaces you like:
Row(
children: [
Flexible(),
Text(" "),
Flexible(),
]);
Need to show image above other image outside the widget margin.
I tried using stack, but it not coming outside.
Stack(children: <Widget>[
Container(child: Image.asset('assets/discover.png')),
Row(
children: <Widget>[
Expanded(child: Text(''), flex: 9),
Expanded(
child: ClipRRect(
borderRadius: new BorderRadius.circular(80.0),
child: Image.asset('assets/user.jpg')),
flex: 6,
),
Expanded(child: Text(''), flex: 9)
],
),
]);
Below source code works for your scenario. You have to give top padding for the image's container and make the entire stack's alignment as topCenter.
Additional things:
Consider giving height and width for the circled image.
Keep the big image widget in AspectRatio widget to occupy the full width of its parent widget and give fit property as well for showing entire image in the right fit.
.
Stack(
alignment: Alignment.topCenter,
children: <Widget>[
Container(
padding: EdgeInsets.only(top: 40),
child: AspectRatio(aspectRatio: 1, child: Image.asset('assets/discover.png', fit: BoxFit.cover),),
),
ClipRRect(
borderRadius: new BorderRadius.circular(40.0),
child: Image.asset('assets/user.jpg', height: 80, width: 80),
),
],
)
return Scaffold(
appBar: AppBar(
title: const Text('Flutter demo'),
),
body: Container(
padding: EdgeInsets.fromLTRB(5, 10, 5, 10),
alignment: Alignment.topCenter,
child: Stack(children: <Widget>[
Container(
margin: EdgeInsets.only(top: 30),
child: ClipRRect(
borderRadius: BorderRadius.circular(20.0),
child: Image.network(
'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcQGLanNnFsLi3QnQFdh-k-mkwG6yrEEXhorSoElObizTnP0_8rR')),
),
Align(
alignment: Alignment.topCenter,
child: CircleAvatar(
radius: 30.0,
backgroundImage: NetworkImage(
'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcSIekuJOwtOWtZl9QX3t46Yz_7RCZ4Kpebnugsst2OFfNl-SGjf'),
backgroundColor: Colors.grey,
),
)
]),
));
Stack(children: <Widget>[
Container(
margin: EdgeInsets.only(top: 50.0),
child: Image.network(
'https://cdn.pixabay.com/photo/2015/06/19/21/24/the-road-815297__340.jpg'),
),
Row(
children: <Widget>[
Expanded(child: Text(''), flex: 9),
Expanded(
child: ClipRRect(
borderRadius: new BorderRadius.circular(80.0),
child: Image.asset('assets/ic_profile.png'),
),
flex: 6,
),
Expanded(child: Text(''), flex: 9)
],
)
]),
In my flutter project, I haved used a Row where I aligned two text horizontally, the first Text size is fixed but the I want the second one to have as much width as the screen size.
Here's is my code-
Container(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: (
Row(
children: <Widget>[
Container(
width: 150,
color: Colors.blue,
child: Text("City: "),
),
Container(
color: Colors.red,
child: Text("London"),
)
],
)
),
),
),
Container(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: (
Row(
children: <Widget>[
Container(
width: 150,
color: Colors.blue,
child: Text("Country: "),
),
Container(
color: Colors.red,
child: Text("England "),
)
],
)
),
),
),
Container(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: (
Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
width: 150,
color: Colors.blue,
child: Text("Address: "),
),
Container(
color: Colors.red,
child: Text("aaaaa zzzzz wwwww qqqqqq rrrrr"),
)
],
)
),
),
),
With the above code I got the following output-
The problem is the black marked section in the image.
So, I need help to know how the text will take as much width as the screen size & rest of the text will be shown in the next line.
The text widget has a property called overflow you can use that to either clip the extra string or put ellipsis. You could try different types of TextOverflow like a clip, ellipsis, fade, etc. As for the putting it to the next line you could set maxLines property to any number you like.
example:
Container(
width: MediaQuery.of(context).size.width / 2
child: Text(
"Hello world!!!!",
maxLines: 2,
overflow: TextOverflow.ellipsis
),
);
This problem can be solved by a widget called Flexible. I had to write few more lines for solving these but it works perfectly for the problem mentioned in the question.
So, here's the code-
Container(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: (
Row(
children: <Widget>[
Container(
width: 150,
color: Colors.blue,
child: Text("City: "),
),
Container(
color: Colors.red,
child: Text("London"),
)
],
)
),
),
),
Container(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: (
Row(
children: <Widget>[
Container(
width: 150,
color: Colors.blue,
child: Text("Country: "),
),
Container(
color: Colors.red,
child: Text("England "),
)
],
)
),
),
),
Container(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: (
Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
width: 150,
color: Colors.blue,
child: Text("Address: "),
),
new Flexible(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
color: Colors.red,
child: Text("aaaaa zzzzz qqqqqq eeeeeeeeeeeeee "),
)
],
),
),
],
)
),
),
),
Inside a card I have a stack with 1) an image and 2) a text inside a container.
How can I make the container width to fill the card width?
Card(
clipBehavior: Clip.antiAlias,
child: Stack(
children: <Widget>[
Positioned.fill(child: Image.network(
image_url,
fit: BoxFit.fitWidth,
),
),
Positioned(
bottom: 0,
child: Container(
padding: new EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
decoration: new BoxDecoration(color: Colors.black12),
child: Row(
children: <Widget>[
Text("test1"),
Text("test2"),
Text("test3"),
],
),
),
),
],
)
);
Set the left and right values to 0 so that the container will behave so.
Card(
clipBehavior: Clip.antiAlias,
child: Stack(
children: <Widget>[
Positioned.fill(child: Image.network(
image_url,
fit: BoxFit.fitWidth,
),
),
Positioned(
bottom: 0,
left: 0,
right: 0,
child: Container(
padding: new EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
decoration: new BoxDecoration(color: Colors.black12),
child: Row(
children: <Widget>[
Text("test1"),
Text("test2"),
Text("test3"),
],
),
),
),
],
)
);
Wrap all the childrens with the Expanded widget.
Row(
children: <Widget>[
Expanded(
child: Text("test1")
),
Expanded(
child: Text("test2")
),
Expanded(
child: Text("test3")
),
],
),
From the Expanded Widget Documentation:
Using an Expanded widget makes a child of a Row, Column, or Flex expand to fill the available space in the main axis (e.g., horizontally for a Row or vertically for a Column). If multiple children are expanded, the available space is divided among them according to the flex factor.