henryleunghk / flutter-native-text-input

Native text input for Flutter
MIT License
66 stars 50 forks source link

[BUG] Placer Holder value submitted #15

Closed Nico3652 closed 2 years ago

Nico3652 commented 2 years ago

First problem, when I'm closing the keyboard by touching outside the onSubmitted callback is fired and don't know why.

Second, when the field is empty, my place holder value is submitted :

placeholder: 'message',
onSubmitted: (String? value) {
  print("submitted value : $value");
},

Result

flutter: submitted value : message

Third, when I'm submitted the textfield config is not reset : ex : If I wrote 3 lines and submit the place holder is not printed again and the 3 lines config is remaining :

Capture d’écran 2021-12-03 à 14 44 44

Thanks in advance

henryleunghk commented 2 years ago

The first two will be fixed.

The third should be expected behaviour. You may reset the content using TextEditingController when onSubmitted is triggered.

Nico3652 commented 2 years ago

Thanks for improvement. Indeed for the controller, but it was already what I used :

NativeTextInput(
      returnKeyType: ReturnKeyType.send,
      cursorColor: ThemeProvider.colorApp1,
      textCapitalization: TextCapitalization.sentences,
      style: Theme.of(context).textTheme.bodyText1,
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(chatHeight / 2),
          border: Border.all(color: chatColor, width: 0.8)),
      placeholderStyle: ThemeProvider.instance.getPlacerHolderStyle(),
      placeholder: 'message',
      onSubmitted: (String? value) {
        print("submitted value : $value");
        if (value != null && value.trim().isNotEmpty) {
          sendNewMessage(value.trim());
          messagesViewModel.saveTyping(typing: false);
        }
      },
      keyboardAppearance: Theme.of(context).brightness,
      focusNode: focusNode,
      minLines: 1,
      maxLines: 3,
      onChanged: (value) {
        if (value.isNotEmpty) {
          messagesViewModel.saveTyping(typing: true);
        } else if (value.isEmpty) {
          messagesViewModel.saveTyping(typing: false);
        }
      },
      controller: textEditingController,
    );

And within the sendMessage() :

void sendNewMessage(String content) {
    textEditingController.clear();
    print('text controller clear the field ');
    emptyChat.value = true;
    messagesViewModel.sendTextMessage(content);
    messagesViewModel.saveTyping(typing: false);
    //FocusScope.of(context).requestFocus(focusNode);
    focusNode.requestFocus();
  }

The textfield lines (in the picture above) is still not reset after the textEditingController.clear() whereas the placeholder is reset

henryleunghk commented 2 years ago

Try this:

NativeTextInput(
  controller: _changeTextController,
  focusNode: _focusNode,
  placeholder: 'placeholder',
  onSubmitted: _onSubmittedText,
)
_onSubmittedText(value) {
  _changeTextController.clear();
  FocusScope.of(context).requestFocus(_focusNode);
}

This is working fine to me:

https://user-images.githubusercontent.com/28475298/144737310-f620e552-d811-45f5-b002-2bc292ad912f.mov