skip to Main Content

I’m trying to create a card which can have 2 columns which are in a 3:1 width ratio. The catch is that this card has to have dynamic height according to its contents.
Something like this:
card that i’m trying to create
This what I have right now:
what I have right now
The white space on the top and bottom of the red column needs to also be red.

This is my code:

Card(
      elevation: 2,
      clipBehavior: Clip.antiAlias,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
      margin: const EdgeInsets.only(top: 10, bottom: 0, left: 10, right: 10),
      child: Row(
        children: [
          Flexible(
            flex: 3,
            fit: FlexFit.tight,
            child: Padding(
              padding: const EdgeInsets.symmetric(
                horizontal: 8.0,
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  TimelineTile(
                    alignment: TimelineAlign.start,
                    isFirst: true,
                    afterLineStyle: const LineStyle(color: TruckerAppTheme.grey, thickness: 2),
                    indicatorStyle:
                        IndicatorStyle(color: Colors.white, padding: const EdgeInsets.all(0), iconStyle: IconStyle(fontSize: 28, iconData: Icons.location_on_rounded, color: TruckerAppTheme.grey)),
                    endChild: Container(
                      padding: const EdgeInsets.fromLTRB(8.0, 8, 0, 0),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.stretch,
                        children: [
                          SizedBox(
                              // width: MediaQuery.of(context).size.width * 0.65,
                              child: Text(widget.load.pickupCity != null ? "${widget.load.pickupCity}, ${widget.load.pickupState}" : "${widget.load.pickupState}",
                                  overflow: TextOverflow.visible, style: TruckerAppTheme.locationText)),
                          SizedBox(
                              // width: MediaQuery.of(context).size.width * 0.6,
                              child: Text(DateFormat('dd-MMM-yy').format(DateFormat('yyyy-MM-dd').parse(widget.load.pickupTimeStamp!)).toString(),
                                  overflow: TextOverflow.ellipsis, style: TruckerAppTheme.bodyNormalSmall)),
                        ],
                      ),
                    ),
                  ),
                  TimelineTile(
                    alignment: TimelineAlign.start,
                    isLast: true,
                    beforeLineStyle: const LineStyle(color: TruckerAppTheme.grey, thickness: 2),
                    indicatorStyle:
                        IndicatorStyle(color: Colors.white, padding: const EdgeInsets.all(0), iconStyle: IconStyle(fontSize: 28, iconData: Icons.flag_rounded, color: TruckerAppTheme.grey)),
                    endChild: Container(
                      padding: EdgeInsets.fromLTRB(8.0, 8, 0, (widget.load.dropTimeStamp != null) ? 0 : 7),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.stretch,
                        children: [
                          SizedBox(
                              //  width: MediaQuery.of(context).size.width * 0.6,
                              child: Text(widget.load.dropCity != null ? "${widget.load.dropCity}, ${widget.load.dropState}" : "${widget.load.dropState}",
                                  overflow: TextOverflow.visible, style: TruckerAppTheme.locationText)),
                          (widget.load.dropTimeStamp != null)
                              ? Text(DateFormat('dd-MMM-yy').format(DateFormat('yyyy-MM-dd').parse(widget.load.dropTimeStamp!)).toString(),
                                  overflow: TextOverflow.ellipsis, style: TruckerAppTheme.bodyNormalSmall)
                              : Container(),
                        ],
                      ),
                    ),
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      widget.load.materialType == ""
                          ? Flexible(
                              child: Padding(
                              padding: const EdgeInsets.only(bottom: 8.0),
                              child: Text(
                                "${widget.load.loadWeight} ${widget.load.loadUnits.toString()[0] + widget.load.loadUnits.toString().substring(1).toLowerCase()} • ${VehicleTypeMapping().vehicleTypeMapping[widget.load.vehicleType!]!.value}",
                                style: TruckerAppTheme.quantityText,
                              ),
                            ))
                          : Flexible(
                              child: Padding(
                              padding: const EdgeInsets.only(bottom: 8.0),
                              child: Text(
                                "${widget.load.loadWeight} ${widget.load.loadUnits.toString()[0] + widget.load.loadUnits.toString().substring(1).toLowerCase()} • ${widget.load.materialType} • ${VehicleTypeMapping().vehicleTypeMapping[widget.load.vehicleType!]!.value}",
                                style: TruckerAppTheme.quantityText,
                              ),
                            )),
                      // const Icon(Icons.arrow_forward, color: Colors.grey)
                    ],
                  )
                ],
              ),
            ),
          ),
          Flexible(
            flex: 1,
            fit: FlexFit.tight,
            child: Container(
              padding: const EdgeInsets.only(bottom: 8, top: 8, right: 8, left: 8),
              margin: EdgeInsets.only(right: 0, top: 0, bottom: 0, left: 1),
              decoration: BoxDecoration(color: TruckerAppTheme.primaryColor.withOpacity(0.5), borderRadius: BorderRadius.circular(0)),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    "Price",
                    style: TruckerAppTheme.quantityText,
                  ),
                  widget.load.perTonneAmount == null
                      ? Text(
                          "Pending",
                          style: TruckerAppTheme.pricePendingText,
                        )
                      : Text(
                          "₹ ${widget.load.perTonneAmount} Per Tonne",
                          style: TruckerAppTheme.pricePendingText,
                        ),
                  const SizedBox(
                    height: 40,
                  ),
                  Text(
                    "Bid Now",
                    style: TextStyle(fontSize: 14),
                  ),
                ],
              ),
            ),
          )
        ],
      ),
    )

