Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to auto resize card view in flutter based on the content

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

enter image description here

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?

like image 621
yoohoo Avatar asked Jan 23 '26 06:01

yoohoo


1 Answers

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,
        ),
      ],
    ),
  )
)
like image 135
Anirudh Bagri Avatar answered Jan 24 '26 21:01

Anirudh Bagri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!