1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 12:59:21 +00:00

Parse links

This commit is contained in:
Pierre HUBERT 2019-05-01 10:21:26 +02:00
parent e7c34e6e71
commit fcc8c2faa4
5 changed files with 102 additions and 2 deletions

View File

@ -2,6 +2,7 @@ import 'package:cached_network_image/cached_network_image.dart';
import 'package:comunic/models/conversation_message.dart';
import 'package:comunic/models/user.dart';
import 'package:comunic/ui/widgets/account_image_widget.dart';
import 'package:comunic/ui/widgets/text_rich_content_widget.dart';
import 'package:comunic/utils/account_utils.dart';
import 'package:comunic/utils/date_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
@ -118,7 +119,7 @@ class ConversationMessageTile extends StatelessWidget {
Container(
child: message.hasMessage
? Container(
child: Text(
child: TextRichContentWidget(
message.message,
textAlign: TextAlign.justify,
style: TextStyle(color: Colors.white),
@ -190,9 +191,10 @@ class ConversationMessageTile extends StatelessWidget {
Container(
child: message.hasMessage
? Container(
child: Text(
child: TextRichContentWidget(
message.message,
textAlign: TextAlign.justify,
style: TextStyle(color: Colors.black),
),
padding:
EdgeInsets.fromLTRB(15.0, 10.0, 15.0, 10.0),

View File

@ -0,0 +1,72 @@
import 'dart:ui';
import 'package:comunic/utils/input_utils.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
/// Text rich content widget
///
/// This widget is used to parse user content (URLs, references,
/// layout content...)
///
/// @author Pierre HUBERT
class TextRichContentWidget extends StatelessWidget {
final TextSpan rootSpan;
final TextAlign textAlign;
final TextStyle style;
TextRichContentWidget(
String text, {
this.textAlign,
this.style,
}) : assert(text != null),
/// Parse content now so we won't have to do it later
rootSpan = TextSpan(style: style, children: _parse(text, style));
/// Parse the text and return it as a list of span elements
static List<TextSpan> _parse(String text, TextStyle style) {
if (style == null) style = TextStyle();
List<TextSpan> list = List();
String currString = "";
text.split("\n").forEach((f) {
text.split(" ").forEach((s) {
if (validateUrl(s)) {
if (currString.length > 0)
//"Flush" previous text
list.add(TextSpan(style: style, text: currString + " "));
// Add link
list.add(TextSpan(
style: style.copyWith(color: Colors.indigo),
text: s,
recognizer: TapGestureRecognizer()
..onTap = () {
launch(s);
}));
currString = "";
} else
currString += s;
currString += " ";
});
currString += "\n";
});
list.add(TextSpan(
style: style, text: currString.substring(0, currString.length - 2)));
return list;
}
@override
Widget build(BuildContext context) {
return RichText(textAlign: textAlign, text: rootSpan);
}
}

View File

@ -11,3 +11,19 @@ bool validateEmail(String value) {
RegExp regex = new RegExp(pattern);
return regex.hasMatch(value);
}
/// Check out whether a given string is a valid URL or not
bool validateUrl(String url) {
//Initial check
if (!url.startsWith("http://") && !url.startsWith("https://")) return false;
try {
final uri = Uri.parse(url);
return uri.hasScheme &&
uri.hasAuthority &&
uri.port != 0 &&
uri.path.length != 0;
} catch (e) {
return false;
}
}

View File

@ -226,6 +226,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.6"
url_launcher:
dependency: "direct main"
description:
name: url_launcher
url: "https://pub.dartlang.org"
source: hosted
version: "5.0.2"
uuid:
dependency: transitive
description:

View File

@ -39,6 +39,9 @@ dependencies:
# This plugins allows to load remote images
cached_network_image: ^0.7.0
# URL launcher is useful to open URL in the phone browser
url_launcher: ^5.0.2
dev_dependencies:
flutter_test:
sdk: flutter