From eee965fba1c0068ec5ab187c09881fc048854d8b Mon Sep 17 00:00:00 2001 From: krawieck Date: Mon, 31 Aug 2020 19:56:48 +0200 Subject: [PATCH] * move comment out of `widgets/` * remove `Widget` suffix from widgets * change `list.length != 0` to `list.isEmpty` * convert MarkdownText into stateless widget --- lib/{widgets => }/comment.dart | 13 +++++----- lib/widgets/comments.dart | 14 +++++------ lib/widgets/markdown_text.dart | 46 ++++++++++++++++++++++------------ lib/widgets/post.dart | 6 ++--- 4 files changed, 46 insertions(+), 33 deletions(-) rename lib/{widgets => }/comment.dart (94%) diff --git a/lib/widgets/comment.dart b/lib/comment.dart similarity index 94% rename from lib/widgets/comment.dart rename to lib/comment.dart index 043602b..0a5d21c 100644 --- a/lib/widgets/comment.dart +++ b/lib/comment.dart @@ -1,14 +1,14 @@ import 'package:cached_network_image/cached_network_image.dart'; import 'package:flutter/material.dart'; -import 'comment_tree.dart'; -import 'markdown_text.dart'; +import 'widgets/comment_tree.dart'; +import 'widgets/markdown_text.dart'; -class CommentWidget extends StatelessWidget { +class Comment extends StatelessWidget { final int indent; final int postCreatorId; final CommentTree commentTree; - CommentWidget( + Comment( this.commentTree, { this.indent = 0, @required this.postCreatorId, @@ -47,8 +47,7 @@ class CommentWidget extends StatelessWidget { style: TextStyle(fontStyle: FontStyle.italic), )); } else { - body = - Flexible(child: MarkdownText(commentTree.comment.content, context)); + body = Flexible(child: MarkdownText(commentTree.comment.content)); } return Column( children: [ @@ -108,7 +107,7 @@ class CommentWidget extends StatelessWidget { top: BorderSide(width: 0.2))), ), for (var c in commentTree.children) - CommentWidget( + Comment( c, indent: indent + 1, postCreatorId: postCreatorId, diff --git a/lib/widgets/comments.dart b/lib/widgets/comments.dart index be6039c..7337471 100644 --- a/lib/widgets/comments.dart +++ b/lib/widgets/comments.dart @@ -2,24 +2,25 @@ import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:lemmy_api_client/lemmy_api_client.dart'; -import 'comment.dart'; +import '../comment.dart'; import 'comment_tree.dart'; /// Manages comments section, sorts them -class CommentsWidget extends HookWidget { +class Comments extends HookWidget { final List rawComments; final List comments; final int postCreatorId; - CommentsWidget(this.rawComments, this.postCreatorId) - : comments = CommentTree.fromList(rawComments); + Comments(this.rawComments, {@required this.postCreatorId}) + : comments = CommentTree.fromList(rawComments), + assert(postCreatorId != null); @override Widget build(BuildContext context) { var sorting = useState(SortType.active); return Column(children: [ // sorting menu goes here - if (comments.length == 0) + if (comments.isEmpty) Padding( padding: EdgeInsets.symmetric(vertical: 50), child: Text( @@ -27,8 +28,7 @@ class CommentsWidget extends HookWidget { style: TextStyle(fontStyle: FontStyle.italic), ), ), - for (var com in comments) - CommentWidget(com, postCreatorId: postCreatorId), + for (var com in comments) Comment(com, postCreatorId: postCreatorId), ]); } } diff --git a/lib/widgets/markdown_text.dart b/lib/widgets/markdown_text.dart index f6d90f2..a315f50 100644 --- a/lib/widgets/markdown_text.dart +++ b/lib/widgets/markdown_text.dart @@ -1,23 +1,37 @@ +import 'package:cached_network_image/cached_network_image.dart'; import 'package:flutter/material.dart'; import 'package:flutter_markdown/flutter_markdown.dart'; import 'package:markdown/markdown.dart' as md; import '../url_launcher.dart'; -Widget MarkdownText(String text, BuildContext context) { - return MarkdownBody( - data: text, - extensionSet: md.ExtensionSet.gitHubWeb, - onTapLink: (href) { - urlLauncher(href) - .catchError((e) => Scaffold.of(context).showSnackBar(SnackBar( - content: Row( - children: [ - Icon(Icons.warning), - Text('couldn\'t open link'), - ], - ), - ))); - }, - ); +class MarkdownText extends StatelessWidget { + final String text; + MarkdownText(this.text); + + @override + Widget build(BuildContext context) => MarkdownBody( + data: text, + extensionSet: md.ExtensionSet.gitHubWeb, + onTapLink: (href) { + urlLauncher(href) + .catchError((e) => Scaffold.of(context).showSnackBar(SnackBar( + content: Row( + children: [ + Icon(Icons.warning), + Text('couldn\'t open link'), + ], + ), + ))); + }, + imageBuilder: (uri, title, alt) => CachedNetworkImage( + imageUrl: uri.toString(), + errorWidget: (context, url, error) => Row( + children: [ + Icon(Icons.warning), + Text('couldn\'t load image, ${error.toString()}') + ], + ), + ), + ); } diff --git a/lib/widgets/post.dart b/lib/widgets/post.dart index 3044634..27e2ec8 100644 --- a/lib/widgets/post.dart +++ b/lib/widgets/post.dart @@ -25,7 +25,7 @@ MediaType whatType(String url) { return MediaType.other; } -class PostWidget extends StatelessWidget { +class Post extends StatelessWidget { final PostView post; final String hostUrl; @@ -35,7 +35,7 @@ class PostWidget extends StatelessWidget { ThemeData _theme; BuildContext _context; - PostWidget(this.post) + Post(this.post) : hostUrl = post.communityActorId.split('/')[2], linkPostDomain = post.url != null ? post.url.split('/')[2] : null; @@ -327,7 +327,7 @@ class PostWidget extends StatelessWidget { Widget _textBody() { return Padding( padding: const EdgeInsets.all(10), - child: MarkdownText(post.body, _context), + child: MarkdownText(post.body), ); }