Reputation: 1217
I am facing an issue in flutter where I have a listview inside a card widget. I am reading data from the SQLite database to populate my listview. I don't know how long the listview will be. My problem is that currently I am setting a fixed height to cardview and since my listview can be long or short I get an overflow pixel error.
Here is my code:
import 'package:flutter/material.dart';
import 'package:finsec/widget/single_line_list_view.dart';
import 'package:finsec/widget/header.dart';
import 'package:finsec/data/cardview_list_item.dart';
class cardView extends StatelessWidget {
cardView({
this.header,
this.elevation,
this.padding,
this.query,
this.iconColor
});
String header, query;
double elevation, padding;
Color iconColor;
final shape = const RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(8.0),
topRight: Radius.circular(8.0),
bottomLeft: Radius.circular(8.0),
bottomRight: Radius.circular(8.0),
)
);
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.fromLTRB(0.0, padding, 0.0, 0.0), //vertical padding between cardivew and $s0.00
child: new Card(
elevation: elevation,
shape: shape,
child: new Container(
width: MediaQuery
.of(context)
.size
.width,
height: 165.00,
child: new Column(
children:[
Header(text: header,
textSize: 24.0,
backgroundColor: Color(0xffC4C4C4),
textColor: Colors.black,
height: 50,
padding: 20.0,
headerRadius: 8.0
),
CardviewListItemData(query) //listview goes here
],
),
),
),
);
}
}
If my listview returns 2 items then cardview looks ok but after 3 items or more I get an overflow pixel error. The reason is I am setting the height of the container to 165.00 manually.
see pic below
How can I fix my code so that the cardview auto resizes based on the number of items in my listview? For example, if my listview returns 5 items then cardview should show all five items. I cannot set the height manually because I don't know the number of items on the list. Is there any way to dynamically resize the cardview to show any number of items returned by the listview?
Upvotes: 3
Views: 7582
Reputation: 2447
You can try to surround your card within a Row
widget within IntrinsicHeight
widget with crossAxisAlignment: CrossAxisAlignment.stretch
With this, children of Row (your cards) will expand vertically, but Row will take the least amount of vertical space available.
Container(
child: IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
YourCard(
width: 20.0,
),
],
),
)
)
Upvotes: 5
Reputation: 1243
Seems like the ListView is inside a Column, try to wrap your CardviewListItemData in a Flexible widget.
Upvotes: 0