i am building a layout where i need to add a horizintal listview inside a column , i,ve tried making it expanded or flexible , and nothind seems to work , this is the column that i am putting the list in :
class HomePage extends StatelessWidget {
HomePage({super.key});
double? h;
double? w;
@override
Widget build(BuildContext context) {
h = MediaQuery.of(context).size.height;
w = MediaQuery.of(context).size.width;
return SafeArea(
child: Scaffold(
body: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
Padding(
padding: EdgeInsets.only(left: (h! * 0.02)),
child: Container(
width: w! * 0.09,
height: h! * 0.06,
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(color: Colors.white),
borderRadius: BorderRadius.circular(15),
),
child: Image.asset(
"assets/images/menu_from_left.png",
// fit: BoxFit.fitHeight,
),
)),
Padding(
padding: EdgeInsets.only(top: h! * 0.04, left: 15.0),
child: Text(
" Discover New Opportunities !",
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
),
SizedBox(
height: h! * 0.02,
),
Padding(
padding: const EdgeInsets.all(15.0),
child: Container(
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(
color: Colors.white,
),
borderRadius: BorderRadius.circular(8)),
child: Row(children: [
Icon(Icons.search),
Expanded(
child: TextField(
decoration: InputDecoration(
border: InputBorder.none,
hintText: "Search for new Jobs"),
showCursor: true,
cursorColor: Colors.black,
))
]),
),
),
SizedBox(
height: h! * 0.0119,
),
Padding(
padding: EdgeInsets.only(left: 15.0),
child: Text(
"For You !",
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
),
SizedBox(
height: h! * 0.0119,
),
Container(
height: 140,
width: 200,
child: Expanded(
child: ListView.builder(
// shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemBuilder: ((context, index) {
return ItemBuilder(h, w, cards[index]);
}),
itemCount: cards.length,
),
),
),
SizedBox(
height: 20,
)
]),
),
);
}
}
and this is the listview item that i am trying to create , it should have more than one item m but i was just trting it first and it didn't work for one item :
Widget ItemBuilder(h, w, Map mp) {
return Padding(
padding: const EdgeInsets.all(20.0),
child: ClipRRect(
borderRadius: BorderRadius.circular(12),
child: Container(
height: 200,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(4),
border: Border.all(color: Colors.black)),
child: Image.asset(
mp["image"],
width: w! * 0.28,
fit: BoxFit.cover,
height: w! * 0.18,
),
),
),
Spacer(),
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: w! * 0.25,
height: w! * 0.18,
decoration: BoxDecoration(
color: mp["jobcontainercolor"],
border: Border.all(
color: mp["jobcontainercolor"]),
borderRadius: BorderRadius.circular(8)),
child: mp["jobcontainertitle"]),
)
]),
),
SizedBox(
height: w! * 0.05,
),
Padding(padding: EdgeInsets.all(8), child: mp["Job_title"]),
Spacer(),
Padding(
padding: EdgeInsets.fromLTRB(8, 4, 0, 24),
child: mp["job_salary"])
]),
color: mp["cardcolor"],
),
));
}
i,ve got couple of exceptions but the last one was this :
FlutterError (RenderFlex children have non-zero flex but incoming width constraints are unbounded.
When a row is in a parent that does not provide a finite width constraint, for example if it is in a horizontal scrollable, it will try to shrink-wrap its children along the horizontal axis. Setting a flex on a child (e.g. using Expanded) indicates that the child is to expand to fill the remaining space in the horizontal direction.
These two directives are mutually exclusive. If a parent is to shrink-wrap its child, the child cannot simultaneously expand to fit its parent.
Consider setting mainAxisSize to MainAxisSize.min and using FlexFit.loose fits for the flexible children (using Flexible rather than Expanded). This will allow the flexible children to size themselves to less than the infinite remaining space they would otherwise be forced to take, and then will cause the RenderFlex to shrink-wrap the children rather than expanding to fit the maximum constraints provided by the parent.
If this message did not help you determine the problem, consider using debugDumpRenderTree():
https://flutter.dev/debugging/#rendering-layer
http://api.flutter.dev/flutter/rendering/debugDumpRenderTree.html
The affected RenderFlex is:
RenderFlex#985b1 relayoutBoundary=up10 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE(creator: Row ← Expanded ← Column ← ColoredBox ← ConstrainedBox ← Container ← ClipRRect ← Padding ← RepaintBoundary ← IndexedSemantics ← _SelectionKeepAlive ← NotificationListener<KeepAliveNotification> ← ⋯, parentData: offset=Offset(0.0, 0.0); flex=1; fit=FlexFit.tight (can use size), constraints: BoxConstraints(0.0<=w<=Infinity, h=0.0), size: MISSING, direction: horizontal, mainAxisAlignment: start, mainAxisSize: max, crossAxisAlignment: start, textDirection: ltr, verticalDirection: down)
The creator information is set to:
Row ← Expanded ← Column ← ColoredBox ← ConstrainedBox ← Container ← ClipRRect ← Padding ← RepaintBoundary ← IndexedSemantics ← _SelectionKeepAlive ← NotificationListener<KeepAliveNotification> ← ⋯
See also: https://flutter.dev/layout/
If none of the above helps enough to fix this problem, please don't hesitate to file a bug:
https://github.com/flutter/flutter/issues/new?template=2_bug.md)
CodePudding user response:
You need to use Expanded
widget directly inside Column
, try this:
Expanded(child: Container(
height: 140,
width: 200,
child: ListView.builder(
// shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemBuilder: ((context, index) {
return ItemBuilder(h, w, cards[index]);
}),
itemCount: cards.length,
),),),
Note
that when you use Expanded
you don't need to set height
for horizontal list.
Update: in your ItemBuilder
, you are calling spacer which mean call infinit space in horizontal list view, try add width to this container in ItemBuilder
:
Container(
height: 200,
width:300,//<--- add this
child: Column(
...
),
),
CodePudding user response:
You can just remove Expanded
and use height on Sizedbox,
SizedBox(
height: 140,
// width: 200, //not needed
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemBuilder: ((context, index) {
To fixed ItemBuilder issue, remove Expanded
from Row widget
Widget ItemBuilder(h, w, Map mp) {
return Padding(
padding: const EdgeInsets.all(20.0),
child: ClipRRect(
borderRadius: BorderRadius.circular(12),
child: Container(
height: 200,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(4),
border: Border.all(color: Colors.black)),
child: Image.asset(
mp["image"],
width: w! * 0.28,
fit: BoxFit.cover,
height: w! * 0.18,
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: w! * 0.25,
height: w! * 0.18,
decoration: BoxDecoration(
// color: mp["jobcontainercolor"],
border: Border.all(
// color: mp["jobcontainercolor"],
),
borderRadius: BorderRadius.circular(8)),
// child: mp["jobcontainertitle"],
),
)
],
),
// SizedBox(
// height: w! * 0.05,
// ),
// Padding(padding: EdgeInsets.all(8), child: mp["Job_title"]),
// Spacer(),
// Padding(
// padding: EdgeInsets.fromLTRB(8, 4, 0, 24),
// child: mp["job_salary"],
// )
]),
// color: mp["cardcolor"],
),
),
);
}
Also try using SizedBox instead of Spacer