Column widget causing layout problems - flutter

I currently have a list made of Containers() (since I cannot use ListView because I need external scrolling), and it's working well except because in each of them, I have a Row(), with two Column() widgets. The first column should be limited print an image, and the second one, to show certain information in rows. It's a very typical way of doing lists, so I thought it shouldn't cause problems.
Thing is, that the second Text() widget in the second Column(), seems to always cause the upper Text() to shift to the right, causing an overflow and making the text unreadable, unreachable by the user. Look at this picture:
Yes, the titles are there (even if you can't see the second one). They are just quite shifted to the right. If I remove the description below, they show up correctly, at the left side, with just a little left padding. Thing is, these titles should be there no matter how elements are below, or at least, that's how I understand the Column() widget should work.
Take a look at the code:
getListItems(List<Artist> artistList) {
// Iterate through the whole Artists array and render a list item for each one
return artistList.map((Artist artist) {
return Container(
margin: EdgeInsets.only(top: 12),
height: 100,
color: Colors.grey[600],
child: Row(
children: <Widget>[
Padding(padding: EdgeInsets.only(left: 15)),
Column(
children: <Widget>[
Image(
width: 100,
height: 100,
fit: BoxFit.cover,
image: artist.avatar)
],
),
Padding(padding: EdgeInsets.only(top: 5)),
Column(
children: <Widget>[
Text(
artist.name,
style: TextStyle(fontSize: 18),
),
Padding(padding: EdgeInsets.only(top: 5)),
Text(artist.description, style: TextStyle(fontSize: 13))
],
)
],
));
}).toList();
What is wrong in this code? Thank you!

You can wrap the second column with Flexible to avoid overflow:
Container(
margin: EdgeInsets.only(top: 12),
height: 100,
color: Colors.grey[600],
child: Row(
children: <Widget>[
Padding(padding: EdgeInsets.only(left: 15)),
Column(
children: <Widget>[
Image(
width: 100,
height: 100,
fit: BoxFit.cover,
image: artist.avatar)
],
),
Flexible(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
artist.name,
style: TextStyle(fontSize: 18),
),
Padding(padding: EdgeInsets.only(top: 5)),
Text(artist.description, style: TextStyle(fontSize: 13))
],
),
)
],
))

You need to make some modifications to the Column which contains the Text widgets
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
artist.name,
style: TextStyle(fontSize: 18),
),
Padding(padding: EdgeInsets.only(top: 5)),
Container(
width: MediaQuery.of(context).size.width * 0.5, // can change "0.5" according to your requirement
child: Text(
artist.description,
style: TextStyle(fontSize: 13),
),
),
],
)

You can use Flexible() widget Wrap your text widget in flexible widget.
return Center(
child: Padding(padding: EdgeInsets.only(left: 50, right: 50),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Flexible(
child: Text('Hii....helloo....how r u?...here you can show multiple line.', textAlign: TextAlign.center,style: TextStyle(fontSize: 20.0, color: Colors.white))),
],
),
)
);

try Expanded
getListItems(List<Artist> artistList) {
// Iterate through the whole Artists array and render a list item for each one
return artistList.map((Artist artist) {
return Container(
margin: EdgeInsets.only(top: 12),
height: 100,
color: Colors.grey[600],
child: Row(
children: <Widget>[
Padding(padding: EdgeInsets.only(left: 15)),
Column(
children: <Widget>[
Image(
width: 100,
height: 100,
fit: BoxFit.cover,
image: artist.avatar)
],
),
Padding(padding: EdgeInsets.only(top: 5)),
Expanded(
child: Column(
children: <Widget>[
Text(
artist.name,
style: TextStyle(fontSize: 18),
),
Padding(padding: EdgeInsets.only(top: 5)),
Text(artist.description, style: TextStyle(fontSize: 13))
],
),
)
],
));
}).toList();

Use Expanded
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
height: 100,
color: Colors.grey[600],
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
Container(
width: 100,
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage("https://picsum.photos/100"),
),
),
),
const SizedBox(width: 8.0),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("Artist Name"),
const SizedBox(height: 8.0),
Text("Artist Description"),
],
),
),
],
),
);
}
}

