|
| 1 | +```dart |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:flutter_link_previewer/flutter_link_previewer.dart'; |
| 4 | +import 'package:flutter_chat_core/flutter_chat_core.dart' show LinkPreviewData; |
| 5 | +
|
| 6 | +class MyChatBubble extends StatefulWidget { |
| 7 | + const MyChatBubble({super.key, required this.message}); |
| 8 | +
|
| 9 | + final String message; |
| 10 | +
|
| 11 | + @override |
| 12 | + State<MyChatBubble> createState() => _MyChatBubbleState(); |
| 13 | +} |
| 14 | +
|
| 15 | +class _MyChatBubbleState extends State<MyChatBubble> { |
| 16 | + LinkPreviewData? _linkPreviewData; |
| 17 | +
|
| 18 | + @override |
| 19 | + Widget build(BuildContext context) { |
| 20 | + return Container( |
| 21 | + padding: const EdgeInsets.all(16), |
| 22 | + child: Column( |
| 23 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 24 | + children: [ |
| 25 | + Text(widget.message), |
| 26 | + LinkPreview( |
| 27 | + // The text that should be parsed to find the first URL |
| 28 | + text: widget.message, |
| 29 | + // Pass the cached preview data to avoid re-fetching |
| 30 | + linkPreviewData: _linkPreviewData, |
| 31 | + // Callback to store the fetched preview data |
| 32 | + onLinkPreviewDataFetched: (data) { |
| 33 | + setState(() { |
| 34 | + _linkPreviewData = data; |
| 35 | + }); |
| 36 | + }, |
| 37 | + // For a chat bubble, you would pass the message text here |
| 38 | + // to align the preview with the text bubble. |
| 39 | + parentContent: widget.message, |
| 40 | + // Customization example |
| 41 | + borderRadius: 4, |
| 42 | + sideBorderColor: Colors.white, |
| 43 | + sideBorderWidth: 4, |
| 44 | + insidePadding: const EdgeInsets.fromLTRB(12, 8, 8, 8), |
| 45 | + outsidePadding: const EdgeInsets.symmetric(vertical: 4), |
| 46 | + titleTextStyle: const TextStyle( |
| 47 | + fontWeight: FontWeight.bold, |
| 48 | + fontSize: 20, |
| 49 | + ), |
| 50 | + ), |
| 51 | + ], |
| 52 | + ), |
| 53 | + ); |
| 54 | + } |
| 55 | +} |
| 56 | +
|
| 57 | +``` |
0 commit comments