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

问题描述 投票:0回答:1

2 个问题:首先)我这里有一个 BMI 计算器。因此,当用户想要输入身高和体重等数据时=>当输入被填充时,文本字段小部件仍然显示标签,但我不希望这样。我真的不知道该怎么办? 你能帮我吗???

第二)我希望未填充的文本字段标签显示在它自己的空间的中心。但如果您仔细查看图片,您会发现“重量”标签位于文本字段的开头,而我不希望这样。我想位于文本字段的中心。 我还希望当用户输入她/他的数据时,输入的文本位于文本字段的中心。就像这里我输入我的身高 178 => 并且这个“178”不在文本字段的中心! 请帮助我!

例如,我希望当我输入我的身高时,高度标签会出现...并且不应该再出现...但不幸的是:( .

感谢您帮助我解决我的两个问题。

这是我的代码:

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,
                      ),
                    ),
                  ),
android flutter dart user-interface textfield
1个回答
0
投票

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

I.E.

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)),  // <--------
                      ),
                    ),
                  ),
© www.soinside.com 2019 - 2024. All rights reserved.