first of all you can Define the container as a child of Expanded(the container in top of widget)
Expanded(
child:
Container(
margin: EdgeInsets.only(top: 12),
height: 100,
color: Colors.grey[600],......
or you can use
media query,media query, Defines the length or width according to the screen resolution of the phone
you gotta define media query for height or width
use the mediaqueryfor that, MediaQuery.of(context).size.height,
Container(
margin: EdgeInsets.only(top: 12),
height: `MediaQuery.of(context).size.height`,
color: Colors.grey[600],
child: Row(
children: <Widget>[
Padding(padding: EdgeInsets.only(left: 15)),
Column(
children: <Widget>[
Image(
width: 100,
height: 100,
fit: BoxFit.cover,
image: artist.avatar)
],
),
Padding(padding: EdgeInsets.only(top: 5)),
Column(
children: <Widget>[
Text(
artist.name,
style: TextStyle(fontSize: 18),
),
Padding(padding: EdgeInsets.only(top: 5)),
Text(artist.description, style: TextStyle(fontSize: 13))
],
)
],
));
}).toList();
if when you use the media query the overflowed error is showing you gotta say
MediaQuery.of(context).size.height - the pixels that error says pixels are overflowed
it means if error says overflowed by 128 pixels you gotta say
Container( height: MediaQuery.of(context).size.height -128 ,)

Related

AutosizeText doesn't resize text

Im trying to create a custom listTile which can expand if it has multiple lines and maintain photo centered but my problem is AutosizeText isn't working and if I wrap AutosizeText with Expand an error apears.
I can use listTile class if there is a method to know if is tree line or not
Can you help me to solve this?
here is my code:
Card(
elevation: 8,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
SizedBox(
width: 70.0,
height: 70.0,
child: ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Image.asset(
'assets/images/photo.jpg',
width: 70,
height: 70,
fit: BoxFit.cover,
),
),
),
SizedBox(
width: 24,
),
Column(
children: [
AutoSizeText(
'{otherUser.name! otherUser.lastName!}',
maxLines: 1,
style: Theme.of(context).textTheme.bodyText1!),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Container(
margin: const EdgeInsets.symmetric(horizontal: 2),
child: Image(
image: AssetImage(
'assets/icons/icon2.png'),
height: 20,
),
),
AutoSizeText(
otherUser.country![0],
maxLines: 2,
style: Theme.of(context)
.textTheme
.subtitle2!
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Container(
margin: const EdgeInsets.symmetric(horizontal: 2),
child: const FaIcon(
FontAwesomeIcons.locationDot,
size: 20,
color: Colors.black45,
),
),
AutoSizeText(
otherUser.location!,
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: Theme.of(context)
.textTheme
.subtitle2!
),
]),
])
],
),
],
),
Row(
children: [Icon(Icons.star_half_outlined), Text("4.5")],
)
],
),
),
This may be due to the default minimum and maximum size in AutoSizeText widget. Change the minSize and maxSize parameter to some range that you need.
NB: If found helpful, do upvote and mark as right answer, thanks.

how to build Proper ListTIle with image

