简体   繁体   English

如何根据内容自动调整 flutter 中的卡片视图大小

[英]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.我在 flutter 中遇到了一个问题,我在卡片小部件中有一个列表视图。 I am reading data from the SQLite database to populate my listview.我正在从 SQLite 数据库中读取数据以填充我的列表视图。 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.我的问题是,目前我正在为 cardview 设置一个固定的高度,并且由于我的列表视图可以长或短,我得到一个溢出像素错误。

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.如果我的列表视图返回 2 个项目,那么 cardview 看起来还可以,但是在 3 个或更多项目之后,我得到一个溢出像素错误。 The reason is I am setting the height of the container to 165.00 manually.原因是我手动将容器的高度设置为 165.00。
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.例如,如果我的列表视图返回 5 个项目,那么 cardview 应该显示所有五个项目。 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?有没有办法动态调整卡片视图的大小以显示列表视图返回的任意数量的项目?

You can try to surround your card within a Row widget within IntrinsicHeight widget with crossAxisAlignment: CrossAxisAlignment.stretch您可以尝试使用 crossAxisAlignment: CrossAxisAlignment.stretch 在IntrinsicHeight小部件内的Row小部件中包围您的卡片

With this, children of Row (your cards) will expand vertically, but Row will take the least amount of vertical space available.这样,Row 的子项(您的卡片)将垂直扩展,但 Row 将占用最少的可用垂直空间。

Container(
  child: IntrinsicHeight(
    child: Row(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: <Widget>[
        YourCard(
          width: 20.0,
        ),
      ],
    ),
  )
)

Seems like the ListView is inside a Column, try to wrap your CardviewListItemData in a Flexible widget.似乎 ListView 在一个 Column 内,尝试将您的 CardviewListItemData 包装在一个灵活的小部件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM