简体   繁体   English

仅当专注于颤动时如何在 TextFormField() 上显示计数器文本?

[英]How to display counter text on TextFormField() only when focused in flutter?

I have the TextFormField() Widget, I want to display the CounterText but only when the field is focused我有TextFormField()小部件,我想显示 CounterText 但仅当该字段被聚焦时

and one more side question How to change the field background color when on focus?还有一个附带问题如何在聚焦时更改字段背景颜色?

在此处输入图片说明

code:-代码:-

TextFormField(
  decoration: InputDecoration(
    labelText: "Weight",
    border: OutlineInputBorder(
    borderSide: BorderSide(
    color:Colors.grey[200],
    width: 1,
    style: BorderStyle.solid
  ),
  gapPadding: 4.0
 ),
  counterText:"Enter Your Weight",
),
),

I think the easiest way is to create a list of Boolean and update them on that so you know when the TextForm is selected and then display counterText我认为最简单的方法是创建一个布尔列表并更新它们,以便您知道何时选择了 TextForm,然后显示 counterText

Column(
      children: <Widget>[
        SizedBox(height: 50),
        TextFormField(
          onTap: () {
            setState(() {
              selected[0] = true;
              selected[1] = false;
            });
          },
          controller: textController,
          decoration: InputDecoration(
            labelText: "Weight",
            border: OutlineInputBorder(
                borderSide: BorderSide(
                    color: Colors.grey[200],
                    width: 1,
                    style: BorderStyle.solid),
                gapPadding: 4.0),
            counterText: selected[0] ? "Enter Your Weight" : ' ',
          ),
        ),
        TextFormField(
          onTap: () {
            setState(() {
              selected[0] = false;
              selected[1] = true;
            });
          },
          decoration: InputDecoration(
            labelText: "Age",
            border: OutlineInputBorder(
                borderSide: BorderSide(
                    color: Colors.grey[200],
                    width: 1,
                    style: BorderStyle.solid),
                gapPadding: 4.0),
            counterText: selected[1] ? "Enter Your Age" : ' ',
          ),
        )
      ],
    )

在此处输入图片说明

暂无
暂无

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

相关问题 如何在 Flutter 中对齐 TextFormField 旁边的文本? - How to align Text next to TextFormField in Flutter? Flutter:向 Column 添加了一个额外的 TextFormField 但显示没有变化 - Flutter: Added an additional TextFormField to Column but no change in display Flutter&Dart,如何在扫描和更新文本时将条形码扫描值传递到TextFormField中 - Flutter & Dart, How to Pass Barcode Scan value into TextFormField on Scan and Update Text 如何更改 TextFormField() 小部件颜色 Flutter - How to change TextFormField() widget color Flutter 当我将输入值强制输入到 flutter 中的该字段时,如何避免滚动到 TEXTFORMFIELD - how to avoid scrolling to the TEXTFORMFIELD when I force input value to that field in flutter 根据表单中 TextFormField() 中的用户输入更新 Text(),执行计算并在另一个 TextFormField() 中显示结果 - Update Text() based on user input in a TextFormField() within a form, perform a calculation and display results in another TextFormField() Flutter 中不推荐使用 TextFormField 的自动验证 - Autovalidate of TextFormField is deprecated in Flutter 在 Flutter 中,如何更改 TextFormField 验证错误行样式? - In Flutter, How can I change TextFormField validation error line style? Flutter Textformfield 验证器将最后一个 TextFormfield 集中在验证错误上,而不是第一个 - Flutter Textformfield validator Focuses Last TextFormfield on validation error instead of first 选中复选框时如何更改文本显示 - How to change text display when checkbox checked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM