I have a container that is sized relative to the window size. I need my text widget, which is nested in this container to fill the size of the container, which I can do except the text all stays on one line. I want the text to wrap to that it fills the height of the container and not just its width.
This is my current code,
Container(
width: width * 2.5,
height: height,
color: Colors.white,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Padding(
padding: EdgeInsets.fromLTRB(width / 2, 0, width / 2, 0),
child: Opacity(
opacity: 0.2,
child: Image.asset(
'assets/images/${model?.imageName}',
height: centerIconHeight,
),
),
),
Flexible(
flex: 1,
child: FittedBox(
fit: BoxFit.contain,
child: Expanded(
child: Text(
model?.title ?? '',
softWrap: true,
maxLines: 10,
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 30,
color: Colors.cyan),
),
),
),
),
],
),
),
And this is what it looks like when I run it. Notice the text is too small because it is only on one line and matching the containers width.
FittedBox tries to fit the text in no matter of its size. Just remove it and text will be wrapped.
UPDATE:
Container(
width: width * 2.5,
height: height,
color: Colors.white,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Padding(
padding: EdgeInsets.fromLTRB(width / 2, 0, width / 2, 0),
child: Opacity(
opacity: 0.2,
child: Image.asset(
'assets/images/${model?.imageName}',
height: centerIconHeight,
),
),
),
Flexible(
flex: 1,
child: Text(
model?.title ?? '',
softWrap: true,
maxLines: 10,
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 30,
color: Colors.cyan),
),
),
],
),
),
Related
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())
i am trying to make the home screen scrollable with SingleChildScrollView but reading other questions on Stackoverflow, it seems that Expanded causes problems. I am new to Flutter, so i am here to ask you if you know how could i fix this. I noticed that if i wrap the Column in a SingleChildScrollView widget it works without the Expanded widget, so that's the problem for sure. Do you know how could i replace the Expanded widget? I don't know what height it is going to have, since i am probably going to add more buttons.
Here there is the code:
return Scaffold(
body: Stack(
children: [
Container(
margin: EdgeInsets.symmetric(horizontal: size.width * 0.10),
child: Column(children: [
SizedBox(height: statusBarHeight), // spazio per statusbar
Align(
alignment: Alignment.topLeft,
child: Container(
margin: EdgeInsets.only(top: size.height * 0.04),
height: 20,
// color: Colors.orange,
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Icon(Icons.wb_sunny_sharp, size: 20, color: Color(0xff8276f4)),
SizedBox(width: 8), // Spazio laterale da icona
Text("MER 18 AGO", style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
color: Color(0xff8276f4),
),),
],)
),
),
Container(
margin: EdgeInsets.only(top: 15, bottom: 20),
width: size.width * 0.80,
decoration: BoxDecoration(
// color: Color(0xffaf8eff),
borderRadius: BorderRadius.circular(29.5)
),
child: Text("Ciao, Andrea!", style: TextStyle(
fontWeight: FontWeight.w700,
fontSize: 30,
color: Color(0xff2e3760),
),),
alignment: Alignment.centerLeft,
),
Container(
height: size.height * 0.30,
width: size.width * 0.80,
decoration: BoxDecoration(
color: Color(0xffdee0e3),
image: DecorationImage(
scale: 0.5,
image: AssetImage('assets/images/standing.png')
),
borderRadius: BorderRadius.circular(29.5)
),
),
SizedBox(height: 20),
Align(
alignment: Alignment.topLeft,
child: Container(
child: Text("Cosa vuoi fare oggi?", style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: Color(0xff2e3760),
),),
),
),
Expanded(
child: Container(
// color: Colors.orange,
margin: EdgeInsets.only(top: 20),
child: GridView.count(
padding: EdgeInsets.all(0),
childAspectRatio: 0.8,
crossAxisSpacing: 20,
mainAxisSpacing: 20,
children: <Widget>[
CategoryCard(
title: "Profilo",
numeroicona: 60307,
numerocolore: 0xFF8c7ffd,
),
CategoryCard(
title: "Università",
numeroicona: 58713,
numerocolore: 0xffb08dff,
),
CategoryCard(
title: "Idee",
numeroicona: 58235,
numerocolore: 0xFF1a88ff,
),
CategoryCard(
title: "Progetti",
numeroicona: 61080,
numerocolore: 0xFF4b5982,
),
],
crossAxisCount: 2),
),
),
],),
),
Now it will scroll like this
I used SliverToBoxAdapter on Column, then replaced GridView with SliverGrid.count. I'm making pr to the repo. After checking, let me know this is what you wanted?
I am using a grid view i need to show a 3x3 container. The issue is i am not able to set the height of the container it's not increasing and cut the content.
My code
Container(
padding: EdgeInsets.all(10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
RichText(
text: new TextSpan(
// Note: Styles for TextSpans must be explicitly defined.
// Child text spans will inherit styles from parent
style: new TextStyle(
fontSize: 14.0,
color: Colors.black,
),
children: <TextSpan>[
new TextSpan(
text: 'Service ',
style: new TextStyle(
fontWeight: FontWeight.bold,
fontSize: 22)),
new TextSpan(
text: 'Deals',
style: TextStyle(fontSize: 18)),
],
),
),
Container(
width: Width * 0.1,
height: Height * 0.05,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("images/see.png"),
),
),
),
],
),
),
Container(
child: GridView(
padding: EdgeInsets.all(0),
shrinkWrap: true,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
),
children: [
Card(
child: Container(
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
height: Height * 0.025,
child: Image.asset('images/heartfill.png'),
),
Container(
height: Height * 0.025,
color: Color(0xffD4E6F1),
child: Image.asset('images/add.png'),
)
],
),
Container(
height: Height * 0.11,
child: Image.asset('images/hp.png'),
),
Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
child: Text('\$45'),
),
Row(
children: [
Container(
height: Height * 0.02,
child: Image.asset('images/star.png'),
),
Container(
child: Text('4.4'),
)
],
)
],
),
)
],
),
),
)
],
),
)
I need to achieve this type of boxes
I have done something similar but its cropping the content i need to increase the height of my container Here is whats the issue
Just use childAspectRatio property of SliverGridDelegateWithFixedCrossAxisCount to set the ratio of the cross-axis to the main-axis extent of each child.
for ex
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
childAspectRatio:0.8
),
It is actually the width to height ratio for child of grid view.Adjust it accordingly.
I have a Row widget, which has many containers with different heights depending on their content. I want to have an equal height of them all. How can I achieve this without hardcoding their values?
This is something I have.
But I want the first card to take the height of the row automatically. So that both cards would be of the same height. How can I achieve this?
This is the code for Card Container:
Widget build(BuildContext context) {
final statusMap = statusOptionView();
return Container(
width: 200,
margin: EdgeInsets.only(right: 20.0),
child: Stack(
children: <Widget>[
Container(
padding: EdgeInsets.only(
left: 20.0, right: 20.0, top: 20.0, bottom: 50.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20.0)),
color: Color(0xFFF97F8B),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
Icon(
Icons.access_time,
color: Colors.white,
),
SizedBox(width: 4.0),
Text(
event.startTime,
style: TextStyle(
color: Colors.white,
),
)
],
),
SizedBox(
height: 20.0,
),
Text(
event.title,
style: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 20.0,
color: Colors.white,
),
maxLines: 2,
),
SizedBox(
height: 20.0,
),
Row(
children: <Widget>[
Icon(
Icons.location_on,
color: Colors.white,
),
SizedBox(
width: 4.0,
),
Expanded(
child: Text(
event.venue,
style: TextStyle(
color: Colors.white,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
)
],
),
],
),
),
Positioned(
bottom: 0.0,
right: 0.0,
left: 0.0,
child: GestureDetector(
onTap: () => Navigator.push(context, MaterialPageRoute(
builder: (BuildContext context) => EventDetailsScreen(event: event, status: status))),
child: Container(
padding: EdgeInsets.symmetric(vertical: 8.0),
decoration: BoxDecoration(
color: Colors.purple,
borderRadius: BorderRadius.circular(30.0)),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(
statusMap['icon'],
color: Colors.white,
size: 16.0,
),
SizedBox(
width: 10.0,
),
Text(
statusMap['value'],
style: TextStyle(
color: Colors.white,
),
),
],
),
),
),
)
],
),
);
While it is wrapped inside a Row widget, just like this:
Row(
children: <Widget>[...events.map((event) => EventCard(event: event)).toList()],
)
Use IntrinsicHeight as parent of Row that contains Card
You can use CrossAxisAlignment.stretch to make all children of a Row/Column use the same height/width.
/// Require the children to fill the cross axis.
///
/// This causes the constraints passed to the children to be tight in the
/// cross axis.
stretch,
Check out https://codepen.io/kuhnroyal/pen/wvGormg
In your case, row is a child of column and column gives full available height to its children so row as a child of the column, take full available height so to tell column that only give required height to its children try to give MainAxisSize.min property to Column. I am not sure it works or not but you can try.
Row(
mainAxisAlignment : MainAxisAlignment.spaceEvenly,
.....)
or try wrapping every widget with expanded.
Hoping that it works
I am using stack to show a container but there is one issue i need to show the small date container in mid of stack.
My code
Widget _buildListItem(int index) {
double statusBarHeight = MediaQuery.of(context).padding.top;
double width = MediaQuery.of(context).size.width;
double height = MediaQuery.of(context).size.height;
return ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(20)),
child: Container(
margin: EdgeInsets.only(left: width * 0.1),
child: Stack(
children: <Widget>[
Container(
width: width * 0.6,
height: height * 0.17,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
'assets/images/1.jpg',
),
fit: BoxFit.fill,
),
),
),
Padding(padding: EdgeInsets.only(top: height * 0.17),
child: Container(
height: height * 0.15,
color: Colors.white,
width: MediaQuery.of(context).size.width * 0.6,
child: Padding(
padding: const EdgeInsets.only(top: 7, bottom: 7, left: 7, right: 7),
child: Row(
children: <Widget>[
Column(crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
_events[index]['name'],
style: TextStyle(
color : Color(0xff000000),
fontWeight: FontWeight.bold,
fontSize: 17
),
),
Text(
_events[index]['sentence'],
style: TextStyle(
color : Color(0xff808080),
fontSize: 15
),
textAlign: TextAlign.left,
),
Text(
'${_events[index]['date']} ${_events[index]['month']} ${_events[index]['year']}',
style: TextStyle(
color : Color(0xff808080),
fontSize: 15
),
textAlign: TextAlign.left,
),
],),
Spacer(),
Icon(Icons.favorite_border, size: 20, color : Color(0xff808080),),
],
),
),
),)
],
),
),
);
}
}
Here is the output of my code
I need something this in middle of image and content
Also one thing is there is any icon in flutter or we can add borders colors in the heart as in the second image heart icon?
Positioned widget helps you position the item which is in stack.
Use the Positioned.fill with Align inside a Stack:
Stack(
children: <Widget>[
Positioned.fill(
child: Align(
alignment: Alignment.centerRight,
child: ....
),
),
],
),
Positioned(
left:10,
right:0,
top:height * 0.14,
child:Container(
height:height *0.05
padding: EdgeInsets.symmetric(horizontal: 15,vertical: 10),
decoration:BoxDecoration(
borderRadius:BorderRadius.circular(15),
color:Colors.white
),
child:Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children:<Widget>[
Text("8"),
Text("Jun")
]
)
)
)
add this to your stack as last child so its above other widgets and icon is Icons.favorite