From 4085c6cdd5fe743bda998a3b1a9c81ce36514694 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 24 Jun 2019 10:48:31 +0200 Subject: [PATCH] Added support of weblink posts --- lib/ui/tiles/post_tile.dart | 40 +++++++++++++++++++++++++++++++++++++ lib/utils/ui_utils.dart | 6 ++++++ pubspec.lock | 14 +++++++++++++ pubspec.yaml | 3 +++ 4 files changed, 63 insertions(+) diff --git a/lib/ui/tiles/post_tile.dart b/lib/ui/tiles/post_tile.dart index 8f28099..338101b 100644 --- a/lib/ui/tiles/post_tile.dart +++ b/lib/ui/tiles/post_tile.dart @@ -23,6 +23,7 @@ import 'package:comunic/utils/navigation_utils.dart'; import 'package:comunic/utils/post_utils.dart'; import 'package:comunic/utils/ui_utils.dart'; import 'package:flutter/material.dart'; +import 'package:url_launcher/url_launcher.dart'; /// Single posts tile /// @@ -166,6 +167,10 @@ class _PostTileState extends State { postContent = _buildPostImage(); break; + case PostKind.WEB_LINK: + postContent = _buildPostWebLink(); + break; + default: } @@ -249,6 +254,41 @@ class _PostTileState extends State { ); } + Widget _buildPostWebLink() { + return Card( + color: Color.fromRGBO(0xf7, 0xf7, 0xf7, 1), + child: InkWell( + onTap: () => launch(widget.post.linkURL), + child: Row( + children: [ + Padding( + padding: const EdgeInsets.only(right: 8.0), + child: NetworkImageWidget( + url: widget.post.linkImage, + width: 70, + roundedEdges: false, + ), + ), + Flexible( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text(htmlDecodeCharacters(widget.post.linkTitle), + style: TextStyle(fontSize: 20.0)), + Text( + widget.post.linkURL, + maxLines: 3, + ), + Text(htmlDecodeCharacters(widget.post.linkDescription)) + ], + ), + ) + ], + ), + ), + ); + } + /// Build the list of comments Widget _buildComments() { assert(widget.post.hasComments); diff --git a/lib/utils/ui_utils.dart b/lib/utils/ui_utils.dart index fb62682..c077768 100644 --- a/lib/utils/ui_utils.dart +++ b/lib/utils/ui_utils.dart @@ -1,6 +1,7 @@ import 'package:cached_network_image/cached_network_image.dart'; import 'package:comunic/utils/intl_utils.dart'; import 'package:flutter/material.dart'; +import 'package:html/parser.dart'; /// User interface utilities /// @@ -171,3 +172,8 @@ Widget smartInputCounterWidgetBuilder( @required bool isFocused, }) => currentLength > 0 ? Text("$currentLength/$maxLength") : Container(); + +/// Parse an HTML String to decode special characters +String htmlDecodeCharacters(String input) { + return parse(input).documentElement.text; +} diff --git a/pubspec.lock b/pubspec.lock index 59a5dd6..31eb69b 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -57,6 +57,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.6" + csslib: + dependency: transitive + description: + name: csslib + url: "https://pub.dartlang.org" + source: hosted + version: "0.16.0" cupertino_icons: dependency: "direct main" description: @@ -88,6 +95,13 @@ packages: description: flutter source: sdk version: "0.0.0" + html: + dependency: "direct main" + description: + name: html + url: "https://pub.dartlang.org" + source: hosted + version: "0.14.0+2" http: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index b95e8f3..dc9c552 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -42,6 +42,9 @@ dependencies: # URL launcher is useful to open URL in the phone browser url_launcher: ^5.0.2 + # Allows to parse HTML special characters + html: ^0.14.0+2 + dev_dependencies: flutter_test: sdk: flutter