A Flutter package for beautifully displaying web URL previews with full customization options, perfect for enhancing chat applications. 🤓🤓
AnyLinkPreview.isValidLink()
to validate URLs before usage.git checkout -b my-new-feature
).git commit -am 'Add some feature'
).git push origin my-new-feature
). /// Display direction. One among `uiDirectionVertical, uiDirectionHorizontal`
/// By default it is `uiDirectionVertical`
final UIDirection displayDirection;
/// Parameter to choose how you'd like the app to handle the link
/// Default is `LaunchMode.platformDefault`
final LaunchMode urlLaunchMode;
/// Web address (Url that need to be parsed)
/// For IOS & Web, only HTTP and HTTPS are support
/// For Android, all url's are supported
final String link;
/// Customize background colour
/// Deaults to `Color.fromRGBO(235, 235, 235, 1)`
final Color? backgroundColor;
/// Widget that need to be shown when
/// plugin is trying to fetch metadata
/// If not given anything then default one will be shown
final Widget? placeholderWidget;
/// Widget that need to be shown if something goes wrong
/// Defaults to plain container with given background colour
/// If the issue is know then we will show customized UI
/// Other options of error params are used
final Widget? errorWidget;
/// Title that need to be shown if something goes wrong
/// Deaults to `Something went wrong!`
final String? errorTitle;
/// Body that need to be shown if something goes wrong
/// Deaults to `Oops! Unable to parse the url. We have sent feedback to our developers & we will try to fix this in our next release. Thanks!`
final String? errorBody;
/// Image that will be shown if something goes wrong
/// & when multimedia enabled & no meta data is available
/// Deaults to `A semi-soccer ball image that looks like crying`
final String? errorImage;
/// Give the overflow type for body text (Description)
/// Deaults to `TextOverflow.ellipsis`
final TextOverflow bodyTextOverflow;
/// Give the limit to body text (Description)
/// Deaults to `3`
final int bodyMaxLines;
/// Cache result time, default cache `1 day`
/// Pass null to disable
final Duration cache;
/// Customize body `TextStyle`
final TextStyle? titleStyle;
/// Customize body `TextStyle`
final TextStyle? bodyStyle;
/// Show or Hide image if available defaults to `true`
final bool showMultimedia;
/// BorderRadius for the card. Deafults to `12`
final double? borderRadius;
/// To remove the card elevation set it to `true`
/// Default value is `false`
final bool removeElevation;
/// Box shadow for the card. Deafults to `[BoxShadow(blurRadius: 3, color: Colors.grey)]`
final List<BoxShadow>? boxShadow;
/// Proxy URL to pass that resolve CORS issues on web.
/// For example, `https://corsproxy.org/` .
final String? proxyUrl;
/// Headers to be added in the HTTP request to the link
final Map<String, String>? headers;
/// Function that needs to be called when user taps on the card.
/// If not given then given URL will be launched.
/// To disable, Pass empty function.
final void Function()? onTap;
/// Height of the preview card. Defaults to
/// `(MediaQuery.of(context).size.height) * 0.15` in case of horizontal and
/// `(MediaQuery.of(context).size.height) * 0.25` in case of vertical
final double? previewHeight;
/// Function only in [AnyLinkPreview.builder]
/// allows to build a custom [Widget] from the [Metadata] and [ImageProvider] fetched
final Widget Function(BuildContext, Metadata, ImageProvider?)? itemBuilder;
Example of AnyLinkPreview widget:
AnyLinkPreview(
link: "https://vardaan.app/",
displayDirection: UIDirection.uiDirectionHorizontal,
showMultimedia: false,
bodyMaxLines: 5,
bodyTextOverflow: TextOverflow.ellipsis,
titleStyle: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 15,
),
bodyStyle: TextStyle(color: Colors.grey, fontSize: 12),
errorBody: 'Show my custom error body',
errorTitle: 'Show my custom error title',
errorWidget: Container(
color: Colors.grey[300],
child: Text('Oops!'),
),
errorImage: "https://google.com/",
cache: Duration(days: 7),
backgroundColor: Colors.grey[300],
borderRadius: 12,
removeElevation: false,
boxShadow: [BoxShadow(blurRadius: 3, color: Colors.grey)],
onTap: (){}, // This disables tap event
)
Example of using AnyLinkPreview as a method/function:
// We will use this to build our own custom UI
Metadata? _metadata = await AnyLinkPreview.getMetadata(
link: "https://google.com/",
cache: Duration(days: 7),
proxyUrl: "https://corsproxy.org/", // Need for web
);
print(_metadata?.title);
Example os using AnyLinkPreview's validation method/function:
// Method to check if the url is valid or not before passing to the previewer
bool _isUrlValid = AnyLinkPreview.isValidLink(
"https://google.com/",
protocols: ['http', 'https'],
hostWhitelist: ['https://youtube.com/'],
hostBlacklist: ['https://facebook.com/'],
);
print('_isUrlValid => $_isUrlValid');
Display Direction can be among these 2 types:
enum UIDirection { uiDirectionVertical, uiDirectionHorizontal }
Thanks to all the contributors
Copyright (c) 2020-2024 Konakanchi Venkata Suresh Babu
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.