当没有角色颤动时,Textformfield on Changed不会被调用

发布于 2025-02-09 21:01:18 字数 2955 浏览 4 评论 0 原文

我有1个功能,可以在按钮上添加textformfield,按下最多4个textformfield,如下图所示,当Textformfield中没有文本时,我想删除该TextFormField,因此我将该登录名放入Onchange中。

当我按按钮1S时间时,如果我从键盘上按键盘的删除按钮,则不会添加TextFormField,而不会键入任何字符。

请查看此视频:

这是我的代码。

TextFormField(
    controller: bullet2Controller,
    focusNode: focusNode2,
    maxLines: null,
    minLines: 1,
    textCapitalization:TextCapitalization.sentences,
                       cursorColor: Colors.black,
                       showCursor: true,
                       autofocus: true,
                       textAlign: TextAlign.start,
                       inputFormatters: [LengthLimitingTextInputFormatter(140),],
                       onChanged: (value) {
                           setState(() {
                               if (value.isEmpty) {
                                   isBullet2Visible = false;

                                   if (isBullet1Visible) {
                                       focusNode1.requestFocus();
                                   } else if (isBullet3Visible) {
                                       focusNode3.requestFocus();
                                   } else if (isBullet4Visible) {
                                       focusNode4.requestFocus();
                                   } else {
                                       FocusScope.of(context).unfocus();
                                   }

                                   if (_counter > 0) {
                                       _counter--;
                                   }
                               }

                               if (kDebugMode) {
                                   print("${value.length.toString()} character(s)");
                               }
                           });
                       },
                       decoration: const InputDecoration(disabledBorder:
                                                            InputBorder.none,
                                                        border:
                                                            InputBorder.none,
                                                        filled: true,
                                                        fillColor: Colors.white,
                                                      ),
                                                      keyboardType:
                                                          TextInputType
                                                              .multiline,
                                                      textInputAction:
                                                          TextInputAction.done,
                                                    ),

是默认行为还是我需要采取任何额外的步骤才能使其正常工作。

I have 1 functionality of adding TextFormField in Container on Button press upto 4 TextFormField like below image and when there's no text in TextFormField i want to remove that TextFormField so i have put that login in onChange.

When i press the button 1s time and it will add TextFormField and without typing any character if i press delete button from keyboard onChange is not getting called.

Please see this video: https://drive.google.com/file/d/1Yln48d5JHvvYdb4LRDXxmlzPzlC__xYq/view?usp=sharing

Here is my code.

TextFormField(
    controller: bullet2Controller,
    focusNode: focusNode2,
    maxLines: null,
    minLines: 1,
    textCapitalization:TextCapitalization.sentences,
                       cursorColor: Colors.black,
                       showCursor: true,
                       autofocus: true,
                       textAlign: TextAlign.start,
                       inputFormatters: [LengthLimitingTextInputFormatter(140),],
                       onChanged: (value) {
                           setState(() {
                               if (value.isEmpty) {
                                   isBullet2Visible = false;

                                   if (isBullet1Visible) {
                                       focusNode1.requestFocus();
                                   } else if (isBullet3Visible) {
                                       focusNode3.requestFocus();
                                   } else if (isBullet4Visible) {
                                       focusNode4.requestFocus();
                                   } else {
                                       FocusScope.of(context).unfocus();
                                   }

                                   if (_counter > 0) {
                                       _counter--;
                                   }
                               }

                               if (kDebugMode) {
                                   print("${value.length.toString()} character(s)");
                               }
                           });
                       },
                       decoration: const InputDecoration(disabledBorder:
                                                            InputBorder.none,
                                                        border:
                                                            InputBorder.none,
                                                        filled: true,
                                                        fillColor: Colors.white,
                                                      ),
                                                      keyboardType:
                                                          TextInputType
                                                              .multiline,
                                                      textInputAction:
                                                          TextInputAction.done,
                                                    ),

Is it default behaviour or do i need to do any extra step to make it work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

濫情▎り 2025-02-16 21:01:18

这是默认行为。
当您的值=''并按DELETE时,它仍然等于'',并且没有被调用。
为了实现目标,您应该使用RawKeyboardListener等听众

this is a default behaviour.
when your value = '' and you press delete it is still equal to '' and onChanged not getting called.
to achieve your goals you should use a listener like RawKeyboardListener

帥小哥 2025-02-16 21:01:18

