From f0e45a5a510d2b4693580179645bd0bb0b352f86 Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Mon, 22 May 2023 09:00:45 -0600 Subject: Soporte y restricción para links MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/widgets/chat_bubble.dart | 105 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 91 insertions(+), 14 deletions(-) (limited to 'lib/widgets/chat_bubble.dart') diff --git a/lib/widgets/chat_bubble.dart b/lib/widgets/chat_bubble.dart index aaac93d..7c2e16c 100644 --- a/lib/widgets/chat_bubble.dart +++ b/lib/widgets/chat_bubble.dart @@ -1,14 +1,16 @@ import 'package:custom_clippers/custom_clippers.dart'; import 'package:flutter/material.dart'; +import 'package:linkchat/models/message.dart'; +import 'package:url_launcher/link.dart'; enum ChatBubbleAlignment { start, end } class ChatBubble extends StatelessWidget { - final String text; + final Message message; final ChatBubbleAlignment alignment; const ChatBubble( - this.text, { + this.message, { super.key, required this.alignment, }); @@ -19,17 +21,17 @@ class ChatBubble extends StatelessWidget { crossAxisAlignment: CrossAxisAlignment.start, children: [ alignment == ChatBubbleAlignment.start - ? BubbleLeft(text) - : BubbleRight(text), + ? BubbleLeft(message) + : BubbleRight(message), ], ); } } class BubbleLeft extends StatelessWidget { - final String text; + final Message message; - const BubbleLeft(this.text, {super.key}); + const BubbleLeft(this.message, {super.key}); @override Widget build(BuildContext context) { @@ -42,9 +44,20 @@ class BubbleLeft extends StatelessWidget { decoration: const BoxDecoration( color: Color(0xFFE1E1E2), ), - child: Text( - text, - style: const TextStyle(fontSize: 15, color: Colors.black), + child: Column( + children: [ + LinkPreview(message), + Link( + uri: Uri.parse(message.messageText), + builder: (context, followLink) => TextButton( + onPressed: followLink, + child: Text( + message.messageText, + style: const TextStyle(color: Colors.blue), + ), + ), + ), + ], ), ), ), @@ -53,9 +66,9 @@ class BubbleLeft extends StatelessWidget { } class BubbleRight extends StatelessWidget { - final String text; + final Message message; - const BubbleRight(this.text, {super.key}); + const BubbleRight(this.message, {super.key}); @override Widget build(BuildContext context) { @@ -71,9 +84,21 @@ class BubbleRight extends StatelessWidget { decoration: const BoxDecoration( color: Color(0xFF113753), ), - child: Text( - text, - style: const TextStyle(fontSize: 15, color: Colors.white), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + LinkPreview(message), + Link( + uri: Uri.parse(message.messageText), + builder: (context, followLink) => TextButton( + onPressed: followLink, + child: Text( + message.messageText, + style: const TextStyle(color: Colors.blue), + ), + ), + ), + ], ), ), ), @@ -81,3 +106,55 @@ class BubbleRight extends StatelessWidget { ); } } + +class LinkPreview extends StatelessWidget { + final Message message; + const LinkPreview(this.message, {super.key}); + + @override + Widget build(BuildContext context) { + return (message.linkTitle != null || + message.linkDescription != null || + message.linkPhotoURL != null) + ? Padding( + padding: const EdgeInsets.only(bottom: 8.0), + child: Card( + clipBehavior: Clip.antiAliasWithSaveLayer, + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + message.linkPhotoURL != null + ? FadeInImage( + placeholder: const AssetImage('assets/loading.gif'), + image: NetworkImage(message.linkPhotoURL!), + imageErrorBuilder: (context, error, stackTrace) => + const SizedBox.shrink(), + fit: BoxFit.fill, + ) + : const SizedBox.shrink(), + message.linkTitle != null + ? Padding( + padding: const EdgeInsets.all(8.0), + child: Text( + message.linkTitle!, + style: Theme.of(context) + .textTheme + .bodyLarge! + .copyWith(fontWeight: FontWeight.bold), + ), + ) + : const SizedBox.shrink(), + message.linkDescription != null + ? Padding( + padding: + const EdgeInsets.fromLTRB(8.0, 0.0, 8.0, 8.0), + child: Text(message.linkDescription!), + ) + : const SizedBox.shrink(), + ], + ), + ), + ) + : const SizedBox.shrink(); + } +} -- cgit v1.2.3