Skip to content

Commit 2938f64

Browse files
feat: link preview package (#790)
* Add empty package * Add old lib utils * Bump dependencies, rename * Add new preview widget * Adapt base text widgets to the new intended layout * Add example * Add more constraints options * Better image image size management * Ensure bubble sizes account for the timeAndStatus widget size * Fix forgotten extra linkPreview widget on top * Update callback to include if sentByMe and update example * Fix crashes * Simplify onTap * Lowercase text, iOS has a tendency to put HTTPS which does not work * Revert previous commit * Fix: return the proxied url for image content * Fix crash when content is not utf8 * Add try-catch to always return something if possible even when images 404 * Better handle redirects for relative images paths * Remove commented code * Fix preview not display if no TimeAndStatus * rebase and fix dependencies * chore: fixes after review --------- Co-authored-by: Alex Demchenko <[email protected]>
1 parent 5b08a68 commit 2938f64

File tree

22 files changed

+1398
-101
lines changed

22 files changed

+1398
-101
lines changed

examples/flyer_chat/ios/Podfile.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ PODS:
4747
- SDWebImage/Core (= 5.21.0)
4848
- SDWebImage/Core (5.21.0)
4949
- SwiftyGif (5.4.5)
50+
- url_launcher_ios (0.0.1):
51+
- Flutter
5052

5153
DEPENDENCIES:
5254
- file_picker (from `.symlinks/plugins/file_picker/ios`)
@@ -55,6 +57,7 @@ DEPENDENCIES:
5557
- integration_test (from `.symlinks/plugins/integration_test/ios`)
5658
- isar_flutter_libs (from `.symlinks/plugins/isar_flutter_libs/ios`)
5759
- path_provider_foundation (from `.symlinks/plugins/path_provider_foundation/darwin`)
60+
- url_launcher_ios (from `.symlinks/plugins/url_launcher_ios/ios`)
5861

5962
SPEC REPOS:
6063
trunk:
@@ -76,6 +79,8 @@ EXTERNAL SOURCES:
7679
:path: ".symlinks/plugins/isar_flutter_libs/ios"
7780
path_provider_foundation:
7881
:path: ".symlinks/plugins/path_provider_foundation/darwin"
82+
url_launcher_ios:
83+
:path: ".symlinks/plugins/url_launcher_ios/ios"
7984

8085
SPEC CHECKSUMS:
8186
DKImagePickerController: 946cec48c7873164274ecc4624d19e3da4c1ef3c
@@ -88,6 +93,7 @@ SPEC CHECKSUMS:
8893
path_provider_foundation: 080d55be775b7414fd5a5ef3ac137b97b097e564
8994
SDWebImage: f84b0feeb08d2d11e6a9b843cb06d75ebf5b8868
9095
SwiftyGif: 706c60cf65fa2bc5ee0313beece843c8eb8194d4
96+
url_launcher_ios: 694010445543906933d732453a59da0a173ae33d
9197

9298
PODFILE CHECKSUM: 4305caec6b40dde0ae97be1573c53de1882a07e5
9399

examples/flyer_chat/lib/local.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:flutter/material.dart';
77
import 'package:flutter/services.dart';
88
import 'package:flutter_chat_core/flutter_chat_core.dart';
99
import 'package:flutter_chat_ui/flutter_chat_ui.dart';
10+
import 'package:flutter_link_previewer/flutter_link_previewer.dart';
1011
import 'package:flyer_chat_file_message/flyer_chat_file_message.dart';
1112
import 'package:flyer_chat_image_message/flyer_chat_image_message.dart';
1213
import 'package:flyer_chat_system_message/flyer_chat_system_message.dart';
@@ -127,6 +128,25 @@ class LocalState extends State<Local> {
127128
],
128129
),
129130
),
131+
linkPreviewBuilder: (context, message, isSentByMe) {
132+
// It's up to you to (optionally) implement the logic to avoid every
133+
// message to refetch the preview data
134+
//
135+
// For example, you can use a metadata to indicate if the preview
136+
// was already fetched (or null).
137+
//
138+
// Additionally, you can cache the data to avoid re-fetching across app restarts.
139+
return LinkPreview(
140+
text: message.text,
141+
linkPreviewData: message.linkPreviewData,
142+
onLinkPreviewDataFetched: (linkPreviewData) {
143+
_chatController.updateMessage(
144+
message,
145+
message.copyWith(linkPreviewData: linkPreviewData),
146+
);
147+
},
148+
);
149+
},
130150
textMessageBuilder:
131151
(
132152
context,

examples/flyer_chat/linux/flutter/generated_plugin_registrant.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <file_selector_linux/file_selector_plugin.h>
1010
#include <isar_flutter_libs/isar_flutter_libs_plugin.h>
11+
#include <url_launcher_linux/url_launcher_plugin.h>
1112

1213
void fl_register_plugins(FlPluginRegistry* registry) {
1314
g_autoptr(FlPluginRegistrar) file_selector_linux_registrar =
@@ -16,4 +17,7 @@ void fl_register_plugins(FlPluginRegistry* registry) {
1617
g_autoptr(FlPluginRegistrar) isar_flutter_libs_registrar =
1718
fl_plugin_registry_get_registrar_for_plugin(registry, "IsarFlutterLibsPlugin");
1819
isar_flutter_libs_plugin_register_with_registrar(isar_flutter_libs_registrar);
20+
g_autoptr(FlPluginRegistrar) url_launcher_linux_registrar =
21+
fl_plugin_registry_get_registrar_for_plugin(registry, "UrlLauncherPlugin");
22+
url_launcher_plugin_register_with_registrar(url_launcher_linux_registrar);
1923
}

examples/flyer_chat/linux/flutter/generated_plugins.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
list(APPEND FLUTTER_PLUGIN_LIST
66
file_selector_linux
77
isar_flutter_libs
8+
url_launcher_linux
89
)
910

1011
list(APPEND FLUTTER_FFI_PLUGIN_LIST

examples/flyer_chat/macos/Flutter/GeneratedPluginRegistrant.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import file_picker
99
import file_selector_macos
1010
import isar_flutter_libs
1111
import path_provider_foundation
12+
import url_launcher_macos
1213

1314
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
1415
FilePickerPlugin.register(with: registry.registrar(forPlugin: "FilePickerPlugin"))
1516
FileSelectorPlugin.register(with: registry.registrar(forPlugin: "FileSelectorPlugin"))
1617
IsarFlutterLibsPlugin.register(with: registry.registrar(forPlugin: "IsarFlutterLibsPlugin"))
1718
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
19+
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
1820
}

examples/flyer_chat/macos/Podfile.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ PODS:
99
- path_provider_foundation (0.0.1):
1010
- Flutter
1111
- FlutterMacOS
12+
- url_launcher_macos (0.0.1):
13+
- FlutterMacOS
1214

1315
DEPENDENCIES:
1416
- file_picker (from `Flutter/ephemeral/.symlinks/plugins/file_picker/macos`)
1517
- file_selector_macos (from `Flutter/ephemeral/.symlinks/plugins/file_selector_macos/macos`)
1618
- FlutterMacOS (from `Flutter/ephemeral`)
1719
- isar_flutter_libs (from `Flutter/ephemeral/.symlinks/plugins/isar_flutter_libs/macos`)
1820
- path_provider_foundation (from `Flutter/ephemeral/.symlinks/plugins/path_provider_foundation/darwin`)
21+
- url_launcher_macos (from `Flutter/ephemeral/.symlinks/plugins/url_launcher_macos/macos`)
1922

2023
EXTERNAL SOURCES:
2124
file_picker:
@@ -28,13 +31,16 @@ EXTERNAL SOURCES:
2831
:path: Flutter/ephemeral/.symlinks/plugins/isar_flutter_libs/macos
2932
path_provider_foundation:
3033
:path: Flutter/ephemeral/.symlinks/plugins/path_provider_foundation/darwin
34+
url_launcher_macos:
35+
:path: Flutter/ephemeral/.symlinks/plugins/url_launcher_macos/macos
3136

3237
SPEC CHECKSUMS:
3338
file_picker: 7584aae6fa07a041af2b36a2655122d42f578c1a
3439
file_selector_macos: 6280b52b459ae6c590af5d78fc35c7267a3c4b31
3540
FlutterMacOS: 8f6f14fa908a6fb3fba0cd85dbd81ec4b251fb24
3641
isar_flutter_libs: a65381780401f81ad6bf3f2e7cd0de5698fb98c4
3742
path_provider_foundation: 080d55be775b7414fd5a5ef3ac137b97b097e564
43+
url_launcher_macos: 0fba8ddabfc33ce0a9afe7c5fef5aab3d8d2d673
3844

3945
PODFILE CHECKSUM: 7eb978b976557c8c1cd717d8185ec483fd090a82
4046

examples/flyer_chat/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ dependencies:
3939
flutter_chat_core: ^2.6.2
4040
flutter_chat_ui: ^2.6.2
4141
flutter_dotenv: ^5.2.1
42+
flutter_link_previewer: ^4.0.0
4243
flutter_lorem: ^2.0.0
4344
flyer_chat_file_message: ^2.2.2
4445
flyer_chat_image_message: ^2.1.12

examples/flyer_chat/windows/flutter/generated_plugin_registrant.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88

99
#include <file_selector_windows/file_selector_windows.h>
1010
#include <isar_flutter_libs/isar_flutter_libs_plugin.h>
11+
#include <url_launcher_windows/url_launcher_windows.h>
1112

1213
void RegisterPlugins(flutter::PluginRegistry* registry) {
1314
FileSelectorWindowsRegisterWithRegistrar(
1415
registry->GetRegistrarForPlugin("FileSelectorWindows"));
1516
IsarFlutterLibsPluginRegisterWithRegistrar(
1617
registry->GetRegistrarForPlugin("IsarFlutterLibsPlugin"));
18+
UrlLauncherWindowsRegisterWithRegistrar(
19+
registry->GetRegistrarForPlugin("UrlLauncherWindows"));
1720
}

examples/flyer_chat/windows/flutter/generated_plugins.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
list(APPEND FLUTTER_PLUGIN_LIST
66
file_selector_windows
77
isar_flutter_libs
8+
url_launcher_windows
89
)
910

1011
list(APPEND FLUTTER_FFI_PLUGIN_LIST

packages/flutter_chat_core/lib/src/models/builders.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ typedef LoadMoreBuilder = Widget Function(BuildContext);
131131
typedef EmptyChatListBuilder = Widget Function(BuildContext);
132132

133133
/// Signature for building the link preview widget.
134-
typedef LinkPreviewBuilder = Widget? Function(BuildContext, TextMessage);
134+
typedef LinkPreviewBuilder =
135+
Widget? Function(BuildContext, TextMessage, bool isSendByMe);
135136

136137
/// A collection of builder functions used to customize the UI components
137138
/// of the chat interface.

0 commit comments

Comments
 (0)