flyerhq / flutter_link_previewer

Customizable link and URL preview extracted from the provided text with the ability to render from the cache. Ideal for chat applications.
MIT License
72 stars 75 forks source link

Wrong Image #55

Open ribalassaf opened 9 months ago

ribalassaf commented 9 months ago

I'm using this package inside of a chat screen when I send a link it fetches the image of the previously sent link .

demchenkoalex commented 8 months ago

I just tried the example in the chat UI package and two links I sent had correct data. Can you share more information please? how do you use this package?

Astaxask commented 7 months ago

The code works just fine. However, every time a link is posted in chat the UI gets rebuilt which is not a good UX design.

demchenkoalex commented 7 months ago

@Astaxask unfortunately this is how Flutter works. Every time you add something to the list view, all visible items are rebuilt :/

Astaxask commented 7 months ago

You might want to have a look at "any_link_preview". They managed to optimize the solution without rebuilding the UI. The thing about that library, though, is the way images are displayed. You guys have a better preview design. But if only you can find a way around rebuilding the UI every time and implement the same functionality as "any_link_preview".

demchenkoalex commented 7 months ago

we are talking about different things. You were saying

However, every time a link is posted in chat the UI gets rebuilt which is not a good UX design

here is a flutter documentation for a list that can add items to it https://api.flutter.dev/flutter/widgets/AnimatedList-class.html if you add print("rebuilt"); to the line 186 of the example you will see

rebuilt
rebuilt
rebuilt

in the console, because there are 3 items. Next, if you press + to add a new item you will see

rebuilt
rebuilt
rebuilt
rebuilt
rebuilt
rebuilt
rebuilt

3 + 4, because flutter rebuilds everything visible on screen when new items is added, no library can change that. This is what happens with this preview or will happen to any other preview.

If you mean that you add a link (for example apple.com) and it shows a preview and then you add next message and previous preview gets rebuilt and UI jumps, then check the README

LinkPreview(
  enableAnimation: true,
  onPreviewDataFetched: (data) {
    setState(() {
      // Save preview data to the state              
    });
  },
  previewData: _previewData, // Pass the preview data from the state
  text: 'https://flyer.chat',
  width: MediaQuery.of(context).size.width,
)

onPreviewDataFetched you should save the fetched preview data and pass it to the previewData param, this way when flutter will rebuilt the list item (and it will) library will read from the cached previewData and not jump/fetch it again.