mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-11-22 12:59:21 +00:00
Parse URLs
This commit is contained in:
parent
3389ca18f7
commit
7de882338d
@ -1,6 +1,8 @@
|
|||||||
import 'package:comunic/utils/bbcode_parser.dart';
|
import 'package:comunic/utils/bbcode_parser.dart';
|
||||||
|
import 'package:comunic/utils/input_utils.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_emoji/flutter_emoji.dart';
|
import 'package:flutter_emoji/flutter_emoji.dart';
|
||||||
|
import 'package:url_launcher/url_launcher.dart';
|
||||||
|
|
||||||
/// Text widget
|
/// Text widget
|
||||||
///
|
///
|
||||||
@ -26,12 +28,65 @@ class TextWidget extends StatelessWidget {
|
|||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
var content = EmojiParser().emojify(this.content);
|
var content = EmojiParser().emojify(this.content);
|
||||||
|
|
||||||
|
// Parse BBcode
|
||||||
if (this.parseBBcode)
|
if (this.parseBBcode)
|
||||||
return BBCodeParsedWidget(text: content);
|
return BBCodeParsedWidget(
|
||||||
else
|
text: content,
|
||||||
return Text(
|
parseCallback: (style, text) => _parseLinks(text, style),
|
||||||
content,
|
|
||||||
style: style,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Just parse link
|
||||||
|
return RichText(
|
||||||
|
text: TextSpan(children: _parseLinks(content, style)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sub parse function
|
||||||
|
List<InlineSpan> _parseLinks(String text, TextStyle style) {
|
||||||
|
var buff = StringBuffer();
|
||||||
|
final list = new List<InlineSpan>();
|
||||||
|
|
||||||
|
// Change word function
|
||||||
|
final changeWordType = () {
|
||||||
|
list.add(TextSpan(style: style, text: buff.toString()));
|
||||||
|
buff.clear();
|
||||||
|
};
|
||||||
|
|
||||||
|
for (final word in text.replaceAll("\n", " \n ").split(" ")) {
|
||||||
|
// Line break
|
||||||
|
if (word == "\n") {
|
||||||
|
buff.write("\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if it is an URL
|
||||||
|
if (validateUrl(word)) {
|
||||||
|
changeWordType();
|
||||||
|
|
||||||
|
list.add(
|
||||||
|
WidgetSpan(
|
||||||
|
child: InkWell(
|
||||||
|
child: Text(
|
||||||
|
word,
|
||||||
|
style: style.copyWith(color: Colors.blueAccent),
|
||||||
|
),
|
||||||
|
onTap: () => launch(word),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
buff.write(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Simple word
|
||||||
|
else {
|
||||||
|
buff.write(word);
|
||||||
|
buff.write(" ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (buff.isNotEmpty) changeWordType();
|
||||||
|
|
||||||
|
return list;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ import 'package:flutter/material.dart';
|
|||||||
|
|
||||||
/// This callback return null if the text has to be left as is or a TextSpan
|
/// This callback return null if the text has to be left as is or a TextSpan
|
||||||
/// if it has been sub parsed...
|
/// if it has been sub parsed...
|
||||||
typedef ParseCallBack = List<TextSpan> Function(TextStyle, String);
|
typedef ParseCallBack = List<InlineSpan> Function(TextStyle, String);
|
||||||
|
|
||||||
class BBCodeParsedWidget extends StatelessWidget {
|
class BBCodeParsedWidget extends StatelessWidget {
|
||||||
final _Element _content;
|
final _Element _content;
|
||||||
@ -241,7 +241,6 @@ class _Element {
|
|||||||
|
|
||||||
if (parsed != null && parsed.length > 0)
|
if (parsed != null && parsed.length > 0)
|
||||||
return TextSpan(
|
return TextSpan(
|
||||||
text: text,
|
|
||||||
style: generatedStyle,
|
style: generatedStyle,
|
||||||
children: parsed,
|
children: parsed,
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user