可检测文本框
具有检测功能的文本小部件。你可以检测主题标签、at符号、URL或任何你想要的内容。它有助于你开发类似Twitter的应用。
是对 hashtagable 的改进。
用法
作为TextField
DetectableTextField(
detectionRegExp: detectionRegExp(),
detectedStyle: TextStyle(
fontSize: 20,
color: Colors.blue,
),
basicStyle: TextStyle(
fontSize: 20,
),
)
-
detectionRegExp决定要检测的文本。detectedStyle是检测到的文本的文本样式。basicStyle 是未检测到的文本的文本样式。 -
其他参数基本与material TextField相同。
作为ReadOnlyText
如果你想在文本中仅用于显示的文本中使用检测功能,DetectableText 将会帮助你。
DetectableText(
text: "#HashTag and @AtSign and https://pub.dev/packages/detectable_text_field",
detectionRegExp: detectionRegExp(),
detectedStyle: TextStyle(
fontSize: 20,
color: Colors.blue,
),
basicStyle: TextStyle(
fontSize: 20,
),
onTap: (tappedText){
print(tappedText);
},
)
detectionRegExp 等参数的用法与DetectableTextField 中的相同。
当用户点击检测到的文本时,会调用onTap(String)参数。
你可以在此回调函数中根据点击的文本添加一些操作。
detectionRegExp()
此包中的小部件和方法预计与RegExp一起使用。
函数detectionRegExp() 根据布尔参数:hashtag、atSign和url 返回示例regExp。它们默认都为true。
如果你不想检测at符号,你需要这样设置参数:detectionRegExp(atSign:false)。其他参数也一样。
如果你查看API参考,你会发现该函数直接返回下面的示例正则表达式。如果你愿意,可以直接使用它们。
| 示例regExp | 主题标签 | at符号 | url |
|---|---|---|---|
hashTagRegExp |
○ | × | × |
atSignRegExp |
× | ○ | × |
urlRegExp |
× | × | ○ |
hashTagAtSignRegExp |
○ | ○ | × |
hashTagUrlRegExp |
○ | × | ○ |
AtSignUrlRegExp |
× | ○ | ○ |
hashTagAtSignUrlRegExp |
○ | ○ | ○ |
- 检测规则几乎与Twitter相同。
- 它需要在“#”或“@”前面有空格。
- 如果存在表情符号或符号,它会停止“#”和“@”的检测。
- 目前的支持的示例语言有六种:英语、日语、韩语、西班牙语、阿拉伯语和泰语。
通过实用函数进行定制
- 检查是否存在检测
print(isDetected("Hello #World", hashTagRegExp));
// true
print(isDetected("Hello World", hashTagRegExp));
// false
- 从文本中提取检测内容
final List<String> detections = extractDetections("#Hello World #Flutter Dart #Thank you", hashTagRegExp);
// ["#Hello", "#Flutter", "#Thank"]
如果您有任何请求或疑问,请随时在github上提问。

