Removed the inline span builder code from messageinput

This commit is contained in:
Ayush Shekhar
2022-04-06 18:26:09 +05:30
parent e93f6da232
commit 92b8711ccf
2 changed files with 24 additions and 21 deletions
@@ -19,37 +19,37 @@ class MessageInputController extends ValueNotifier<Message> {
/// message.
factory MessageInputController({
Message? message,
Map<RegExp, InlineSpanBuilder>? textPatternSpan,
Map<RegExp, TextStyleBuilder>? textPatternStyle,
}) =>
MessageInputController._(
initialMessage: message ?? Message(),
textPatternSpan: textPatternSpan,
textPatternStyle: textPatternStyle,
);
/// Creates a controller for an editable text field from an initial [text].
factory MessageInputController.fromText(
String? text, {
Map<RegExp, InlineSpanBuilder>? textPatternSpan,
Map<RegExp, TextStyleBuilder>? textPatternStyle,
}) =>
MessageInputController._(
initialMessage: Message(text: text),
textPatternSpan: textPatternSpan,
textPatternStyle: textPatternStyle,
);
/// Creates a controller for an editable text field from initial
/// [attachments].
factory MessageInputController.fromAttachments(
List<Attachment> attachments, {
Map<RegExp, InlineSpanBuilder>? textPatternSpan,
Map<RegExp, TextStyleBuilder>? textPatternStyle,
}) =>
MessageInputController._(
initialMessage: Message(attachments: attachments),
textPatternSpan: textPatternSpan,
textPatternStyle: textPatternStyle,
);
MessageInputController._({
required Message initialMessage,
Map<RegExp, InlineSpanBuilder>? textPatternSpan,
Map<RegExp, TextStyleBuilder>? textPatternStyle,
}) : _textEditingController = MessageTextFieldController.fromValue(
initialMessage.text == null
? const TextEditingValue()
@@ -57,7 +57,7 @@ class MessageInputController extends ValueNotifier<Message> {
text: initialMessage.text!,
composing: TextRange.collapsed(initialMessage.text!.length),
),
textPatternSpan: textPatternSpan,
textPatternStyle: textPatternStyle,
),
_initialMessage = initialMessage,
super(initialMessage) {
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
/// A function that takes a [BuildContext] and returns an [InlineSpan].
typedef InlineSpanBuilder = InlineSpan Function(
/// A function that takes a [BuildContext] and returns a [TextStyle].
typedef TextStyleBuilder = TextStyle? Function(
BuildContext context,
String text,
);
@@ -11,17 +11,17 @@ class MessageTextFieldController extends TextEditingController {
/// Returns a new MessageTextFieldController
MessageTextFieldController({
String? text,
this.textPatternSpan,
this.textPatternStyle,
}) : super(text: text);
/// Returns a new MessageTextFieldController with the given text [value].
MessageTextFieldController.fromValue(
TextEditingValue? value, {
this.textPatternSpan,
this.textPatternStyle,
}) : super.fromValue(value);
/// A map of style to apply to the text matching the RegExp patterns.
final Map<RegExp, InlineSpanBuilder>? textPatternSpan;
final Map<RegExp, TextStyleBuilder>? textPatternStyle;
/// Builds a [TextSpan] from the current text,
/// highlighting the matches for [textPatternStyle].
@@ -31,7 +31,7 @@ class MessageTextFieldController extends TextEditingController {
TextStyle? style,
required bool withComposing,
}) {
final pattern = textPatternSpan;
final pattern = textPatternStyle;
if (pattern == null || pattern.isEmpty) {
return super.buildTextSpan(
context: context,
@@ -45,10 +45,13 @@ class MessageTextFieldController extends TextEditingController {
onMatch: (match) {
final text = match[0]!;
final key = pattern.keys.firstWhere((it) => it.hasMatch(text));
return pattern[key]?.call(context, text) ??
TextSpan(
text: text,
);
return TextSpan(
text: text,
style: pattern[key]?.call(
context,
text,
),
);
},
);
}
@@ -57,10 +60,10 @@ class MessageTextFieldController extends TextEditingController {
extension _TextSpanX on TextSpan {
TextSpan splitMapJoin(
Pattern pattern, {
InlineSpan Function(Match)? onMatch,
InlineSpan Function(InlineSpan)? onNonMatch,
TextSpan Function(Match)? onMatch,
TextSpan Function(TextSpan)? onNonMatch,
}) {
final children = <InlineSpan>[];
final children = <TextSpan>[];
toPlainText().splitMapJoin(
pattern,