【问题标题】:how to change the width of textfield dynamically while inputting value如何在输入值时动态更改文本字段的宽度
【发布时间】:2019-12-12 04:53:05
【问题描述】:

我想在输入值时动态更改文本字段的宽度以适应所有输入文本。 这是我尝试过的。

var valueIncreaser = 30;
var textfieldSpanValue = 50.0;

Container(
  width: textfieldSpanValue,
  child: TextFormField(              
              onChanged: (text) {
                setState(() {
                  textfieldSpanValue = 20;
                  textfieldSpanValue = valueIncreaser + textfieldSpanValue * textfieldSpanValue;
                });
              },
              controller: myController,
              maxLength: 10,
              maxLengthEnforced: true,
              keyboardType: TextInputType.number,
              inputFormatters: <TextInputFormatter>[
                WhitelistingTextInputFormatter.digitsOnly
              ],
              ),
)

我想要这样的东西。

【问题讨论】:

    标签: android flutter textfield


    【解决方案1】:

    您的代码中似乎有一些简单的逻辑错误。您需要做的就是将宽度增量逻辑设置为:

    textfieldSpanValue = valueIncreaser + textfieldSpanValue * text.length;

    var valueIncreaser = 30;
    var textfieldSpanValue = 50.0;
    
    Container(
      width: textfieldSpanValue,
      child: TextFormField(              
                  onChanged: (text) {
                    setState(() {
                       textfieldSpanValue = 20;
                      textfieldSpanValue =
                          valueIncreaser + textfieldSpanValue * text.length;
                    });
                  },
                  controller: myController,
                  maxLength: 10,
                  maxLengthEnforced: true,
                  keyboardType: TextInputType.number,
                  inputFormatters: <TextInputFormatter>[
                    WhitelistingTextInputFormatter.digitsOnly
                  ],
                  ),
    )
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-09
      • 2017-05-10
      • 2014-07-12
      • 1970-01-01
      • 2012-12-14
      • 1970-01-01
      • 1970-01-01
      • 2023-02-10
      相关资源
      最近更新 更多