【问题标题】:How can I expand a container iside a column in flutter without overflowing?如何在不溢出的情况下扩展列内的容器?
【发布时间】:2020-09-07 12:20:09
【问题描述】:

我正在尝试在列内展开容器,但是当我尝试使用展开的小部件时,它会溢出屏幕。如何在不溢出屏幕的情况下展开中间容器?

Column(
          // mainAxisSize: MainAxisSize.,
          // padding: EdgeInsets.all(10),
          // shrinkWrap: true,
          children: <Widget>[
            _PlayingGameAppBar(
              onZero: () => zerar(),
              time: time,
            ),
            Padding(
              padding: EdgeInsets.all(10),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  Container(
                    width: double.infinity,
                    // height: double.infinity,
                    decoration: BoxDecoration(
                      color: Colors.grey[400],
                      borderRadius: BorderRadius.circular(10),
                    ),
                    child: Padding(
                      padding: EdgeInsets.only(
                          top: 8, bottom: 8, left: 4, right: 4),
                      child: Center(
                        child: Text(
                          widget.theme.name,
                          style: TextStyle(
                            fontWeight: FontWeight.bold,
                            color: Colors.black45,
                            fontSize: 18,
                          ),
                        ),
                      ),
                    ),
                  ),
                  SizedBox(height: 7),
                  Container( /// I want to expand this container, to make it fill the avaiable space
                    // constraints: BoxConstraints(minHeight: 100),
                    constraints: BoxConstraints.loose(Size.fromHeight(100)),
                    // height: double.infinity,
                    decoration: BoxDecoration(
                      color: Colors.grey[400],
                      borderRadius: BorderRadius.circular(10),
                    ),
                    child: ListView.builder(
                      shrinkWrap: true,
                      physics: NeverScrollableScrollPhysics(),
                      itemCount: answers.length,
                      itemBuilder: (context, index) {
                        ThemeAnswer answer = answers[index];
                        return Center(
                          child: AnswerTile(
                            answer: answer.answer,
                            points: answer.points,
                            direction: AnswerDirection.left,
                            alignment: AnswerTileAlignment.LEFT,
                            padding: EdgeInsets.only(left: 10),
                          ),
                        );
                      },
                    ),
                  ),
                  SizedBox(height: 10),
                  Container(
                    height: 50,
                    decoration: BoxDecoration(
                      color: Colors.grey[400],
                      borderRadius: BorderRadius.circular(10),
                    ),
                    child: Padding(
                      padding: EdgeInsets.only(left: 10, right: 5),
                      child: TextFormField(
                        controller: _answerController,
                        onChanged: (text) =>
                            setState(() => this.text = text),
                        decoration: InputDecoration(
                          border: InputBorder.none,
                          hintText: loc.answerInputHint,
                          suffixIcon: Opacity(
                            opacity: text.isEmpty ? 0.4 : 1,
                            child: AbsorbPointer(
                              absorbing: text.isEmpty,
                              child: IconButton(
                                icon: Icon(Icons.add, color: Colors.black),
                                onPressed: () => onSend,
                                tooltip: loc.add,
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),

我尝试使用 Expanded 小部件、Flexible 小部件、IntrinsicHeight 小部件进行扩展并使用 MediaQuery 进行定义,但它没有展开

【问题讨论】:

  • 列垂直对齐其子级,并且对大小透视没有任何限制。它只会按原样布置孩子。如果您尝试使用展开到可用垂直空间的东西(例如,包装在 Expanded 中的 Container 小部件)作为子项,则会出现错误。你打算在那个 Container 中保存什么样的信息?你试过给它一定的尺寸吗?您可以使用 MediaQuery.of(context).size 来实现响应。

标签: flutter dart overflow


【解决方案1】:

如果您想让ColumnContainer 扩展而可滚动,请尝试将Column 包装成SingleChildScrollView

如果您想让 Container 在展开时可滚动,请尝试以下操作:

  • 将您的Container 包装成SingleChildScrollView
  • 将您的SingleChildScrollView 包装成Expanded:这将强制容器填充剩余空间并施加一些限制(Column 不施加高度限制,这就是您需要它的原因。)

不管怎样,这应该可以解决问题!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-03
    • 1970-01-01
    • 2021-11-14
    • 2022-11-24
    • 1970-01-01
    相关资源
    最近更新 更多