RenderFlex children have non-zero flex error with textfield flutter - flutter

I'm trying to create this design: https://www.uplabs.com/posts/public-transport-app-design?utm_source=extension&utm_medium=click&utm_campaign=muzli (the top of the first one)
But i'm having this issue: renderflex children have non-zero flex but incoming height constraints are unbounded
This is my code:
Scaffold(
body: Container(
width: double.infinity,
height: double.infinity,
child: Stack(
children: [
Positioned(
left: 0,
right: 0,
child: Container(
width: double.maxFinite,
height: MediaQuery.of(context).size.height / 1.5,
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/abstract2.jpg"),
fit: BoxFit.cover)),
)),
Positioned(
left: 0,
top: 5,
child: SizedBox(
height: 20.0,
child: Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: const [
Expanded(child: TextField()),
Icon(Icons.person)
],
),
),
)),
The thing is when I remove replace the TextField() with Text(), the error disapear. I don't know what i'm doing wrong.
Can you help me please ? Thanks

try this.
Stack(
children: [
/// this is usefull cause it says that the stack will cover all of the screen
SizedBox(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
),
///place your image here
Container(
height: MediaQuery.of(context).size.height*0.5,
decoration: const BoxDecoration(color: Colors.red),
// decoration: const BoxDecoration(
// image: DecorationImage(
// image: AssetImage("assets/images/abstract2.jpg"),
// fit: BoxFit.cover)),
),
///text field ned a width or to be flexible to work.
Positioned(
width: MediaQuery.of(context).size.width,
top: MediaQuery.of(context).size.height *0.3,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Flexible(
child: TextFormField(
maxLines: 1,
keyboardType: TextInputType.multiline,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
fillColor: StyleConstants.whiteColor,
filled: true,
border: StyleConstants.defaultBorder,
focusedBorder: StyleConstants.focusedBorder,
enabledBorder: StyleConstants.defaultBorder,
contentPadding: const EdgeInsets.symmetric(
vertical: 20.0, horizontal: 20.0),
),
),
), Icon(Icons.person)],
),
)
],
)

Try this buddy, I think the problem is caused by using expanded in sizedbox. I usually get this error when I use a TextField in a Row. In such cases, you need to wrap the TextField in a SizedBox or Container and define its width and height, or wrap it in a Flexible Widget. Then it is resolved.
child: SizedBox(
height: 20.0,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: const [
Expanded(child: TextField()),
Icon(Icons.person)
],
),

Related

Is there any way to set the max size of the Expanded or Flex widget in Flutter?