i always build ListTile like this, the problem is the upper text 'Bluckcurrent' and the bottom text 'STOCK: 10' is not in the same line with the end of image. I want it to match the image height automatically. Sorry for my bad explanation, if you need more information please tell me i will update the question.
Container(
padding: EdgeInsets.fromLTRB(16, 8, 16, 8),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
width: 90,
height: 100,
decoration: BoxDecoration(
color: Constants.blueLightColor.withOpacity(0.2),
borderRadius: BorderRadius.circular(16),
),
child: Icon(
Icons.image_outlined,
size: 42,
color: Constants.blueLightColor,
),
),
SizedBox(width: 10),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'BLACKCURRENT MOUNT TEA',
style: TextStyle(
fontWeight: Constants.bold,
),
maxLines: 2,
),
Text(
'250ml',
style: TextStyle(
height: 1.4,
fontSize: 12,
color: Constants.greyColor,
),
),
Text(
'Rp. 8.000',
style: TextStyle(
height: 1.7,
fontWeight: Constants.bold,
),
),
Text(
'STOCK : 10',
style: TextStyle(
height: 1.7,
fontWeight: Constants.bold,
),
),
],
),
),
SizedBox(width: 16),
],
),
),
Add a sizedBox with specific height wrapping the row widget and the let the image fetch its width. And in the Column of the Text widgets add a spacer where you wish to add extra space
SizedBox(
height: 100,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.network(
"https://www.bastiaanmulder.nl/wp-content/uploads/2013/11/dummy-image-portrait.jpg",
fit: BoxFit.cover,
),
SizedBox(
width: 10,
),
SizedBox(
height: 200,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
"Some title here",
style:
TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
SizedBox(
height: 10,
),
Text("Price: \$60"),
Spacer(),
Text(
"Stock : 10",
style:
TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
)
],
),
),
],
),
),
Just to show it will align properly, increasing the height to 200
Try: set Container height 100
child: Container(
height: 100,
padding: EdgeInsets.fromLTRB(16, 8, 16, 8),...
How about using MainAxisSize.min ?
It will fix column height to children height.
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.min,
children: [
...
],
),
try this.
since image height is dynamic, then it will align vertically with all the column children on the right side
Card(
child: Container(
padding: EdgeInsets.all(8),
width: double.infinity,
child: Stack(
children: [
Positioned(
child: SizedBox(
// no need to set height
// SizedBox will strech following widget on the right
width: 50,
child: Image.network(
'https://picsum.photos/250?image=9',
fit: BoxFit.cover, // you can modified as you need
),
),
top: 0, //
left: 0,
bottom: 0), //
Container(
margin: EdgeInsets.only(left: 60),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'WWWWWWWWWWWWWWWWW',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
),
),
SizedBox(
height: 10,
),
Text("lorem ipsum"),
SizedBox(
height:5,
),
Text("lorem ipsum"),
],
),
),
],
),
),
),
You can use IntrinsicHeight widget to wrap your Row.
IntrinsicHeight(child: Row())

Padding not rendering between header and textfield?

I want to add space between my header and text field, as currently, they are far too close. I have tried adding padding to the row which has the text field, however, there is no movement. Any thoughts on how I could do so?
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
Image.asset(
"assets/Login_Photo.jpg",
height: MediaQuery.of(context).size.height * 0.4,
width: MediaQuery.of(context).size.width,
fit: BoxFit.cover,
),
Padding(
padding: const EdgeInsets.all(50.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Login',
style: TextStyle(
fontSize: 35,
fontFamily: 'Avenir',
fontWeight: FontWeight.w900,
),
),
Row(
padding: const EdgeInsets.all(50.0),
children: <Widget>[
Expanded(
child: TextField(
decoration:
InputDecoration(hintText: "Email Address"))),
I have edited your code, So you can wrap Row in the Container and set margin like as top, bottom ,left, right according to your UI, Please tell me if my code is useful or not.
Widget build(BuildContext context) {
return Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Image.asset(
"assets/Login_Photo.jpg",
height: MediaQuery.of(context).size.height * 0.4,
width: MediaQuery.of(context).size.width,
fit: BoxFit.cover,
),
Padding(
padding: const EdgeInsets.all(50.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Login',
style: TextStyle(
fontSize: 35,
fontFamily: 'Avenir',
fontWeight: FontWeight.w900,
),
),
Container(
margin:
EdgeInsets.only(top: 10, left: 0, right: 0, bottom: 0),
child: Row(
// padding: const EdgeInsets.all(50.0),
children: <Widget>[
Expanded(
child: TextField(
decoration:
InputDecoration(hintText: "Email Address"),
),
),
],
),
),
],
),
)
],
),
);
}
Use SizedBox widget, with the height property, you assign the distance you want to separate. For Exmaple:
SizedBox(height: 10)//change this value for which you need.
Full code, look like this:
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
Image.asset(
"assets/Login_Photo.jpg",
height: MediaQuery.of(context).size.height * 0.4,
width: MediaQuery.of(context).size.width,
fit: BoxFit.cover,
),
Padding(
padding: const EdgeInsets.all(50.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Login',
style: TextStyle(
fontSize: 35,
fontFamily: 'Avenir',
fontWeight: FontWeight.w900,
),
),
Sizedbox(height: 10),
Row(
padding: const EdgeInsets.all(50.0),
children: <Widget>[
Expanded(
child: TextField(
decoration:
InputDecoration(hintText: "Email Address"))),
How strange that it doesn't work for you, maybe one of these alternatives works for you:
Container and give some height:
Column(
children: <Widget>[
Widget1(),
Container(height: 10), // set height
Widget2(),
],
)
Spacer
Column(
children: <Widget>[
Widget1(),
Spacer(), // use Spacer
Widget2(),
],
)
Expanded
Column(
children: <Widget>[
Widget1(),
Expanded(child: SizedBox()), // use Expanded
Widget2(),
],
)
mainAxisAlignment
Column(
mainAxisAlignment: MainAxisAlignment.spaceAround, // mainAxisAlignment
children: <Widget>[
Widget1(),
Widget2(),
],
)
Wrap
Wrap(
direction: Axis.vertical, // make sure to set this
spacing: 20, // set your spacing
children: <Widget>[
Widget1(),
Widget2(),
],
)
Space between Column's children in Flutter

flutter row widget space between doesn't work. how can i fix this?

I wanna give space between Math and A+.
I tried
mainAxisAlignment.spacebetween
insert Spacer()
wrap with Container() and give width : infinity
all doesn't work. don't know why
here's my code
body: Container(
margin: EdgeInsets.only(top: 5),
child: ListView.builder(
itemCount: 20,
itemBuilder: (context, index) {
return Container(
decoration: BoxDecoration(
color: kFadingBackgroundColor,
borderRadius: BorderRadius.all(Radius.circular(5.0)),
boxShadow: [
BoxShadow(
color: Colors.grey[200],
offset: Offset(0.0, 2.5), //(x,y)
blurRadius: 2.0,
),
],
),
margin: EdgeInsets.symmetric(horizontal: 70.w, vertical: 10.h),
padding: EdgeInsets.only(
top: 5.0, bottom: 8.0, left: 45.w, right: 45.w),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
child: Column(
children: <Widget>[
// SizedBox(height: 5),
CircleAvatar(
backgroundColor: Colors.transparent,
child: Image.asset('assets/images/math.png'),
)
],
),
),
SizedBox(width: 30.w),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
child: Row( ✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅
// mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Math',
style: TextStyle(
fontSize: 100.sp, color: blueGray),
),
// SizedBox(
// width: double.infinity,
// ),
// Spacer(),
Text(
'A+',
style: TextStyle(color: blueGray),
),
],
),
),
Container(
width: 300,
child: Text(
'hellowoasasasasassssssssssssssssssssssssssssssssssssssssssssssssssssssssssssrld lroremkasdasdasdasdasdsadasdasdasdasdasdasdasdasdremmws',
softWrap: true,
),
),
],
)
],
));
},
),
),
It looks like your post is mostly code; please add some more details.
It looks like your post is mostly code; please add some more details.
It looks like your post is mostly code; please add some more details.
you can wrap the second column in an Expanded widget and give the Row mainAxisSize.Max and try spacer or space between
or you can try wrapping the Text("Math") in an Expanded widget.
you can find more info on Expanded Widget Here

