Skip to content

Commit

Permalink
Fixes after PR
Browse files Browse the repository at this point in the history
  • Loading branch information
demchenkoalex committed May 1, 2021
1 parent 22c8fe8 commit 98c6f48
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .metadata
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# This file should be version controlled and should not be manually edited.

version:
revision: 4d7946a68d26794349189cf21b3f68cc6fe61dcb
revision: adc687823a831bbebe28bdccfac1a628ca621513
channel: stable

project_type: package
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.5

- Add an optional animation

## 1.0.4

- Update dependencies
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

Preview of the link extracted from the provided text with basic customization and ability to render from cached data.

<img src="https://user-images.githubusercontent.com/14123304/110205826-88bb0580-7e7a-11eb-8101-b12e83ce0787.PNG" width="320" height="693">
<img src="https://user-images.githubusercontent.com/14123304/116777066-81743a80-aa6c-11eb-89bc-d4166c418878.png" width="428" height="926">

## Getting Started

Expand Down
2 changes: 1 addition & 1 deletion example/.metadata
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# This file should be version controlled and should not be manually edited.

version:
revision: 4d7946a68d26794349189cf21b3f68cc6fe61dcb
revision: adc687823a831bbebe28bdccfac1a628ca621513
channel: stable

project_type: app
8 changes: 5 additions & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class MyHomePage extends StatelessWidget {
),
body: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
margin: const EdgeInsets.all(16),
Expand All @@ -48,7 +49,8 @@ class MyHomePage extends StatelessWidget {
Radius.circular(20),
),
child: LinkPreview(
text: 'https://github.com/flyerhq',
enableAnimation: true,
text: 'https://flyer.chat',
width: MediaQuery.of(context).size.width,
),
),
Expand All @@ -66,8 +68,8 @@ class MyHomePage extends StatelessWidget {
Radius.circular(20),
),
child: LinkPreview(
text:
'https://dev.to/demchenkoalex/making-a-right-keyboard-accessory-view-in-react-native-4n3p',
enableAnimation: true,
text: 'https://github.com/flyerhq',
width: MediaQuery.of(context).size.width,
),
),
Expand Down
25 changes: 15 additions & 10 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,22 @@ String? _getActualImageUrl(String baseUrl, String? imageUrl) {
}

Future<Size> _getImageSize(String url) {
final image = Image.network(url);
final stream = Image.network(url).image.resolve(ImageConfiguration.empty);
final completer = Completer<Size>();
final listener = ImageStreamListener(
(ImageInfo info, bool _) => completer.complete(
Size(
height: info.image.height.toDouble(),
width: info.image.width.toDouble(),
),
),
);
image.image.resolve(ImageConfiguration.empty).addListener(listener);

void listener(ImageInfo info, bool _) {
if (!completer.isCompleted) {
completer.complete(
Size(
height: info.image.height.toDouble(),
width: info.image.width.toDouble(),
),
);
stream.removeListener(ImageStreamListener(listener));
}
}

stream.addListener(ImageStreamListener(listener));
return completer.future;
}

Expand Down
41 changes: 25 additions & 16 deletions lib/src/widgets/link_preview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import '../utils.dart' show getPreviewData;
class LinkPreview extends StatefulWidget {
/// Creates [LinkPreview]
const LinkPreview({
Key? key,
this.animationDuration,
this.enableAnimation = false,
Key? key,
this.linkStyle,
this.metadataTextStyle,
this.metadataTitleStyle,
Expand All @@ -26,10 +26,10 @@ class LinkPreview extends StatefulWidget {
required this.width,
}) : super(key: key);

/// Expanding animation duration
/// Expand animation duration
final Duration? animationDuration;

/// Enables expanding animation
/// Enables expand animation. Default value is false.
final bool? enableAnimation;

/// Style of highlighted links in the text
Expand Down Expand Up @@ -70,23 +70,35 @@ class LinkPreview extends StatefulWidget {
class _LinkPreviewState extends State<LinkPreview>
with SingleTickerProviderStateMixin {
late final AnimationController _controller = AnimationController(
duration: widget.animationDuration ?? const Duration(seconds: 1),
duration: widget.animationDuration ?? const Duration(milliseconds: 300),
vsync: this,
)..forward();

late final Animation<double> _animation = CurvedAnimation(
parent: _controller,
curve: Curves.fastOutSlowIn,
curve: Curves.easeOutQuad,
);

@override
void dispose() {
super.dispose();
_controller.dispose();
super.dispose();
}

Future<PreviewData> _fetchData(String text) async {
return await getPreviewData(text);
final previewData = await getPreviewData(text);
_handlePreviewDataFetched(previewData);
return previewData;
}

void _handlePreviewDataFetched(PreviewData previewData) {
Future.delayed(
widget.animationDuration ?? const Duration(milliseconds: 300),
).then((_) {
if (mounted) {
widget.onPreviewDataFetched?.call(previewData);
}
});
}

Future<void> _onOpen(LinkableElement link) async {
Expand All @@ -99,9 +111,9 @@ class _LinkPreviewState extends State<LinkPreview>

Widget _animated(Widget child) {
return SizeTransition(
sizeFactor: _animation,
axis: Axis.vertical,
axisAlignment: -1,
sizeFactor: _animation,
child: child,
);
}
Expand Down Expand Up @@ -132,16 +144,16 @@ class _LinkPreviewState extends State<LinkPreview>

Widget _containerWidget({
required bool animate,
Widget? child,
bool withPadding = false,
Widget? child,
}) {
final _padding = widget.padding ??
const EdgeInsets.symmetric(
horizontal: 24,
vertical: 16,
);

final shouldAnimate = widget.enableAnimation ?? animate;
final shouldAnimate = widget.enableAnimation == true && animate;

return Container(
constraints: BoxConstraints(maxWidth: widget.width),
Expand All @@ -157,6 +169,7 @@ class _LinkPreviewState extends State<LinkPreview>
vertical: 16,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_linkify(),
if (withPadding && child != null)
Expand Down Expand Up @@ -279,14 +292,10 @@ class _LinkPreviewState extends State<LinkPreview>
: _fetchData(widget.text);

return FutureBuilder<PreviewData>(
initialData: null,
initialData: widget.previewData,
future: _previewData,
builder: (BuildContext context, AsyncSnapshot<PreviewData> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting ||
snapshot.hasError ||
snapshot.data == null) return _containerWidget(animate: false);

widget.onPreviewDataFetched?.call(snapshot.data!);
if (snapshot.data == null) return _containerWidget(animate: false);

final aspectRatio = snapshot.data!.image == null
? null
Expand Down
8 changes: 4 additions & 4 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: flutter_link_previewer
description: >
Preview of the link extracted from the provided text
with basic customization and ability to render from cached data.
version: 1.0.4
version: 1.0.5
homepage: https://github.com/flyerhq/flutter_link_previewer

environment:
Expand All @@ -12,10 +12,10 @@ environment:
dependencies:
flutter:
sdk: flutter
flutter_chat_types: ^2.0.6
flutter_linkify: ^5.0.0
flutter_chat_types: ^2.1.1
flutter_linkify: ^5.0.2
html: ^0.15.0
http: ^0.13.1
http: ^0.13.2
linkify: ^4.0.0
meta: ^1.3.0
url_launcher: ^6.0.3
Expand Down

0 comments on commit 98c6f48

Please sign in to comment.