Is there any way to set the max size of the Expanded or Flex widget in Flutter?
Containers constraints are not working at all.
Row(
children: [
Container(
decoration: BoxDecoration(
color: Colors.red,
),
child: SizedBox(
height: 30,
width: 50,
),
),
Flexible(
child: Container(
constraints:
const BoxConstraints(minWidth: 50, maxWidth: 100),
decoration: BoxDecoration(
color: Colors.blue,
),
child: SizedBox(
height: 30,
),
),
),
Container(
decoration: BoxDecoration(
color: Colors.green,
),
child: SizedBox(
height: 30,
width: 50,
),
),
],
),
What I want: Max Width
I tryed Everythig. Really.
Limit max width of Container in Flutter
This didn't help me.
The Expanded widget is supposed to take all the space available in a Row or a Column so you can't just limit its size by using SizedBox or any other method but you can do either of two things whichever suits your needs to limit its size:
Using the flex property of the Expanded Widget:
Row(
children: [
Expanded( //wrap all the widgets in your row with expanded
flex:1, //and provide a flex property (higher number equals more space taken)
child: Container(
decoration: BoxDecoration(
color: Colors.red,
),
child: SizedBox(
height: 30,
width: 50,
),
),
),
Expanded(
flex:2, // which means flex:2 will take twice the amount
child: Container(
constraints:
const BoxConstraints(minWidth: 50, maxWidth: 100),
decoration: BoxDecoration(
color: Colors.blue,
),
child: SizedBox(
height: 30,
),
),
),
Expanded(
flex:1, //same for this one
child: Container(
decoration: BoxDecoration(
color: Colors.green,
),
child: SizedBox(
height: 30,
width: 50,
),
),
),
],
),
Result:
Limiting the width of the Row itself:
SizedBox( //wrap your Row with a SizedBox and give it some width
width: 200,
child: Row(
children: [
Container(
Result:
Edit: As you want to have an adaptable container you just need to change some things:
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween, //add mainAxisAlignment to give spaceBetween the containers
children: [
Container(
decoration: BoxDecoration(
color: Colors.red,
),
child: SizedBox(
height: 30,
width: 50,
),
),
Container(
height: 30, //give you container some height instead of a child SizedBox
constraints:
BoxConstraints(minWidth: MediaQuery.of(context).size.width/10, maxWidth: MediaQuery.of(context).size.width/5), // add responsive constraints which change values according to your screen size
decoration: BoxDecoration(
color: Colors.blue,
),
child: Expanded(child: Container(),) //give it an Expanded widget to take in all the maxWidth
),
Container(
decoration: BoxDecoration(
color: Colors.green,
),
child: SizedBox(
height: 30,
width: 50,
),
),
],
),
And then you'll get this:
Shrinked:
Unshrinked:

Flutter Image in Card Widget does not fill the whole card

I want to have a glass look background for my cards. When I pass the container in the card widget, the image is in the middle and too small.
Example:
Example of how it looks
My build method:
final width = MediaQuery.of(context).size.width;
final height = MediaQuery.of(context).size.height;
return Card(
elevation: 6,
color: backgroundColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(14),
),
child: InkWell(
onTap: () {},
child: Container(
width: width / 1.6,
height: height / 4,
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/card/test.png'),
fit: BoxFit.fitWidth,
alignment: Alignment.topCenter,
),
),
child: Padding(
padding: const EdgeInsets.only(left: 16.0, right: 16.0, top: 15, bottom: 0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(groupData.name, overflow: TextOverflow.ellipsis,),
Text(groupData.moduleId, overflow: TextOverflow.ellipsis,),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(child: Text(moduleData.name, overflow: TextOverflow.ellipsis,)),
Image.asset('assets/images/card/group_card_icon.png', height: 60, width: 60, fit: BoxFit.cover,)
],
)
],
),
),
),
),
);
Maybe it's not a code problem and your test.png image has empty transparent space surrounding the actual glass-like background, i just copy pasted your code but changed the fit to BoxFit.fill in your DecorationImage and it works well:
When you want to fill the image in a complete area you need to use fill, replace this with your decoration part of the decoration.
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/card/test.png'),
fit: BoxFit.fill,
alignment: Alignment.topCenter,
),
),

How to expand Container inside a Row?

I have the following code:
return Scaffold(
body: SafeArea(
child: ListView(children: [
Container(
child: Center(
child: Row(children: [
Flexible(
child: Container(
margin: const EdgeInsets.fromLTRB(10, 15, 5, 15),
decoration: BoxDecoration(color: Colors.white),
child: Row(children: [
Container(
height: 20,
width: 20,
decoration: BoxDecoration(color: Colors.black38),
),
Expanded( // <- does nothing. container still 0x0px
child: Container(
decoration: BoxDecoration(color: Colors.black38),
)),
]),
))
])))
]),
));
I am trying to expand the third container to the remaining size of the Row it's in, but somehow it always stays 0x0 pixel in size, until I define a manual size by using height: or/and width:.
Thanks for any help!
I could be wrong, but I think it's actually not 0 x 0 but [remaining width] x 0. You will see that just putting a height will make it visible. Also when putting a 3rd widget in the row you will see it will be pushed all the way to the right.
The thing is that, as far as I understand it, a Row expects it's children to provide a height. Because otherwise it doesn't know how high it needs to be. What height do you expect the Expanded to be? 20 like the other container? full height of the screen? If a widget inside a Row doesn't provide a height it will be made as small as possible, so in this case, invisible.
What probably helps is to wrap the row in an IntrinsicHeight like this:
return Scaffold(
body: SafeArea(
child: ListView(children: [
Container(
child: Center(
child: Row(children: [
Flexible(
child: Container(
margin: const EdgeInsets.fromLTRB(10, 15, 5, 15),
decoration: BoxDecoration(color: Colors.white),
child: IntrinsicHeight(child: Row(children: [
Container(
height: 20,
width: 20,
decoration: BoxDecoration(color: Colors.black38),
),
Expanded( // <- does nothing. container still 0x0px
child: Container(
decoration: BoxDecoration(color: Colors.black38),
)),
]),
)))
])))
]),
));
try this
Container(
child: Center(
child: Row(children: [
Flexible(
child: Container(
margin: const EdgeInsets.fromLTRB(10, 15, 5, 15),
decoration: BoxDecoration(color: Colors.white),
child: Row(children: [
Container(
height: 20,
width: 20,
decoration: BoxDecoration(color: Colors.black38),
),
Expanded(
child: Container(
height: 20,
decoration: BoxDecoration(color: Colors.pink),
),
),
]),
))
]))),