3 Widget Positions: Image Top and Image Plus Text full width at bottom

I'm having trouble positioning the following on a page:
Image at top
Image + Text touching bottom in that order
Here is the code but I just can't pull it all together:
Widget body = Container(
margin: EdgeInsets.only(top: 30),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
logo,
Container(child: Expanded(child: rotator)),
Container(
width: double.maxFinite,
height: 25,
child: Text("here",
style: TextStyle(
backgroundColor: Colors.teal, fontSize: 20)),
)
]),
));
Image depicting what I'm trying to achieve:
Your question is not specific to me, but maybe this is what you are trying to achieve? Flexible might help solve your problem and this can help you with the responsiveness of your app too
Container(
margin: EdgeInsets.only(top: 30.0),
child: Column(
children: [
Flexible(flex: 2, child: Container(color: Colors.red, width: double.infinity),),
Spacer(),
Flexible(flex: 3, child: Container(color: Colors.green, width: double.infinity)),
Flexible(flex: 1, child: Container(color: Colors.blue, width: double.infinity))
]
)
)
You can try:
Widget body = Container(
margin: EdgeInsets.only(top: 30),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Image1,
Column(
children: <Widget>[
Image2,
TextField(),
],
),
]),
));
Try this,
final body = Container(
margin: EdgeInsets.only(top: 30),
// No need to use the `Center` widget
child: Column(
children: <Widget>[
logo,
Spacer(), // Add this line
rotator, // Use only the required image
SizedBox(
// If you are specifying only the size, `SizedBox` is better.
width: double.maxFinite,
height: 25,
child: Text(
"here",
style: TextStyle(backgroundColor: Colors.teal, fontSize: 20),
),
)
],
),
);