Flutter, как я могу установить высоту списка элементов с гибкой высотой с максимальной высотой 300?

#flutter #dart #containers #constraints #height

#флаттер #dart #контейнеры #ограничения #высота

Вопрос:

Это изображение (список ветвей)

  • У меня есть контейнер внутри столбца, который содержит список ветвей.
  • мне нужно, чтобы высота контейнера была гибкой, пока она не достигнет высоты = 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(),
                                                    ),
                                            ],
                                          ),
                                        ),
                                      ),
                                    );
                                  },
                                ),
                              ),
                            ],
                          ),
                        )
 

Ответ №1:

Добавьте в свой столбец в свойстве контейнера mainAxisSize: MainAxisSize.min , и я уверен, вам придется удалить Expanded виджет здесь