Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions lib/src/annotation_editing_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ part of flutter_mentions;
/// A custom implementation of [TextEditingController] to support @ mention or other
/// trigger based mentions.
class AnnotationEditingController extends TextEditingController {
Map<String, Annotation> _mapping;
Map<String, Annotation> _mapping = {};
String? _pattern;

// Generate the Regex pattern for matching all the suggestions in one.
AnnotationEditingController(this._mapping)
: _pattern = _mapping.keys.isNotEmpty
? "(${_mapping.keys.map((key) => RegExp.escape(key)).join('|')})"
: null;
void initialise(Map<String, Annotation> mapping) {
// Generate the Regex pattern for matching all the suggestions in one.
_mapping = mapping;
if (_mapping.keys.isNotEmpty) {
var result = _mapping.keys.map((key) => RegExp.escape(key)).toList();
result.sort((b, a) => a.toLowerCase().compareTo(b.toLowerCase()));
var finalresult = result.join('|');
_pattern = '($finalresult)(?![A-Za-z0-9_])';
}
}

/// Can be used to get the markup from the controller directly.
String get markupText {
Expand Down Expand Up @@ -51,11 +56,15 @@ class AnnotationEditingController extends TextEditingController {
set mapping(Map<String, Annotation> _mapping) {
this._mapping = _mapping;

_pattern = "(${_mapping.keys.map((key) => RegExp.escape(key)).join('|')})";
var result = _mapping.keys.map((key) => RegExp.escape(key)).toList();
result.sort((b, a) => a.toLowerCase().compareTo(b.toLowerCase()));
var finalresult = result.join('|');
_pattern = '($finalresult)(?![A-Za-z0-9_])';
}

@override
TextSpan buildTextSpan({BuildContext? context, TextStyle? style, bool? withComposing}) {
TextSpan buildTextSpan(
{BuildContext? context, TextStyle? style, bool? withComposing}) {
var children = <InlineSpan>[];

if (_pattern == null || _pattern == '()') {
Expand Down
47 changes: 35 additions & 12 deletions lib/src/mention_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class FlutterMentions extends StatefulWidget {
this.appendSpaceOnAdd = true,
this.hideSuggestionList = false,
this.onSuggestionVisibleChanged,
this.textController,
}) : super(key: key);

final bool hideSuggestionList;
Expand Down Expand Up @@ -241,6 +242,9 @@ class FlutterMentions extends StatefulWidget {
/// {@macro flutter.services.autofill.autofillHints}
final Iterable<String>? autofillHints;

/// Alternative for default text controller
final AnnotationEditingController? textController;

@override
FlutterMentionsState createState() => FlutterMentionsState();
}
Expand Down Expand Up @@ -299,8 +303,7 @@ class FlutterMentionsState extends State<FlutterMentions> {
_selectedMention = null;
});

final _list = widget.mentions
.firstWhere((element) => selectedMention.str.contains(element.trigger));
final _list = _getSelectedMentionFromList();

// find the text by range and replace with the new value.
controller!.text = controller!.value.text.replaceRange(
Expand Down Expand Up @@ -336,10 +339,18 @@ class FlutterMentionsState extends State<FlutterMentions> {
});

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

return element.end == cursorPos &&
_pattern = widget.mentions.map((e) {
if (e.trigger.contains(r'[')) {
return '\\${e.trigger}';
}
return e.trigger;
}).join('|');

var match = false;
match = element.end == cursorPos &&
element.str.toLowerCase().contains(RegExp(_pattern));

return match;
});

showSuggestions.value = val != -1;
Expand Down Expand Up @@ -373,8 +384,12 @@ class FlutterMentionsState extends State<FlutterMentions> {
@override
void initState() {
final data = mapToAnotation();

controller = AnnotationEditingController(data);
if (widget.textController != null) {
controller = widget.textController;
} else {
controller ??= AnnotationEditingController();
}
controller!.initialise(data);

if (widget.defaultText != null) {
controller!.text = widget.defaultText!;
Expand All @@ -392,7 +407,6 @@ class FlutterMentionsState extends State<FlutterMentions> {
void dispose() {
controller!.removeListener(suggestionListerner);
controller!.removeListener(inputListeners);

super.dispose();
}

Expand All @@ -406,10 +420,7 @@ class FlutterMentionsState extends State<FlutterMentions> {
@override
Widget build(BuildContext context) {
// Filter the list based on the selection
final list = _selectedMention != null
? widget.mentions.firstWhere(
(element) => _selectedMention!.str.contains(element.trigger))
: widget.mentions[0];
final list = _getSelectedMentionFromList();

return Container(
child: PortalEntry(
Expand Down Expand Up @@ -489,4 +500,16 @@ class FlutterMentionsState extends State<FlutterMentions> {
),
);
}

Mention _getSelectedMentionFromList() {
return _selectedMention != null
? widget.mentions.firstWhere((element) {
var trigger = element.trigger;
if (trigger.contains('\\')) {
trigger = trigger.substring(1);
}
return _selectedMention!.str.contains(trigger);
})
: widget.mentions[0];
}
}
8 changes: 4 additions & 4 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.6.1"
version: "2.8.1"
boolean_selector:
dependency: transitive
description:
Expand All @@ -28,7 +28,7 @@ packages:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.1"
clock:
dependency: transitive
description:
Expand Down Expand Up @@ -80,7 +80,7 @@ packages:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.7.0"
path:
dependency: transitive
description:
Expand Down Expand Up @@ -134,7 +134,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.0"
version: "0.4.2"
typed_data:
dependency: transitive
description:
Expand Down