I have tried using Intrinsic Height, Expanded, etc. but to no avail.

3

Answers


  1. way 1. try to sizedbox widget

    testWidget(BuildContext context) {
            return Card(
              elevation: 2,
              clipBehavior: Clip.antiAlias,
              shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
              margin: const EdgeInsets.only(top: 10, bottom: 0, left: 10, right: 10),
              child: Row(
                children: [
                  Expanded(
                      child: Container(
                    color: Colors.amber,
                  )),
                  SizedBox(
                    width: MediaQuery.of(context).size.width / 3,
                    height: 100,
                    child: Flexible(
                        child: Container(
                      color: Colors.blue,
                    )),
                  )
                ],
              ),
            );
          }
    
    Login or Signup to reply.
  2. I tried this and its working as expected. Make sure you have zero margin for the Container.

              Card(
                  elevation: 2,
                  child: Row(
                    children: [
                      Flexible(
                        flex: 3,
                        fit: FlexFit.tight,
                        child: Container(
                          color: Colors.black,
                          child: Column(
                            mainAxisSize: MainAxisSize.min,
                            children: const [
                              Padding(
                                padding: EdgeInsets.all(4),
                                child: Text('ITEM!'),
                              ),],
                          ),
                        ),
                      ),
                      Flexible(
                        flex: 1,
                        fit: FlexFit.tight,
                        child: Container(
                          color: Colors.red.withOpacity(0.7),
                          padding: const EdgeInsets.all(8),
                          child: Column(
                            mainAxisSize: MainAxisSize.max,
                            children: const [
                              Padding(
                                padding: EdgeInsets.all(4),
                                child: Text('ITEM!'),
                              ),
                         ...
                            ],
                          ),
                        ),
                      )
                    ],
                  ))
    
    Login or Signup to reply.
  3. You can achieve this with a custom clipper as below code

    Widget build(BuildContext context) {
    return Stack(
      children: [
        Container(
          width: MediaQuery.of(context).size.width - 40,
          margin: const EdgeInsets.only(right: 5),
          decoration: BoxDecoration(
            color: Colors.red,
            borderRadius: BorderRadius.circular(10),
          ),
        ),
        ClipPath(
          clipper: PromoCustomClipper(),
          child: Container(
            width: MediaQuery.of(context).size.width - 40,
            margin: const EdgeInsets.only(right: 5),
            decoration: BoxDecoration(
              color: Colors.grey,
              borderRadius: BorderRadius.circular(10),
            ),
            child: Padding(
              padding: const EdgeInsets.only(top: 10, left: 15, right: 125),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text('Content dynamic height'),
                  const SizedBox(height: 15),
                  Text('Content description')
                ],
              ),
            ),
          ),
        ),
      ],
    );
    

    }

    // custom clipper class

    class PromoCustomClipper extends CustomClipper<Path>{
    @override
      getClip(Size size) {
      var path = Path();
      path.lineTo(0, 0);
      path.lineTo(0, size.height);
      path.lineTo(230, size.height);
      path.lineTo(230, 0);
      return path;
    

    }

    @override

    bool shouldReclip(covariant CustomClipper oldClipper) {

    return false;

    }

    }

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search