【问题标题】:Flutter, how can i set the height of list of items with flexible height with max height 300?Flutter,如何设置具有灵活高度且最大高度为 300 的项目列表的高度?
【发布时间】:2021-03-03 21:18:22
【问题描述】:

  • 我有一个容器,里面有一个包含分支列表的列。
  • 我需要将容器的高度设置为灵活,直到它达到高度 = 300,但实际发生的是它始终为 300,如果它只包含一个项目,则高度为 300[但我需要它只取项目的实际高度],如果它包含超过 3 个项目,它也仍然是 300。

 : Container(
                          decoration: BoxDecoration(
                              color: Theme.of(context).primaryColorDark,
                              borderRadius:
                                  BorderRadius.all(Radius.circular(10))),
                          ***constraints: BoxConstraints(
                            maxHeight: 300,
                            minHeight: 170,
                          ),***
                          width: double.infinity,
                          padding: EdgeInsets.all(8),
                          margin: EdgeInsets.only(left: 16, right: 16),
                          child: Column(
                            children: [
                              Align(
                                alignment: Alignment.centerLeft,
                                child: Text(
                                  'Select Branch',
                                  style: TextStyle(
                                      color: Colors.white,
                                      fontSize: 20,
                                      fontWeight: FontWeight.bold),
                                ),
                              ),
                              SizedBox(
                                height: 8,
                              ),
                              Align(
                                alignment: Alignment.centerLeft,
                                child: Padding(
                                  padding: const EdgeInsets.only(left: 8.0),
                                  child: Text(
                                    'Number of branches = ${_branches.length}',
                                    style: TextStyle(
                                        color: Colors.grey, fontSize: 10),
                                  ),
                                ),
                              ),
                              SizedBox(
                                height: 16,
                              ),
                              Expanded(
                                child: ListView.builder(
                                  itemCount: _branches.length,
                                  itemBuilder: (ctx, i) {
                                    return GestureDetector(
                                      ....
                                      child: Card(
                                        elevation: 8,
                                        shape: RoundedRectangleBorder(
                                          borderRadius:
                                              BorderRadius.circular(12),
                                        ),
                                        color:
                                            Theme.of(context).backgroundColor,
                                        child: Container(
                                          padding: EdgeInsets.all(20),
                                          child: Row(
                                            mainAxisSize: MainAxisSize.min,
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            children: [
                                              Text(
                                                _branches[i]
                                                    .branchDistrict
                                                    .name,
                                                style: TextStyle(
                                                    color: Colors.white,
                                                    fontSize: 16),
                                              ),
                                              (_selectedBranchChecked == i)
                                                  ? Container(
                                                      height: 40,
                                                      width: 40,
                                                      child: Image.asset(
                                                        getAssetsName(
                                                            AssetsImage
                                                                .checkIcon),
                                                        fit: BoxFit.cover,
                                                      ),
                                                    )
                                                  : Container(
                                                      padding: EdgeInsets.only(
                                                          top: 5, bottom: 5),
                                                      height: 40,
                                                      width: 40,
                                                      decoration: BoxDecoration(
                                                        shape: BoxShape.circle,
                                                        color: Colors.white,
                                                      ),
                                                      child: SizedBox(),
                                                    ),
                                            ],
                                          ),
                                        ),
                                      ),
                                    );
                                  },
                                ),
                              ),
                            ],
                          ),
                        )

【问题讨论】:

    标签: flutter dart containers constraints height


    【解决方案1】:

    在容器属性mainAxisSize: MainAxisSize.min 中添加到您的列,我敢打赌您必须在此处删除Expanded 小部件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-07
      • 1970-01-01
      • 2021-05-17
      • 2012-12-14
      • 1970-01-01
      • 1970-01-01
      • 2017-03-23
      相关资源
      最近更新 更多