move ui building functions into the build method

This commit is contained in:
krawieck 2020-08-31 21:03:50 +02:00
parent fdcb29fb1c
commit 3c47eb0886
1 changed files with 268 additions and 269 deletions

View File

@ -27,17 +27,14 @@ MediaType whatType(String url) {
class Post extends StatelessWidget {
final PostView post;
final String hostUrl;
final String instanceUrl;
/// nullable
final String linkPostDomain;
ThemeData _theme;
BuildContext _context;
final String postUrlDomain;
Post(this.post)
: hostUrl = post.communityActorId.split('/')[2],
linkPostDomain = post.url != null ? post.url.split('/')[2] : null;
: instanceUrl = post.communityActorId.split('/')[2],
postUrlDomain = post.url != null ? post.url.split('/')[2] : null;
// == ACTIONS ==
@ -49,8 +46,8 @@ class Post extends StatelessWidget {
print('GO TO USER');
}
void _goToPost() {
print('GO OT POST');
void _goToPost(BuildContext context) {
print('GO TO POST');
}
void _goToCommunity() {
@ -87,34 +84,11 @@ class Post extends StatelessWidget {
@override
Widget build(BuildContext context) {
_theme = Theme.of(context);
_context = context;
final theme = Theme.of(context);
return Container(
decoration: BoxDecoration(
boxShadow: [BoxShadow(blurRadius: 10, color: Colors.black54)],
color: _theme.colorScheme.surface,
borderRadius: BorderRadius.all(Radius.circular(20)),
),
child: InkWell(
onTap: _goToPost,
child: Column(
children: [
_info(),
_title(),
if (whatType(post.url) != MediaType.other)
_postImage()
else if (post.url != null)
_linkPreview(),
if (post.body != null) _textBody(),
_actions(),
],
),
),
);
}
Widget _info() {
/// assemble info section
Widget info() {
// TODO: add NSFW, locked, removed, deleted, stickied
return Column(children: [
Padding(
padding: const EdgeInsets.all(10),
@ -156,7 +130,7 @@ class Post extends StatelessWidget {
overflow: TextOverflow.ellipsis, // TODO: fix overflowing
text: TextSpan(
style: TextStyle(
fontSize: 15, color: _theme.textTheme.bodyText1.color),
fontSize: 15, color: theme.textTheme.bodyText1.color),
children: [
TextSpan(
text: '!',
@ -170,7 +144,7 @@ class Post extends StatelessWidget {
text: '@',
style: TextStyle(fontWeight: FontWeight.w300)),
TextSpan(
text: hostUrl,
text: instanceUrl,
style: TextStyle(fontWeight: FontWeight.w600),
recognizer: TapGestureRecognizer()
..onTap = _goToInstance),
@ -184,7 +158,7 @@ class Post extends StatelessWidget {
text: TextSpan(
style: TextStyle(
fontSize: 13,
color: _theme.textTheme.bodyText1.color),
color: theme.textTheme.bodyText1.color),
children: [
TextSpan(
text: 'by',
@ -193,13 +167,14 @@ class Post extends StatelessWidget {
text:
''' ${post.creatorPreferredUsername ?? post.creatorName}''',
style: TextStyle(fontWeight: FontWeight.w600),
recognizer: TapGestureRecognizer()..onTap = _goToUser,
recognizer: TapGestureRecognizer()
..onTap = _goToUser,
),
TextSpan(
text:
''' · ${timeago.format(post.published, locale: 'en_short')}'''),
if (linkPostDomain != null)
TextSpan(text: ' · $linkPostDomain'),
if (postUrlDomain != null)
TextSpan(text: ' · $postUrlDomain'),
if (post.locked) TextSpan(text: ' · 🔒'),
],
))
@ -221,7 +196,8 @@ class Post extends StatelessWidget {
]);
}
Widget _title() {
/// assemble title section
Widget title() {
return Padding(
padding: const EdgeInsets.only(left: 10, right: 10, bottom: 10),
child: Row(
@ -269,20 +245,8 @@ class Post extends StatelessWidget {
);
}
Widget _postImage() {
assert(post.url != null);
return InkWell(
onTap: _openFullImage,
child: CachedNetworkImage(
imageUrl: post.url,
progressIndicatorBuilder: (context, url, progress) =>
CircularProgressIndicator(value: progress.progress),
),
);
}
Widget _linkPreview() {
/// assemble link preview
Widget linkPreview() {
assert(post.url != null);
var url = post.url.split('/')[2];
@ -305,14 +269,14 @@ class Post extends StatelessWidget {
Row(children: [
Spacer(),
Text('$url ',
style: _theme.textTheme.caption
style: theme.textTheme.caption
.apply(fontStyle: FontStyle.italic)),
Icon(Icons.launch, size: 12),
]),
Row(children: [
Flexible(
child: Text(post.embedTitle,
style: _theme.textTheme.subtitle1
style: theme.textTheme.subtitle1
.apply(fontWeightDelta: 2)))
]),
Row(children: [Flexible(child: Text(post.embedDescription))]),
@ -324,14 +288,22 @@ class Post extends StatelessWidget {
);
}
Widget _textBody() {
return Padding(
padding: const EdgeInsets.all(10),
child: MarkdownText(post.body),
/// assemble image
Widget postImage() {
assert(post.url != null);
return InkWell(
onTap: _openFullImage,
child: CachedNetworkImage(
imageUrl: post.url,
progressIndicatorBuilder: (context, url, progress) =>
CircularProgressIndicator(value: progress.progress),
),
);
}
Widget _actions() {
/// assemble actions section
Widget actions() {
return Padding(
padding: const EdgeInsets.fromLTRB(10, 5, 10, 5),
child: Row(
@ -363,4 +335,31 @@ class Post extends StatelessWidget {
),
);
}
return Container(
decoration: BoxDecoration(
boxShadow: [BoxShadow(blurRadius: 10, color: Colors.black54)],
color: theme.colorScheme.surface,
borderRadius: BorderRadius.all(Radius.circular(20)),
),
child: InkWell(
onTap: () => _goToPost(context),
child: Column(
children: [
info(),
title(),
if (whatType(post.url) != MediaType.other)
postImage()
else if (post.url != null)
linkPreview(),
if (post.body != null)
Padding(
padding: const EdgeInsets.all(10),
child: MarkdownText(post.body)),
actions(),
],
),
),
);
}
}