【问题标题】:Flutter SingleChildScrollView with ExpandedFlutter SingleChildScrollView with Expanded
【发布时间】:2021-09-03 00:08:55
【问题描述】:

你们如何解决下面的 Flutter 布局?

我有一个屏幕,我必须在其中显示,如图所示:一个徽标 + 3 个 TextFormFields + 2 个按钮 + 一个容器。

问题:

  1. 我需要将所有小部件放在Column 中,并将列放在SingleChildScrollView 中,这样当键盘弹出时,它不会覆盖TextFields。
  2. 最后一个小部件Container 将占用底部的所有剩余屏幕空间,但不会超过屏幕大小。为此,我的想法是使用 Expanded 小部件,以便 Container 可以扩展到屏幕底部,但这会报错:

The following assertion was thrown during performLayout(): RenderFlex children have non-zero flex but incoming height constraints are unbounded.

所以我想我的问题,简而言之,我如何防止键盘覆盖 TextFields,同时我强制容器占用底部的所有剩余空间。

这是我的尝试:

SingleChildScrollView(
        child: Column(
      children: [
        Image.asset("assets/images/appLogo.png"),
        TextFormField(),
        TextFormField(),
        TextFormField(),
        Row(children: [TextButton(), TextButton()]),
        Expanded(child: Container())
      ],
    ));

【问题讨论】:

标签: flutter flutter-renderflex-error


【解决方案1】:

Expanded 不知道要带多大尺寸。其他孩子也不知道他们的确切大小。

将您的图像包装在容器中并为其指定高度和宽度。还可以尝试将所有文​​本字段分别包装在一个列或容器中。

SingleChildScrollView(
    child: Column(
  children: [
    Container(
    width: MediaQuery.of(context).Size.width * 0.4,
    height: MediaQuery.of(context).Size.height * 0.2,
    child: Image.asset("assets/images/appLogo.png"),
    ),
    Column(
    children: [ 
    TextFormField(),
    TextFormField(),
    TextFormField(),]
    )
    Row(children: [TextButton(), TextButton()]),
    Expanded(child: Container())
  ],
));

我希望这对你有用。

【讨论】:

  • 感谢您的回答,我会试一试并再次评论
猜你喜欢
  • 2019-10-13
  • 1970-01-01
  • 1970-01-01
  • 2021-01-18
  • 1970-01-01
  • 2020-11-05
  • 2020-02-23
  • 2021-08-29
  • 1970-01-01
相关资源
最近更新 更多