mirror of
				https://gitlab.com/comunic/comunicmobile
				synced 2025-11-04 04:04:18 +00:00 
			
		
		
		
	Parse links
This commit is contained in:
		@@ -2,6 +2,7 @@ import 'package:cached_network_image/cached_network_image.dart';
 | 
				
			|||||||
import 'package:comunic/models/conversation_message.dart';
 | 
					import 'package:comunic/models/conversation_message.dart';
 | 
				
			||||||
import 'package:comunic/models/user.dart';
 | 
					import 'package:comunic/models/user.dart';
 | 
				
			||||||
import 'package:comunic/ui/widgets/account_image_widget.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/account_utils.dart';
 | 
				
			||||||
import 'package:comunic/utils/date_utils.dart';
 | 
					import 'package:comunic/utils/date_utils.dart';
 | 
				
			||||||
import 'package:comunic/utils/ui_utils.dart';
 | 
					import 'package:comunic/utils/ui_utils.dart';
 | 
				
			||||||
@@ -118,7 +119,7 @@ class ConversationMessageTile extends StatelessWidget {
 | 
				
			|||||||
                      Container(
 | 
					                      Container(
 | 
				
			||||||
                        child: message.hasMessage
 | 
					                        child: message.hasMessage
 | 
				
			||||||
                            ? Container(
 | 
					                            ? Container(
 | 
				
			||||||
                                child: Text(
 | 
					                                child: TextRichContentWidget(
 | 
				
			||||||
                                  message.message,
 | 
					                                  message.message,
 | 
				
			||||||
                                  textAlign: TextAlign.justify,
 | 
					                                  textAlign: TextAlign.justify,
 | 
				
			||||||
                                  style: TextStyle(color: Colors.white),
 | 
					                                  style: TextStyle(color: Colors.white),
 | 
				
			||||||
@@ -190,9 +191,10 @@ class ConversationMessageTile extends StatelessWidget {
 | 
				
			|||||||
                  Container(
 | 
					                  Container(
 | 
				
			||||||
                    child: message.hasMessage
 | 
					                    child: message.hasMessage
 | 
				
			||||||
                        ? Container(
 | 
					                        ? Container(
 | 
				
			||||||
                            child: Text(
 | 
					                            child: TextRichContentWidget(
 | 
				
			||||||
                              message.message,
 | 
					                              message.message,
 | 
				
			||||||
                              textAlign: TextAlign.justify,
 | 
					                              textAlign: TextAlign.justify,
 | 
				
			||||||
 | 
					                              style: TextStyle(color: Colors.black),
 | 
				
			||||||
                            ),
 | 
					                            ),
 | 
				
			||||||
                            padding:
 | 
					                            padding:
 | 
				
			||||||
                                EdgeInsets.fromLTRB(15.0, 10.0, 15.0, 10.0),
 | 
					                                EdgeInsets.fromLTRB(15.0, 10.0, 15.0, 10.0),
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										72
									
								
								lib/ui/widgets/text_rich_content_widget.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								lib/ui/widgets/text_rich_content_widget.dart
									
									
									
									
									
										Normal 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);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -11,3 +11,19 @@ bool validateEmail(String value) {
 | 
				
			|||||||
  RegExp regex = new RegExp(pattern);
 | 
					  RegExp regex = new RegExp(pattern);
 | 
				
			||||||
  return regex.hasMatch(value);
 | 
					  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;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -226,6 +226,13 @@ packages:
 | 
				
			|||||||
      url: "https://pub.dartlang.org"
 | 
					      url: "https://pub.dartlang.org"
 | 
				
			||||||
    source: hosted
 | 
					    source: hosted
 | 
				
			||||||
    version: "1.1.6"
 | 
					    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:
 | 
					  uuid:
 | 
				
			||||||
    dependency: transitive
 | 
					    dependency: transitive
 | 
				
			||||||
    description:
 | 
					    description:
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -39,6 +39,9 @@ dependencies:
 | 
				
			|||||||
  # This plugins allows to load remote images
 | 
					  # This plugins allows to load remote images
 | 
				
			||||||
  cached_network_image: ^0.7.0
 | 
					  cached_network_image: ^0.7.0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  # URL launcher is useful to open URL in the phone browser
 | 
				
			||||||
 | 
					  url_launcher: ^5.0.2
 | 
				
			||||||
 | 
					
 | 
				
			||||||
dev_dependencies:
 | 
					dev_dependencies:
 | 
				
			||||||
  flutter_test:
 | 
					  flutter_test:
 | 
				
			||||||
    sdk: flutter
 | 
					    sdk: flutter
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user