SingleChildScrollView嵌套在Columns中

huangapple go评论87阅读模式
英文:

SingleChildScrollView inside a nest of Columns

问题

I am having trouble implementing SingleChildScrollView. In my understanding, I can use SingleChildScrollView to make the widgets inside it scrollable. I have created the following basic example in which I want the ElevatedButton to be at the bottom. The remaining space is to be occupied by other widgets. And as per my understanding, if the content in other widgets exceeds the available space, the content should become scrollable because of SingleChildScrollView.

return Scaffold(
  backgroundColor: Colors.white,
  body: SafeArea(
    child: Column(
      children: [
        Text("Some Text Here"),
        Column(
          children: [
            Expanded(
              child: SingleChildScrollView(
                scrollDirection: Axis.vertical,
                child: Column(
                  children: [
                    Container(color: Colors.red, height: 500),
                    Text("HELLO"),
                    Container(color: Colors.red, height: 500),
                  ],
                ),
              ),
            ),
            ElevatedButton(
              onPressed: () {},
              child: const Text("BUTTON"),
            ),
          ],
        )
      ],
    ),
  ),
);

I have used Expanded above SingleChildScrollView so that the remaining space after ElevatedButton has been placed should be the scrollable area so that it has a height constraint.

How can I fix this?

Error: ════════ Exception caught by rendering library
═════════════════════════════════ The following assertion was thrown
during performLayout(): RenderFlex children have non-zero flex but
incoming height constraints are unbounded.

英文:

I am having trouble implementing SingleChildScrollView. In my understanding, I can use SingleChildScrollView to make the widgets inside it scrollable. I have created following basic example in which I want ElevatedButton to be at the bottom. The remaining space is to be occupied by other widgets. And as per my understanding, if the content in other widgets exceeds the available space, the content should become scrollable because of SingleChildScrollView.

return Scaffold(
  backgroundColor: Colors.white,
  body: SafeArea(
    child: Column(
      children: [
        Text("Some Text Here"),
        Column(
          children: [
            Expanded(
              child: SingleChildScrollView(
                scrollDirection: Axis.vertical,
                child: Column(
                  children: [
                    Container(color: Colors.red, height: 500),
                    Text("HELLO"),
                    Container(color: Colors.red, height: 500),
                  ],
                ),
              ),
            ),
            ElevatedButton(
              onPressed: () {},
              child: const Text("BUTTON"),
            ),
          ],
        )
      ],
    ),
  ),
);

I have used Expanded above SingleChildScrollView so that the remaining space after ElevatedButton has been placed should be the scrollable area so that it has a height constraint.

How can I fix this?

> Error: ════════ Exception caught by rendering library
> ═════════════════════════════════ The following assertion was thrown
> during performLayout(): RenderFlex children have non-zero flex but
> incoming height constraints are unbounded.

答案1

得分: 1

只需要2列。第一列用于SinglechildScrollView中的布局,第二列用于将SinglechildScrollView与顶部的文本和底部的按钮布局。

return Scaffold(
  backgroundColor: Colors.white,
  body: SafeArea(
    child: Column(
      children: [
        Text("这里有一些文本"),
        Expanded(
          child: SingleChildScrollView(
            scrollDirection: Axis.vertical,
            child: Column(
              children: [
                Container(color: Colors.red, height: 500),
                Text("你好"),
                Container(color: Colors.red, height: 500),
              ],
            ),
          ),
        ),
        ElevatedButton(
          onPressed: () {},
          child: const Text("按钮"),
        ),
      ],
    ),
  ),
);
英文:

You only need 2 Columns. First column is for the layout in the SinglechildScrollView and the second column is to layout the SinglechildScrollView with the text at the top and the button at the bottom.

return Scaffold(
          backgroundColor: Colors.white,
          body: SafeArea(
            child: Column(
              children: [
                Text("Some Text Here"),
                Expanded(
                  child: SingleChildScrollView(
                    scrollDirection: Axis.vertical,
                    child: Column(
                      children: [
                        Container(color: Colors.red, height: 500),
                        Text("HELLO"),
                        Container(color: Colors.red, height: 500),
                      ],
                    ),
                  ),
                ),
                ElevatedButton(
                  onPressed: () {},
                  child: const Text("BUTTON"),
                ),
              ],
            ),
          ),
        );

答案2

得分: 1

Second Column is getting incoming height constraints are unbounded. You can wrap it with Expanded widget to get available space.

return Scaffold(
  backgroundColor: Colors.white,
  body: SafeArea(
    child: Column(
      children: [
        Text("Some Text Here"),
        Expanded(
          child: Column( //this one 👽🏻
            children: [
              Expanded(
                child: SingleChildScrollView(
                  scrollDirection: Axis.vertical,
                  child: Column(
                    children: [
                      Container(color: Colors.red, height: 500),
                      Text("HELLO"),
                      Container(color: Colors.red, height: 500),
                    ],
                  ),
                ),
              ),
              ElevatedButton(
                onPressed: () {},
                child: const Text("BUTTON"),
              ),
            ],
          ),
        )
      ],
    ),
  ),
);
英文:

Second Column is getting incoming height constraints are unbounded., You can wrap it with Expanded widget to get available space.

return Scaffold(
  backgroundColor: Colors.white,
  body: SafeArea(
    child: Column(
      children: [
        Text("Some Text Here"),
        Expanded(
          child: Column( //this one 👆🏻
            children: [
              Expanded(
                child: SingleChildScrollView(
                  scrollDirection: Axis.vertical,
                  child: Column(
                    children: [
                      Container(color: Colors.red, height: 500),
                      Text("HELLO"),
                      Container(color: Colors.red, height: 500),
                    ],
                  ),
                ),
              ),
              ElevatedButton(
                onPressed: () {},
                child: const Text("BUTTON"),
              ),
            ],
          ),
        )
      ],
    ),
  ),
);

huangapple
  • 本文由 发表于 2023年5月7日 21:19:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194174.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定