Flutter: 如何设置我的TextField的高度而不导致屏幕出现错误?

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

Flutter : How to set height for my TextField without make my screen bug?

问题

I want to add a textfield inside my row.....

我想在我的行内添加一个文本字段.....

Here, the display is good

在这里,显示效果很好

But here

但是在这里

The display is bugged just because I added a TextField, but.. why ?

显示效果出错仅仅是因为我添加了一个TextField,但是...为什么?

I want the textfield can be added without bug my display instead of having a white screen when I add my textfield.....

我希望可以添加文本字段而不会导致显示错误,而不是在添加文本字段时出现白屏.....

I really don't understand why

我真的不明白为什么

英文:

I want to add a textfield inside my row.....

Widget build(BuildContext context) {
    return 
      SingleChildScrollView(
        child: Column(
          children: [
            Row(
              children: [
                  Image.asset("assets/cross.png"),
                  Text("Nouveau contact"),
              ]
            ),
            Row(
              children: [
                Radio(value: "Homme", groupValue: gender, onChanged: (value) { setState(() { gender = value; }); }),
                Text("Homme"),
                Divider(indent: 10.0,),
                Radio(value: "Femme", groupValue: gender, onChanged: (value) { setState(() { gender = value; }); }),
                Text("Femme"),
              ],
            ),
            Row(
              children: [
                //NOTHING HERE
              ],
            ),
            Row(),
          ],
        ),
      );
  }

Here, the display is good

But here

Widget build(BuildContext context) {
    return 
      SingleChildScrollView(
        child: Column(
          children: [
            Row(
              children: [
                  Image.asset("assets/cross.png"),
                  Text("Nouveau contact"),
              ]
            ),
            Row(
              children: [
                Radio(value: "Homme", groupValue: gender, onChanged: (value) { setState(() { gender = value; }); }),
                Text("Homme"),
                Divider(indent: 10.0,),
                Radio(value: "Femme", groupValue: gender, onChanged: (value) { setState(() { gender = value; }); }),
                Text("Femme"),
              ],
            ),
            Row(
              children: [
                TextField(
                  maxLines: 1,
                  controller: TextEditingController(
                    text: firstname
                  ),
                  decoration: InputDecoration(
                    hintText: "Prénom"
                  ),
                ),
              ],
            ),
            Row(),
          ],
        ),
      );
  }

The display is bugged just because I added a TextField, but.. why ?

I want the textfield can be added without bug my display instead of having a white screen when I add my textfield.....

I really don't understand why

答案1

得分: 1

如果您查看控制台,您应该能够看到为什么出错。应该会显示类似以下内容:

"输入装饰器,通常由TextField创建,不能具有无界宽度。\n这是在父部件没有提供有限宽度约束时发生的。例如,如果InputDecorator包含在Row中,那么它的宽度必须受到约束。可以使用Expanded部件或SizedBox来约束InputDecorator或包含它的TextField的宽度。"

要解决这个问题,您可以按照建议的方式进行编写:

Row(
  children: [
    Expanded(
      child: TextField(
        maxLines: 1,
        controller: TextEditingController(
            text: firstname
        ),
        decoration: InputDecoration(
            hintText: "Prénom"
        ),
      ),
    ),
  ],
),

简化解释一下,Row 告诉其子部件:“尽情占用所有可用空间,没有限制”。而默认情况下,TextField 希望占用所有可用空间。这两者不兼容。Expanded 是一个部件,确保其子部件知道实际可用的空间有多大。

英文:

If you look in your console you should be able to see why it's wrong. It should say something like

> "An InputDecorator, which is typically created by a TextField, cannot
> have an unbounded width.\nThis happens when the parent widget does not
> provide a finite width constraint. For example, if the InputDecorator
> is contained by a Row, then its width must be constrained. An Expanded
> widget or a SizedBox can be used to constrain the width of the
> InputDecorator or the TextField that contains it."

So to solve it you can do what is suggested by writing

Row(
  children: [
    Expanded(
      child: TextField(
        maxLines: 1,
        controller: TextEditingController(
            text: firstname
        ),
        decoration: InputDecoration(
            hintText: "Prénom"
        ),
      ),
    ),
  ],
),

To try to explain it a bit simplified. A Row tells their children: "Take all the space you want, there is no limit". And a TextField by default wants all available space. This doesn't work together. An Expanded is a widget that makes sure that its child knows how much space there really is.

huangapple
  • 本文由 发表于 2023年2月27日 16:40:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75578317.html
匿名

发表评论

匿名网友

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

确定