简体   繁体   English

SingleChildScrollView,子列上的空格问题

[英]SingleChildScrollView , Problem with spaces on the child Column

I had a BOTTOM OVERFLOWED BY ** PIXELS when i tried to open the phone keyboard so i wrapped my Column in a SingleChildScrollView But somehow it became pushed to the top and the MainAxisAlignment.spaceEvenly stopped working so i made some space between them using SizedBox my question is: is that the good way to do it?当我尝试打开手机键盘时,我的** PIXELS 底部溢出,所以我将我的 Column 包裹在SingleChildScrollView但不知何故它被推到顶部并且MainAxisAlignment.spaceEvenly停止工作,所以我使用SizedBox在它们之间留出了一些空间问题是:这是做到这一点的好方法吗? to achieve space between the widgets and is it gonna work on different phone sizes实现小部件之间的空间,它是否适用于不同的手机尺寸

my code:我的代码: 在此处输入图像描述

second question: how can i push all the column to the top until last item is showing without scrolling when i open the keyboard like the picture below第二个问题:当我打开键盘时,如何将所有列推到顶部直到最后一项显示而不滚动,如下图所示

在此处输入图像描述

  1. To make the MainAxisAlignment.spaceEvenly work again, you need to wrap your inside a ConstrainedBox to be like this:要使MainAxisAlignment.spaceEvenly再次工作,您需要将您的内部包装为ConstrainedBox ,如下所示:
SingleChildScrollView(
        child: ConstrainedBox(
          constraints: BoxConstraints(
            maxHeight: MediaQuery.of(context).size.height,
          ),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              Text('data'),
              Text('HelloWorld'),
            ],
          ),
        ),
      ),
  1. To scroll to the end of your form, use a ScrollController for the SingleChildScrollView and animate to the end of the list like this:要滚动到表单的末尾,请为SingleChildScrollView使用ScrollController并像这样动画到列表的末尾:
   controller.animateTo(
      controller.position.maxScrollExtent,
      duration: const Duration(milliseconds: 500),
      curve: Curves.easeOut,
    );
  1. It's okay to use SizedBox for spaces, A small Tip: remember to set const before it to make your app more performant, cuz this way flutter won't rebuild the sizedBox Widget every time your widget is rebuilt可以将SizedBox用于空格,一个小提示:记得在它之前设置const以使您的应用程序性能更高,因为这样 flutter 不会在每次重建小部件时重建 sizedBox 小部件

Hope you find this helpful =D希望你觉得这对你有帮助 =D

Normally you should be using a ListView , that should solve the problem of your keyboard hidding your form fields.通常您应该使用ListView ,这应该可以解决键盘隐藏表单字段的问题。

About the SizedBox and the issues with the distribution of you widget, that is because normally when you create a column, it uses the entire hight of the parent, but in the case of the SingleChildScrollView , it does not have a size to give you, hence the column doesnt know how to distribute your children.关于SizedBox和你的小部件分布的问题,那是因为通常当你创建一个列时,它使用父级的整个高度,但在SingleChildScrollView的情况下,它没有给你一个大小,因此专栏不知道如何分配您的孩子。

Finally, I've seen a lot of developers use the SizedBox to create spacing, so I dont think there is nothing wrong with using it.最后,我看到很多开发者使用SizedBox来创建间距,所以我认为使用它并没有什么问题。 You could also create a ListView.separated ( official docs ) which will add a the same Widget (that you provide) as a separator.您还可以创建一个ListView.separated官方文档),它将添加一个相同的 Widget(您提供的)作为分隔符。

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

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