Flutter how to set container on other container with stack

I have simple 2 widget is Stack i need to show the second widget on the end of first widget
Some thing like this
Here is my code
Stack(
children: <Widget>[
Container(
padding: EdgeInsets.only(top: statusBarHeight * 0.8),
height: height * 0.4,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
'assets/images/place2.jpg',
),
fit: BoxFit.fill,
),
),
),
Align(
alignment: Alignment.bottomCenter,
child: ClipRRect(
borderRadius: BorderRadius.only(
topRight: Radius.circular(30), topLeft: Radius.circular(30)),
child: Container(
decoration: BoxDecoration(
color: Colors.white,
),
child: Column(
children: <Widget>[
SizedBox(
height: height * 0.03,
),
Container(
height: height * 0.01,
width: width * 0.1,
color: Colors.pink,
),
SizedBox(
height: height * 0.031,
),
Container(
width: width,
margin: EdgeInsets.only(left: 10),
child: Text(
'PLACES',
textAlign: TextAlign.left,
style: TextStyle(fontSize: 30),
),
),
SizedBox(
height: height * 0.02,
),
Places(),
Places(),
Places(),
Places(),
Places(),
],
),
),
),
)
],
),
In this code the second widget overlaps with first widget and cover the whole screen. I just need to show it and the end of first widget
The widget you are looking for is Positioned.
You use it like
Stack(
children: [
//Behind container, fill screen (can also use Positioned.fill)
Positioned(top:0, left: 0, right: 0, bottom: 0, child: Container()),
//Front most container, aligned bottom, full width
Positioned(left: 0, right: 0, bottom: 0, child: Container()),
]
)
you are doing correctly but because of Column widget size you are got getting your desire output.
You can give mainAxisSize to MainAxisSize.min, so it will only take space which that widget require.
child: Column(
mainAxisSize: MainAxisSize.min, // added line
children: <Widget>[
if Your widget size if big and then also you want same behaviour then you can give height to container and wrap column with SingleChildScrollView.
Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(
height: 200,
width: double.infinity,
child: Text("Text"),
),
Container(
height: MediaQuery.of(context).size.height * 0.7,
decoration: BoxDecoration(
color: primaryColor,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(35),
topRight: Radius.circular(35))),
),
],
),
setting the MainAxisAlignment of Column to MainAxisAlignment.end pushed the container at bottom and gave me the results i wanted.

Flutter Row with two container with centered image and text above it. Like in image attached

How to create Row with two containers with centered image and text above image also centered.
Like in image attached.
enter image description here
Is this what you are trying to do:
class ExampleDesign extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Center(
child: Container(
height: 180,
padding: EdgeInsets.symmetric(
horizontal: 5,
),
child: Row(
children: <Widget>[
Expanded(
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
width: 3,
),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Container(
height: 60,
width: 120,
padding: EdgeInsets.only(left: 10),
decoration: BoxDecoration(
border: Border.all(
width: 3,
color: Colors.black,
),
),
child: Align(
alignment: Alignment.centerLeft,
child: Text('Image'),
),
),
Text('Some text centered'),
],
),
),
),
SizedBox(
width: 20,
),
Expanded(
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
width: 3,
),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Container(
height: 60,
width: 120,
padding: EdgeInsets.only(left: 10),
decoration: BoxDecoration(
border: Border.all(
width: 3,
color: Colors.black,
),
),
child: Align(
alignment: Alignment.centerLeft,
child: Text('Image'),
),
),
Text('Some text centered'),
],
),
),
),
],
),
),
);
}
}
OUTPUT:
I hope this answers your question.
Create a widget using this post How to to display text over the images in Flutter? which returns an image above and a text below, Wrap two of these widgets using a Row. That should work.