mirror of
				https://gitlab.com/comunic/comunicmobile
				synced 2025-10-31 10:14:50 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			128 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			128 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
| import 'package:comunic/utils/bbcode_parser.dart';
 | |
| import 'package:comunic/utils/input_utils.dart';
 | |
| import 'package:comunic/utils/navigation_utils.dart';
 | |
| import 'package:flutter/material.dart';
 | |
| import 'package:flutter_emoji/flutter_emoji.dart';
 | |
| import 'package:url_launcher/url_launcher.dart';
 | |
| 
 | |
| /// Text widget
 | |
| ///
 | |
| /// The content passed to this widget is automatically parsed
 | |
| ///
 | |
| /// @author Pierre Hubert
 | |
| 
 | |
| class TextWidget extends StatefulWidget {
 | |
|   final String content;
 | |
|   final bool parseBBcode;
 | |
|   final TextStyle style;
 | |
| 
 | |
|   const TextWidget({
 | |
|     Key key,
 | |
|     @required this.content,
 | |
|     this.parseBBcode = false,
 | |
|     this.style,
 | |
|   })  : assert(content != null),
 | |
|         assert(parseBBcode != null),
 | |
|         super(key: key);
 | |
| 
 | |
|   @override
 | |
|   _TextWidgetState createState() => _TextWidgetState();
 | |
| }
 | |
| 
 | |
| class _TextWidgetState extends State<TextWidget> {
 | |
|   Widget _cache;
 | |
| 
 | |
|   @override
 | |
|   Widget build(BuildContext context) {
 | |
|     if (_cache == null) _cache = doBuild();
 | |
| 
 | |
|     return _cache;
 | |
|   }
 | |
| 
 | |
|   Widget doBuild() {
 | |
|     var content = EmojiParser().emojify(this.widget.content);
 | |
| 
 | |
|     // Parse BBcode
 | |
|     if (this.widget.parseBBcode)
 | |
|       return BBCodeParsedWidget(
 | |
|         text: content,
 | |
|         parseCallback: (style, text) => _parseLinks(text, style),
 | |
|       );
 | |
| 
 | |
|     // Just parse link
 | |
|     return RichText(
 | |
|       text: TextSpan(children: _parseLinks(content, widget.style)),
 | |
|     );
 | |
|   }
 | |
| 
 | |
|   /// Sub parse function
 | |
|   List<InlineSpan> _parseLinks(String text, TextStyle style) {
 | |
|     if (style == null) style = TextStyle();
 | |
| 
 | |
|     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(" ");
 | |
|       }
 | |
| 
 | |
|       // Check if it is a directory reference
 | |
|       else if (validateDirectoryReference(word)) {
 | |
|         changeWordType();
 | |
| 
 | |
|         list.add(
 | |
|           WidgetSpan(
 | |
|             child: InkWell(
 | |
|               child: Text(
 | |
|                 word,
 | |
|                 style: style.copyWith(color: Colors.blueAccent),
 | |
|               ),
 | |
|               onTap: () => openVirtualDirectory(context, word),
 | |
|             ),
 | |
|           ),
 | |
|         );
 | |
| 
 | |
|         buff.write(" ");
 | |
|       }
 | |
| 
 | |
|       // Simple word
 | |
|       else {
 | |
|         buff.write(word);
 | |
|         buff.write(" ");
 | |
|       }
 | |
|     }
 | |
| 
 | |
|     if (buff.isNotEmpty) changeWordType();
 | |
| 
 | |
|     return list;
 | |
|   }
 | |
| }
 |