感谢Vladyslav Ulianytskyi的建议。

我已经使用 rawKeyboardListner 来完成此操作。这是示例代码。

RawKeyboardListener(
    autofocus: true,
    onKey: (event) {
        setState(() {
            if (event.isKeyPressed(LogicalKeyboardKey.backspace)) {
                print("Perform Your Action");
            }
        });
    },
    focusNode: FocusNode(),
    child: TextFormField(controller: bullet2Controller,
        focusNode: focusNode2,
        maxLines: null,
        minLines: 1,
        textCapitalization: TextCapitalization.sentences,
            cursorColor: Colors.black,
            showCursor: true,
            autofocus: true,
            textAlign: TextAlign.start,
            inputFormatters: [LengthLimitingTextInputFormatter(140),
            ],
            decoration: const InputDecoration(
                disabledBorder: InputBorder.none,
                border: InputBorder.none,
                filled: true,
                fillColor: Colors.white,
            ),
            keyboardType: TextInputType.multiline,
            textInputAction: TextInputAction.done,
        ),
    ),
)

Thanks to Vladyslav Ulianytskyi suggestion.

I have done this with the use of RawKEyboardListner. Here is the sample code.

RawKeyboardListener(
    autofocus: true,
    onKey: (event) {
        setState(() {
            if (event.isKeyPressed(LogicalKeyboardKey.backspace)) {
                print("Perform Your Action");
            }
        });
    },
    focusNode: FocusNode(),
    child: TextFormField(controller: bullet2Controller,
        focusNode: focusNode2,
        maxLines: null,
        minLines: 1,
        textCapitalization: TextCapitalization.sentences,
            cursorColor: Colors.black,
            showCursor: true,
            autofocus: true,
            textAlign: TextAlign.start,
            inputFormatters: [LengthLimitingTextInputFormatter(140),
            ],
            decoration: const InputDecoration(
                disabledBorder: InputBorder.none,
                border: InputBorder.none,
                filled: true,
                fillColor: Colors.white,
            ),
            keyboardType: TextInputType.multiline,
            textInputAction: TextInputAction.done,
        ),
    ),
)
这样的小城市 2025-02-16 21:01:18

您可以尝试下面的代码,希望它对您有用,

TextEditingController bullet2Controller = TextEditingController();
int currentTextLength = 0;

TextFormField(
    maxLines: null,
    controller: bullet2Controller,
    decoration: InputDecoration(
      hintText: 'Type your observations'
    ),
    style: TextStyle(
      fontSize: 14,
      fontWeight: FontWeight.w300,
      fontFamily: 'Roboto'
    ),
    onChanged: (String newText){
      if (newText != ''){
        if(newText[0] != '•'){
          newText = '• ' + newText;
          bullet2Controller.text = newText;
         bullet2Controller.selection = TextSelection.fromPosition(TextPosition(offset: bullet2Controller.text.length));
        }
        if(newText[newText.length - 1] == '\n' && newText.length > currentTextLength){
           bullet2Controller.text = newText + '• ';
          bullet2Controller.selection = TextSelection.fromPosition(TextPosition(offset: bullet2Controller.text.length));
        }
        currentTextLength = bullet2Controller.text.length;
      }
  }
)

让我知道它是否适合您,或者让我知道是否有任何问题。

You can try it will below code hope it's work for you

TextEditingController bullet2Controller = TextEditingController();
int currentTextLength = 0;

TextFormField(
    maxLines: null,
    controller: bullet2Controller,
    decoration: InputDecoration(
      hintText: 'Type your observations'
    ),
    style: TextStyle(
      fontSize: 14,
      fontWeight: FontWeight.w300,
      fontFamily: 'Roboto'
    ),
    onChanged: (String newText){
      if (newText != ''){
        if(newText[0] != '•'){
          newText = '• ' + newText;
          bullet2Controller.text = newText;
         bullet2Controller.selection = TextSelection.fromPosition(TextPosition(offset: bullet2Controller.text.length));
        }
        if(newText[newText.length - 1] == '\n' && newText.length > currentTextLength){
           bullet2Controller.text = newText + '• ';
          bullet2Controller.selection = TextSelection.fromPosition(TextPosition(offset: bullet2Controller.text.length));
        }
        currentTextLength = bullet2Controller.text.length;
      }
  }
)

let me know if it's work for you or let me know if there any question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文