Skip to content

Commit

Permalink
fix url regex and add previewBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
demchenkoalex committed May 16, 2023
1 parent 9d1bec5 commit 4d97b3d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
4 changes: 2 additions & 2 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,11 @@ Future<PreviewData> getPreviewData(
}

/// Regex to check if text is email.
const regexEmail = r'([a-zA-Z0-9+._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)';
const regexEmail = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}';

/// Regex to check if content type is an image.
const regexImageContentType = r'image\/*';

/// Regex to find all links in the text.
const regexLink =
r'((http|ftp|https):\/\/)?([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?';
r'((http|ftp|https):\/\/)?([\w_-]+(?:(?:\.[\w_-]*[a-zA-Z_][\w_-]*)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?[^\.\s]';
36 changes: 22 additions & 14 deletions lib/src/widgets/link_preview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class LinkPreview extends StatefulWidget {
this.openOnPreviewImageTap = false,
this.openOnPreviewTitleTap = false,
this.padding,
this.previewBuilder,
required this.previewData,
this.requestTimeout,
required this.text,
Expand Down Expand Up @@ -86,6 +87,9 @@ class LinkPreview extends StatefulWidget {
/// Padding around initial text widget.
final EdgeInsets? padding;

/// Function that allows you to build a custom link preview.
final Widget Function(BuildContext, PreviewData)? previewBuilder;

/// Pass saved [PreviewData] here so [LinkPreview] would not fetch preview
/// data again.
final PreviewData? previewData;
Expand Down Expand Up @@ -422,20 +426,24 @@ class _LinkPreviewState extends State<LinkPreview>
final previewData = widget.previewData;

if (previewData != null && _hasData(previewData)) {
final aspectRatio = widget.previewData!.image == null
? null
: widget.previewData!.image!.width /
widget.previewData!.image!.height;

final width = aspectRatio == 1 ? widget.width : widget.width - 32;

return _containerWidget(
animate: shouldAnimate,
child: aspectRatio == 1
? _minimizedBodyWidget(previewData)
: _bodyWidget(previewData, width),
withPadding: aspectRatio == 1,
);
if (widget.previewBuilder != null) {
return widget.previewBuilder!(context, previewData);
} else {
final aspectRatio = widget.previewData!.image == null
? null
: widget.previewData!.image!.width /
widget.previewData!.image!.height;

final width = aspectRatio == 1 ? widget.width : widget.width - 32;

return _containerWidget(
animate: shouldAnimate,
child: aspectRatio == 1
? _minimizedBodyWidget(previewData)
: _bodyWidget(previewData, width),
withPadding: aspectRatio == 1,
);
}
} else {
return _containerWidget(animate: false);
}
Expand Down

0 comments on commit 4d97b3d

Please sign in to comment.