display images

This commit is contained in:
zoe 2022-08-06 16:05:14 +02:00
parent 652db6391e
commit ceb9c3922f
6 changed files with 89 additions and 6 deletions

View File

@ -2,10 +2,12 @@ class AccountModel {
late String acct;
late String displayName;
late String avatar;
late String url;
AccountModel.fromJson(Map<String, dynamic> json) {
acct = json["acct"];
displayName = json["display_name"];
avatar = json["avatar"];
url = json["url"];
}
}

View File

@ -0,0 +1,23 @@
enum MediaAttachmentType {
image,
video,
gifv,
audio,
unknown,
}
class MediaAttachmentModel {
late MediaAttachmentType type;
late String url;
late String previewUrl;
late String? description;
MediaAttachmentModel.fromJson(Map<String, dynamic> json) {
url = json["url"];
previewUrl = json["preview_url"];
type = MediaAttachmentType.values.firstWhere((element) =>
// ignore: prefer_interpolation_to_compose_strings
element.toString() == "MediaAttachmentType." + json["type"]);
description = json["description"];
}
}

View File

@ -1,6 +1,7 @@
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:slothmu/business_logic/account/account.dart';
import 'package:slothmu/business_logic/timeline/media.dart';
import '../settings.dart' as settings;
import '../../global.dart' as global;
@ -22,6 +23,7 @@ class PostModel implements Comparable {
late bool favourited;
late bool reblogged;
late AccountModel account;
late List<MediaAttachmentModel> attachments;
PostModel.fromJson(Map<String, dynamic> json) {
id = json["id"] as String;
@ -35,6 +37,11 @@ class PostModel implements Comparable {
favourited = json["favourited"] as bool;
reblogged = json["reblogged"] as bool;
account = AccountModel.fromJson(json["account"]);
attachments = [];
List<dynamic> jsonAttachmentList = json["media_attachments"];
for (int i = 0; i < jsonAttachmentList.length; i++) {
attachments.add(MediaAttachmentModel.fromJson(jsonAttachmentList[i]));
}
}
@override

View File

@ -11,6 +11,11 @@
"notifications": "notifications",
"settings": "settings",
"show": "show",
"hide": "hide"
"hide": "hide",
"media-not-supported": "media type not supported",
"show-about-page": "show about page",
"about": "about",
"account-settings": "account settings",
"logout": "log out"
}

View File

@ -0,0 +1,36 @@
import 'package:flutter/widgets.dart';
import 'package:localization/localization.dart';
import 'package:slothmu/business_logic/timeline/media.dart';
class MediaAttachments extends StatelessWidget {
const MediaAttachments({required this.models, Key? key}) : super(key: key);
final List<MediaAttachmentModel> models;
@override
Widget build(BuildContext context) {
List<Widget> children = [];
for (int i = 0; i < models.length; i++) {
if (models[i].type == MediaAttachmentType.image) {
children.add(ImageAttachmentDisplay(model: models[i]));
} else {
children.add(Text("media-not-supported".i18n()));
}
}
return Column(
children: children,
);
}
}
class ImageAttachmentDisplay extends StatelessWidget {
const ImageAttachmentDisplay({required this.model, Key? key})
: super(key: key);
final MediaAttachmentModel model;
@override
Widget build(BuildContext context) {
return Column(children: [
Image.network(model.url),
]);
}
}

View File

@ -1,6 +1,8 @@
import 'package:flutter/material.dart';
import 'package:localization/localization.dart';
import 'package:slothmu/business_logic/account/account.dart';
import 'package:slothmu/business_logic/timeline/media.dart';
import 'package:slothmu/partials/media_attachment.dart';
import '../business_logic/timeline/timeline.dart' as tl;
class Post extends StatefulWidget {
@ -21,6 +23,7 @@ class _PostState extends State<Post> {
sensitive: widget.model.sensitive,
content: widget.model.content,
spoilerText: widget.model.spoilerText,
media: widget.model.attachments,
),
postActionBar(context),
],
@ -90,11 +93,13 @@ class PostBody extends StatefulWidget {
required this.sensitive,
required this.spoilerText,
required this.content,
required this.media,
Key? key,
}) : super(key: key);
final String content;
final String spoilerText;
final bool sensitive;
final List<MediaAttachmentModel> media;
@override
State<PostBody> createState() => _PostBodyState();
@ -139,11 +144,16 @@ class _PostBodyState extends State<PostBody> {
),
Visibility(
visible: visible,
child: RichText(
textAlign: TextAlign.start,
text: TextSpan(
style: Theme.of(context).textTheme.bodyMedium,
text: widget.content),
child: Column(
children: [
RichText(
textAlign: TextAlign.start,
text: TextSpan(
style: Theme.of(context).textTheme.bodyMedium,
text: widget.content),
),
MediaAttachments(models: widget.media),
],
),
),
],