Skip to content

Commit

Permalink
Merge pull request #39 from felixgabler/fg/open_from_title_and_image
Browse files Browse the repository at this point in the history
Open link on text and image tap too
  • Loading branch information
demchenkoalex authored Feb 19, 2022
2 parents eface02 + f20b2c9 commit db4a5a5
Showing 1 changed file with 72 additions and 48 deletions.
120 changes: 72 additions & 48 deletions lib/src/widgets/link_preview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_chat_types/flutter_chat_types.dart' show PreviewData;
import 'package:flutter_linkify/flutter_linkify.dart' hide UrlLinkifier;
import 'package:url_launcher/url_launcher.dart';

import '../url_linkifier.dart' show UrlLinkifier;
import '../utils.dart' show getPreviewData;

Expand All @@ -25,6 +26,8 @@ class LinkPreview extends StatefulWidget {
this.metadataTitleStyle,
this.onLinkPressed,
required this.onPreviewDataFetched,
this.openOnPreviewImageClick = false,
this.openOnPreviewTitleClick = false,
this.padding,
required this.previewData,
required this.text,
Expand Down Expand Up @@ -72,6 +75,12 @@ class LinkPreview extends StatefulWidget {
/// preview data again.
final void Function(PreviewData) onPreviewDataFetched;

/// Open the link when the link preview image is clicked. Defaults to false.
final bool openOnPreviewImageClick;

/// Open the link when the link preview title is clicked. Defaults to false.
final bool openOnPreviewTitleClick;

/// Padding around initial text widget
final EdgeInsets? padding;

Expand Down Expand Up @@ -182,9 +191,11 @@ class _LinkPreviewState extends State<LinkPreview>
widget.previewData?.image?.url != null;
}

Future<void> _onOpen(LinkableElement link) async {
if (await canLaunch(link.url)) {
await launch(link.url);
Future<void> _onOpen(String url) async {
if (widget.onLinkPressed != null) {
widget.onLinkPressed!(url);
} else if (await canLaunch(url)) {
await launch(url);
}
}

Expand All @@ -208,23 +219,27 @@ class _LinkPreviewState extends State<LinkPreview>
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
padding: EdgeInsets.only(
bottom: _padding.bottom,
left: _padding.left,
right: _padding.right,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
if (data.title != null) _titleWidget(data.title!),
if (data.description != null)
_descriptionWidget(data.description!),
],
GestureDetector(
onTap:
widget.openOnPreviewTitleClick ? () => _onOpen(data.link!) : null,
child: Container(
padding: EdgeInsets.only(
bottom: _padding.bottom,
left: _padding.left,
right: _padding.right,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
if (data.title != null) _titleWidget(data.title!),
if (data.description != null)
_descriptionWidget(data.description!),
],
),
),
),
if (data.image?.url != null && widget.hideImage != true)
_imageWidget(data.image!.url, width),
_imageWidget(data.image!.url, data.link!, width),
],
);
}
Expand Down Expand Up @@ -295,18 +310,21 @@ class _LinkPreviewState extends State<LinkPreview>
);
}

Widget _imageWidget(String url, double width) {
return Container(
constraints: BoxConstraints(
maxHeight: width,
Widget _imageWidget(String imageUrl, String linkUrl, double width) {
return GestureDetector(
onTap: widget.openOnPreviewImageClick ? () => _onOpen(linkUrl) : null,
child: Container(
constraints: BoxConstraints(
maxHeight: width,
),
width: width,
child: widget.imageBuilder != null
? widget.imageBuilder!(imageUrl)
: Image.network(
imageUrl,
fit: BoxFit.contain,
),
),
width: width,
child: widget.imageBuilder != null
? widget.imageBuilder!(url)
: Image.network(
url,
fit: BoxFit.contain,
),
);
}

Expand All @@ -316,9 +334,7 @@ class _LinkPreviewState extends State<LinkPreview>
linkStyle: widget.linkStyle,
maxLines: 100,
minLines: 1,
onOpen: widget.onLinkPressed != null
? (element) => widget.onLinkPressed!(element.url)
: _onOpen,
onOpen: (link) => _onOpen(link.url),
options: const LinkifyOptions(
defaultToHttps: true,
humanize: false,
Expand All @@ -340,38 +356,46 @@ class _LinkPreviewState extends State<LinkPreview>
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Expanded(
child: Container(
margin: const EdgeInsets.only(right: 4),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
if (data.title != null) _titleWidget(data.title!),
if (data.description != null)
_descriptionWidget(data.description!),
],
child: GestureDetector(
onTap: widget.openOnPreviewTitleClick
? () => _onOpen(data.link!)
: null,
child: Container(
margin: const EdgeInsets.only(right: 4),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
if (data.title != null) _titleWidget(data.title!),
if (data.description != null)
_descriptionWidget(data.description!),
],
),
),
),
),
if (data.image?.url != null && widget.hideImage != true)
_minimizedImageWidget(data.image!.url),
_minimizedImageWidget(data.image!.url, data.link!),
],
),
),
],
);
}

Widget _minimizedImageWidget(String url) {
Widget _minimizedImageWidget(String imageUrl, String linkUrl) {
return ClipRRect(
borderRadius: const BorderRadius.all(
Radius.circular(12),
),
child: SizedBox(
height: 48,
width: 48,
child: widget.imageBuilder != null
? widget.imageBuilder!(url)
: Image.network(url),
child: GestureDetector(
onTap: widget.openOnPreviewImageClick ? () => _onOpen(linkUrl) : null,
child: SizedBox(
height: 48,
width: 48,
child: widget.imageBuilder != null
? widget.imageBuilder!(imageUrl)
: Image.network(imageUrl),
),
),
);
}
Expand Down

0 comments on commit db4a5a5

Please sign in to comment.