简体   繁体   English

Flutter - 我可以让 SingleChildScrollView 填充可用空间吗?

[英]Flutter - can I make a SingleChildScrollView fill the available space?

I have a Flutter app which I'm building for Android.我有一个正在为 Android 构建的 Flutter 应用程序。 The structure goes broadly like this:结构大致如下:

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("")),
      body: SingleChildScrollView(
        child: Container(
          decoration: const BoxDecoration(
            gradient: ...
          ),
          child: ...
        ),
      )
    );
  }

The goal here is to have the gradient background fill all the screen below the app bar, and if the content is larger than that space then to make it scrollable.这里的目标是让渐变背景填充应用栏下方的所有屏幕,如果内容大于该空间,则使其可滚动。

If I omit the SingleChildScrollView , the Container fills the space.如果我省略SingleChildScrollView ,则Container会填充空间。 But of course if it overflows then there is no scrolling.但当然,如果它溢出,则没有滚动。 With the code as above, the scroll view does its thing on small screens but on large screens the gradient background doesn't fill the whole available area.使用上面的代码,滚动视图在小屏幕上完成它的工作,但在大屏幕上,渐变背景不会填满整个可用区域。

If I change it around like this:如果我像这样改变它:

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("")),
      body: Container(
        decoration: const BoxDecoration(
          gradient: ...
        ),
        child: Column(children: [
          SingleChildScrollView(
            child: ...
          ),
          Expanded(child:Container())
        ]),
      )
    );
  }

then the gradient fills the background but the scroll view doesn't do the right thing - the content overflows the screen but can't be scrolled.然后渐变填充背景但滚动视图没有做正确的事情 - 内容溢出屏幕但无法滚动。 How do I get it to do both?我怎样才能做到这两点?

The reason you're facing this issue is that your container is inside the SingleChildScrollView Widget and is getting scrolled along the SingleChildScrollView Widget.您遇到此问题的原因是您的容器位于SingleChildScrollView小部件内,并且正在沿着SingleChildScrollView小部件滚动。 And Removing the SingleChildScrollView will result in renderflex overflow error并且删除SingleChildScrollView会导致renderflex 溢出错误

The Expanded keyword will throw incorrect use of ParentDataWidget Expanded关键字将引发incorrect use of ParentDataWidget

Also, you have added SingleChildScrollView widget inside the column you have to swap these well此外,您已经在列中添加了 SingleChildScrollView 小部件,您必须很好地交换这些小部件

Here is an example that I have given which will help you achieve it.这是我给出的一个示例,它将帮助您实现它。

  Widget build(BuildContext context) {
    return Scaffold(
        body: Container(
    // Add The Container gradient here
      width: double.infinity,
      height: MediaQuery.of(context).size.height,
      child: SingleChildScrollView(
          child: Column(
            children: [
              Container(
                height: 100,
                width: 50,
                color: Colors.red,
              ),
              Container(
                height: 100,
                width: 50,
                color: Colors.blue,
              ),
              Container(
                height: 1000,
                width: 50,
                color: Colors.yellow,
              ),
              Container(
                height: 1000,
                width: 50,
                color: Colors.orange,
              ),
            ],
          )),
    ));
  }

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

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