From 315c61a2125da196fb4477931828c53ff005ea6a Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 11 May 2019 09:48:01 +0200 Subject: [PATCH] Display posts text and images --- lib/models/post.dart | 2 + lib/ui/tiles/post_tile.dart | 88 +++++++++++++++++++++++- lib/ui/widgets/network_image_widget.dart | 5 +- 3 files changed, 92 insertions(+), 3 deletions(-) diff --git a/lib/models/post.dart b/lib/models/post.dart index 1fd356f..982f49b 100644 --- a/lib/models/post.dart +++ b/lib/models/post.dart @@ -61,4 +61,6 @@ class Post { assert(likes != null), assert(userLikes != null), assert(access != null); + + bool get hasContent => content != null; } diff --git a/lib/ui/tiles/post_tile.dart b/lib/ui/tiles/post_tile.dart index dfebf21..53f0143 100644 --- a/lib/ui/tiles/post_tile.dart +++ b/lib/ui/tiles/post_tile.dart @@ -1,11 +1,22 @@ +import 'package:comunic/enums/post_kind.dart'; import 'package:comunic/lists/users_list.dart'; import 'package:comunic/models/post.dart'; +import 'package:comunic/models/user.dart'; +import 'package:comunic/ui/widgets/account_image_widget.dart'; +import 'package:comunic/ui/widgets/network_image_widget.dart'; +import 'package:comunic/utils/date_utils.dart'; import 'package:flutter/material.dart'; /// Single posts tile /// /// @author Pierre HUBERT +/// User style +const TextStyle _userNameStyle = TextStyle( + color: Color.fromRGBO(0x72, 0xaf, 0xd2, 1.0), //#72afd2 + fontWeight: FontWeight.w600, + fontSize: 16.0); + class PostTile extends StatelessWidget { final Post post; final UsersList usersInfo; @@ -18,10 +29,83 @@ class PostTile extends StatelessWidget { assert(usersInfo != null), super(key: key); + User get _user => usersInfo.getUser(post.userID); + + Widget _buildHeaderRow() { + // Header row + return Row( + children: [ + // User account image + Padding( + padding: const EdgeInsets.only(right: 8.0, left: 8.0), + child: AccountImageWidget(user: _user), + ), + + // Column with user name + post target + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + _user.displayName, + style: _userNameStyle, + ), + Text(diffTimeFromNowToStr(post.timeSent)), + ], + ), + ), + + PopupMenuButton( + itemBuilder: (c) => [], + ) + ], + ); + } + + Widget _buildContentRow() { + Widget postContent; + switch (post.kind) { + case PostKind.IMAGE: + postContent = _buildPostImage(); + break; + + default: + } + + return Column( + children: [ + // Post "rich" content + Container(child: postContent), + + // Post text + Container(child: post.hasContent ? Text(post.content) : null), + ], + ); + } + @override Widget build(BuildContext context) { - return ListTile( - leading: Text("a post"), + return Card( + elevation: 1.0, + child: Padding( + padding: const EdgeInsets.all(8.0), + + // Main post column + child: Column( + children: [ + _buildHeaderRow(), + _buildContentRow(), + ], + ), + ), + ); + } + + Widget _buildPostImage() { + return NetworkImageWidget( + url: post.fileURL, + allowFullScreen: true, + roundedEdges: false, ); } } diff --git a/lib/ui/widgets/network_image_widget.dart b/lib/ui/widgets/network_image_widget.dart index 7de54da..d8278a0 100644 --- a/lib/ui/widgets/network_image_widget.dart +++ b/lib/ui/widgets/network_image_widget.dart @@ -11,6 +11,7 @@ import 'package:flutter/material.dart'; class NetworkImageWidget extends StatelessWidget { final String url; final bool allowFullScreen; + final bool roundedEdges; final double width; final double height; @@ -20,6 +21,7 @@ class NetworkImageWidget extends StatelessWidget { this.allowFullScreen = false, this.width, this.height, + this.roundedEdges = true, }) : assert(url != null), assert(allowFullScreen != null), super(key: key); @@ -59,7 +61,8 @@ class NetworkImageWidget extends StatelessWidget { ), ), ), - borderRadius: BorderRadius.all(Radius.circular(8.0)), + borderRadius: + roundedEdges ? BorderRadius.all(Radius.circular(8.0)) : null, clipBehavior: Clip.hardEdge, ); }