flutter_chips_input icon indicating copy to clipboard operation
flutter_chips_input copied to clipboard

Fix backspace bug for android devices

Open jorelkimcruz opened this issue 3 years ago • 9 comments

  • Wrapped notificationListener with RawKeyboardListener to fix bug for android devices

jorelkimcruz avatar Aug 19 '21 09:08 jorelkimcruz

also in my case I also have to revert to


  void _updateTextInputState({bool replaceText = false, String putText = ''}) {
    if (replaceText || putText != '') {
      final updatedText =
          String.fromCharCodes(_chips.map((_) => kObjectReplacementChar)) +
              (replaceText ? '' : _value.normalCharactersText) +
              putText;
      setState(() => _value = _value.copyWith(
            text: updatedText,
            selection: TextSelection.collapsed(offset: updatedText.length),
            //composing: TextRange(start: 0, end: text.length),
            composing: TextRange.empty,
          ));
    }
    _closeInputConnectionIfNeeded(); //Hack for #34 (https://github.com/danvick/flutter_chips_input/issues/34#issuecomment-684505282). TODO: Find permanent fix
    _textInputConnection ??= TextInput.attach(this, textInputConfiguration);
    _textInputConnection?.setEditingState(_value);
  }

but this is not in the PR.

jorelkimcruz avatar Aug 19 '21 09:08 jorelkimcruz

@jorelkimcruz Hey, I have tested your changes but found some bug while testing on 'Samsung Phone'. These changes have fixed errors in the case of the alphabet but in the case of numbers, the problem persists.

Updated PR

this is what causing the issue.

 composing: (Platform.isIOS || replacedLength == textLength)
            ? TextRange.empty
            : TextRange(
                start: replacedLength,
                end: textLength,
              ),
  void _updateTextInputState({bool replaceText = false, String putText = ''}) {
    if (replaceText || putText != '') {
      final updatedText =
          String.fromCharCodes(_chips.map((_) => kObjectReplacementChar)) +
              (replaceText ? '' : _value.normalCharactersText) +
              putText;
      setState(() => _value = _value.copyWith(
            text: updatedText,
            selection: TextSelection.collapsed(offset: updatedText.length),
            //composing: TextRange(start: 0, end: text.length),
            composing: TextRange.empty,
          ));
    }
    _closeInputConnectionIfNeeded(); //Hack for #34 (https://github.com/danvick/flutter_chips_input/issues/34#issuecomment-684505282). TODO: Find permanent fix
    _textInputConnection ??= TextInput.attach(this, textInputConfiguration);
    _textInputConnection?.setEditingState(_value);
  }

jorelkimcruz avatar Aug 20 '21 03:08 jorelkimcruz

@jorelkimcruz Hey, the above commits have fixed the issue but the cursor is not visible. can you fix this?

aman-singh7 avatar Aug 20 '21 17:08 aman-singh7

@jorelkimcruz Hey, the above commits have fixed the issue but the cursor is not visible. can you fix this?

have you tried changing the cursors color?

jorelkimcruz avatar Aug 21 '21 11:08 jorelkimcruz

@jorelkimcruz Hey, the above commits have fixed the issue but the cursor is not visible. can you fix this?

have you tried changing the cursors color?

Is there a way to change the cursor color? I am not able to find any parameter that does this.

aman-singh7 avatar Aug 21 '21 11:08 aman-singh7

@jorelkimcruz Hey, the above commits have fixed the issue but the cursor is not visible. can you fix this?

have you tried changing the cursors color?

Is there a way to change the cursor color? I am not able to find any parameter that does this.

inside text_cursor.dart

jorelkimcruz avatar Aug 21 '21 11:08 jorelkimcruz

@jorelkimcruz, have you tried this on the iOS simulator? I just tried out your branch (per https://github.com/danvick/flutter_chips_input/issues/97#issuecomment-927638264), and it seems to fix the focus issue, but it has a really strange issue of character repeating. When you type (with a hardware keyboard) into the input in the iOS simulator, sometimes a key that you hit once may be repeated multiple times. I've noticed this both for entering new letters as well as backspacing (where sometimes a single backspace will remove multiple characters).

brianlenz avatar Oct 11 '21 19:10 brianlenz

@jorelkimcruz, have you tried this on the iOS simulator? I just tried out your branch (per #97 (comment)), and it seems to fix the focus issue, but it has a really strange issue of character repeating. When you type (with a hardware keyboard) into the input in the iOS simulator, sometimes a key that you hit once may be repeated multiple times. I've noticed this both for entering new letters as well as backspacing (where sometimes a single backspace will remove multiple characters).

Hi, Sorry been busy and didnt got the time to push a fix. but if you need this asap here is a fix. you might want to update

return RawKeyboardListener(
      focusNode: _focusNode, // or FocusNode()
      onKey: (event) {
        final str = currentTextEditingValue.text;
        if (event.runtimeType.toString() == 'RawKeyDownEvent' &&
            event.logicalKey == LogicalKeyboardKey.backspace &&
            str.isNotEmpty) {
          final sd = str.substring(0, str.length);
          updateEditingValue(TextEditingValue(
              text: sd, selection: TextSelection.collapsed(offset: sd.length)));
        }
      },
      child: NotificationListener<SizeChangedLayoutNotification>(
        onNotification: (SizeChangedLayoutNotification val) {
          WidgetsBinding.instance?.addPostFrameCallback((_) async {
            _suggestionsBoxController.overlayEntry?.markNeedsBuild();
          });
          return true;
        },
        child: SizeChangedLayoutNotifier(
          child: Column(
            children: <Widget>[
              GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: () {
                  requestKeyboard();
                },
                child: InputDecorator(
                  decoration: widget.decoration,
                  isFocused: _focusNode.hasFocus,
                  isEmpty: _value.text.isEmpty && _chips.isEmpty,
                  child: Wrap(
                    crossAxisAlignment: WrapCrossAlignment.center,
                    spacing: 4.0,
                    runSpacing: 4.0,
                    children: chipsChildren,
                  ),
                ),
              ),
              CompositedTransformTarget(
                link: _layerLink,
                child: Container(),
              ),
            ],
          ),
        ),
      ),
    );

jorelkimcruz avatar Oct 13 '21 05:10 jorelkimcruz

@jorelkimcruz, we're not actively using your branch, so no rush on a fix. The main issue we were trying to work around is with the iOS keyboard disappearing, which I have submitted #99 for.

I'll be happy to test out your change again once you push the change onto your branch, too 👍

brianlenz avatar Oct 13 '21 17:10 brianlenz