dart 如何在flutter中隐藏填充输入中的标签?

uidvcgyl  于 5个月前  发布在  Flutter
关注(0)|答案(1)|浏览(61)

2个问题:首先)我有一个BMI计算器在这里.所以当用户想要输入她的数据,如身高和体重=>当输入被填充的文本字段小部件仍然显示标签,我不想.我真的不知道该怎么办??你能帮我吗?
和第二)我想这是未填充的textfield的标签还没有被显示在它自己的空间的中心。但如果你仔细看看图片你看到的“重量”标签是在textfield的开始,我不希望这样。我想在textfield的中心。他的数据输入的文本是在文本字段的中心。像这里我输入我的身高178 =>和这个“178”是不是在文本字段的中心!请帮助我的家伙!
所以在这里,例如,我希望当我输入我的高度,高度标签去..
谢谢你帮我解答两个问题x1c 0d1x
下面是我的代码:

TextField(
                    controller: height_controller,
                    keyboardType: const TextInputType.numberWithOptions(),
                    decoration: const InputDecoration(
                      floatingLabelAlignment: FloatingLabelAlignment.center,
                      label: Text(
                        'Height',
                        style: TextStyle(
                            color: Colors.white70,
                            fontSize: 30,
                            fontWeight: FontWeight.w300),
                        textAlign: TextAlign.center,
                      ),
                    ),
                  ),

字符串

uemypmqf

uemypmqf1#

对于您的第一个问题,如果您希望标签在有一些输入时不可见,那么您需要添加一个条件,该条件将在onValueChanged方法被触发时更改。

var visible by remember { mutableStateOf(true) }
    TextField(
        value = text,
        onValueChange = {
            text = it
            visible = text.isEmpty()
        },
        label = {
            if (visible) {
                Text("Label",
                    style = TextStyle(
                        textAlign = LocalTextStyle.current.copy(textAlign = TextAlign.Center).textAlign))
            }
                },
    )

字符串
对于您的第二个问题,要对齐TextField标签内的文本,您可以使用LocalTextStyle.current,因为它默认使用它。

TextField(
                    controller: height_controller,
                    keyboardType: const TextInputType.numberWithOptions(),
                    decoration: const InputDecoration(
                      floatingLabelAlignment: FloatingLabelAlignment.center,
                      label: Text(
                        'Height',
                        style: TextStyle(
                            color: Colors.white70,
                            fontSize: 30,
                            fontWeight: FontWeight.w300),
                            textAlign = LocalTextStyle.current.copy(textAlign = TextAlign.Center).textAlign)),  // <--------
                      ),
                    ),
                  ),

相关问题