fayeed / flutter_mentions

A simple flutter input widget to add @ mentions functionality to your app
MIT License
107 stars 122 forks source link

Mention list is not showing on @ at 0th position of the textfield #9

Closed krishnakumarcn closed 4 years ago

krishnakumarcn commented 4 years ago

One possible fix I could find is that Im showing below:

I don't know if this will break your logic somewhere else. Could you please look into it?

controller.addListener(() {
      final cursorPos = controller.selection.baseOffset;

      if (cursorPos - 1 > 0) {
        var _pos = 0;

        final lengthMap = <LengthMap>[];

        // split on each word and generate a list with start & end position of each word.
        controller.value.text.split(RegExp(r'(\s)')).forEach((element) {
          lengthMap.add(
              LengthMap(str: element, start: _pos, end: _pos + element.length));

          _pos = _pos + element.length + 1;
        });

        final val = lengthMap.indexWhere((element) {
          _pattern = widget.mentions.map((e) => e.trigger).join('|');

          return element.end == cursorPos &&
              element.str.toLowerCase().contains(RegExp(_pattern));
        });

        print("Val is: $val");
        setState(() {
          _showSuggestions = val != -1;
          _selectedMention = val == -1 ? null : lengthMap[val];
        });
      } else {

       /// Fix here.
        if (controller.text == "@") {

         /// its the same code inside the if but I only added this `@` check for checking if initial character is `@`

           var _pos = 0;

          final lengthMap = <LengthMap>[];

          // split on each word and generate a list with start & end position of each word.
          controller.value.text.split(RegExp(r'(\s)')).forEach((element) {
            lengthMap.add(LengthMap(
                str: element, start: _pos, end: _pos + element.length));

            _pos = _pos + element.length + 1;
          });

          final val = lengthMap.indexWhere((element) {
            _pattern = widget.mentions.map((e) => e.trigger).join('|');

            return element.end == cursorPos &&
                element.str.toLowerCase().contains(RegExp(_pattern));
          });

          print("Val is: $val");
          setState(() {
            _showSuggestions = val != -1;
            _selectedMention = val == -1 ? null : lengthMap[val];
          });
        }
      }
    });

@fayeed

fayeed commented 4 years ago

@krishnakumarcn Thanks for this will take a look a this later.

fayeed commented 4 years ago

@krishnakumarcn The issue this line: if (cursorPos - 1 > 0) I was checking up to position 1 don't know why I did that, just needed to change to if (cursorPos >= 0) {. Fix is in version 0.1.3.