Skip to content

Commit

Permalink
feat: open link on text and image tap too
Browse files Browse the repository at this point in the history
  • Loading branch information
felixgabler committed Feb 1, 2022
1 parent d8500bb commit 530d112
Showing 1 changed file with 61 additions and 48 deletions.
109 changes: 61 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 Down Expand Up @@ -175,9 +176,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 @@ -201,23 +204,26 @@ 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: () => _onOpen(data.link!),
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 @@ -288,18 +294,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: () => _onOpen(linkUrl),
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 @@ -309,9 +318,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 @@ -333,38 +340,44 @@ 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: () => _onOpen(data.link!),
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: () => _onOpen(linkUrl),
child: SizedBox(
height: 48,
width: 48,
child: widget.imageBuilder != null
? widget.imageBuilder!(imageUrl)
: Image.network(imageUrl),
),
),
);
}
Expand Down

0 comments on commit 530d112

Please sign in to comment.