【问题标题】:Flutter TextField with maxLength and使用 maxLength 和 Flutter TextField
【发布时间】:2020-05-20 10:16:05
【问题描述】:

我需要一个强制 maxLength 为 250 个字符且其标签与提示文本对齐的文本区域。我的代码如下。

但是,小部件将文本呈现在提示之上。有谁知道解决方案? 就像计数器小部件强制实际文本字段向上移动一样。

TextField with maxLength = 250 and alignLabelWithHint = true

TextField(
    expands: false,
    minLines: null,
    maxLines: null,
    maxLengthEnforced: maxLengthEnforced,
    maxLength: maxLength,
    controller: controller,
    onChanged: (String value){
      print(value);
    },
    cursorColor: Colors.deepOrange,
    keyboardType: keyboardType,
    decoration: InputDecoration(

        alignLabelWithHint: true,
        labelText: text,
        labelStyle: TextStyle(color: Colors.grey),
        hintText: text,
        prefixIcon: Container(
          child: Column(
            children: <Widget>[
              Container(
                margin: EdgeInsets.only(top: 15.0),
                child: Material(
                  elevation: 0,
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(30)),
                  child: Icon(
                    icon,
                    color: Colors.red,
                  ),
                ),
              ),
            ],
          ),
        ),
        border: InputBorder.none,
        contentPadding: EdgeInsets.only(right: 10, top: 5, bottom: 5)),
)

【问题讨论】:

    标签: flutter textfield maxlength


    【解决方案1】:

    提示的目的是在用户开始输入时消失。我尝试了您的代码并且没有问题。

    TextField(
                      expands: false,
                      minLines: null,
                      maxLines: null,
                      maxLengthEnforced: true,
                      maxLength: 250,
                      // controller: controller,
                      onChanged: (String value) {
                        print(value);
                      },
                      cursorColor: Colors.deepOrange,
                      // keyboardType: keyboardType,
                      decoration: InputDecoration(
                          alignLabelWithHint: true,
                          labelText: "hi",
                          labelStyle: TextStyle(color: Colors.grey),
                          hintText: "hi",
                          prefixIcon: Container(
                            child: Column(
                              children: <Widget>[
                                Container(
                                  margin: EdgeInsets.only(top: 15.0),
                                  child: Material(
                                    elevation: 0,
                                    color: Colors.white,
                                    borderRadius:
                                        BorderRadius.all(Radius.circular(30)),
                                    child: Icon(
                                      Icons.highlight,
                                      color: Colors.red,
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ),
                          border: InputBorder.none,
                          contentPadding:
                              EdgeInsets.only(right: 10, top: 5, bottom: 5)),
                    ),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-12
      • 2021-10-07
      • 2021-10-14
      • 2021-04-01
      • 2021-10-06
      • 1970-01-01
      • 2018-07-27
      • 1970-01-01
      相关资源
      最近更新 更多