add offline attachment rendering

This commit is contained in:
Salvatore Giordano
2020-03-12 10:30:07 +01:00
parent fe5adc72dc
commit 6f696c41ed
2 changed files with 112 additions and 86 deletions
+5
View File
@@ -573,6 +573,10 @@ class _MessageInputState extends State<MessageInput> {
file = await FilePicker.getFile(type: type);
}
if (file == null) {
return;
}
final channel = StreamChannel.of(context).channel;
final bytes = await file.readAsBytes();
@@ -717,6 +721,7 @@ class _MessageInputState extends State<MessageInput> {
imageUrl: attachment.type == FileType.IMAGE ? attachment.url : null,
assetUrl: attachment.url,
type: type,
localUri: attachment.file.uri,
);
});
}
+49 -28
View File
@@ -1,3 +1,4 @@
import 'dart:io';
import 'dart:math';
import 'package:cached_network_image/cached_network_image.dart';
@@ -450,22 +451,8 @@ class _MessageWidgetState extends State<MessageWidget>
);
}
Padding _buildMessageText(
int nOfAttachmentWidgets,
String text,
BuildContext context,
) {
return Padding(
padding: EdgeInsets.only(
right: _isMyMessage ? 0.0 : 8.0,
left: _isMyMessage ? 8.0 : 0.0,
),
child: Container(
decoration:
_buildBoxDecoration(_isLastUser || nOfAttachmentWidgets > 0),
padding: EdgeInsets.all(10),
constraints: BoxConstraints.loose(Size.fromWidth(300)),
child: Column(
Widget _buildSendingError(Widget child) {
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
@@ -485,12 +472,32 @@ class _MessageWidgetState extends State<MessageWidget>
fontSize: 11,
),
),
child,
],
);
}
Padding _buildMessageText(
int nOfAttachmentWidgets,
String text,
BuildContext context,
) {
return Padding(
padding: EdgeInsets.only(
right: _isMyMessage ? 0.0 : 8.0,
left: _isMyMessage ? 8.0 : 0.0,
),
child: Container(
decoration:
_buildBoxDecoration(_isLastUser || nOfAttachmentWidgets > 0),
padding: EdgeInsets.all(10),
constraints: BoxConstraints.loose(Size.fromWidth(300)),
child: _buildSendingError(
MarkdownBody(
data: text,
onTapLink: (link) {
if (link.startsWith('@')) {
final mentionedUser =
widget.message.mentionedUsers.firstWhere(
final mentionedUser = widget.message.mentionedUsers.firstWhere(
(u) => '@${u.name.replaceAll(' ', '')}' == link,
orElse: () => null,
);
@@ -520,7 +527,6 @@ class _MessageWidgetState extends State<MessageWidget>
p: _messageTheme.messageText,
),
),
],
),
),
);
@@ -896,7 +902,12 @@ class _MessageWidgetState extends State<MessageWidget>
Widget _buildImage(
Attachment attachment,
) {
return Hero(
tag: attachment.imageUrl ?? attachment.assetUrl ?? attachment.thumbUrl,
child: CachedNetworkImage(
imageBuilder: (context, provider) {
return GestureDetector(
child: Image(image: provider),
onTap: () {
print(attachment.toJson());
Navigator.push(context, MaterialPageRoute(builder: (_) {
@@ -907,9 +918,8 @@ class _MessageWidgetState extends State<MessageWidget>
);
}));
},
child: Hero(
tag: attachment.imageUrl ?? attachment.assetUrl ?? attachment.thumbUrl,
child: CachedNetworkImage(
);
},
placeholder: (_, __) {
return Container(
width: 200,
@@ -918,8 +928,20 @@ class _MessageWidgetState extends State<MessageWidget>
},
imageUrl:
attachment.thumbUrl ?? attachment.imageUrl ?? attachment.assetUrl,
errorWidget: (context, url, error) {
return Container(
errorWidget: (context, url, error) => _buildErrorImage(attachment),
fit: BoxFit.cover,
),
);
}
Widget _buildErrorImage(Attachment attachment) {
if (attachment.localUri != null) {
return Image.file(
File(attachment.localUri.path),
);
}
return Center(
child: Container(
width: 200,
height: 140,
color: Color(0xffd0021B).withAlpha(26),
@@ -929,10 +951,6 @@ class _MessageWidgetState extends State<MessageWidget>
color: Colors.white,
),
),
);
},
fit: BoxFit.cover,
),
),
);
}
@@ -956,6 +974,9 @@ class _MessageWidgetState extends State<MessageWidget>
videoPlayerController: videoController,
autoInitialize: true,
errorBuilder: (_, e) {
if (attachment.thumbUrl == null) {
return _buildErrorImage(attachment);
}
return Stack(
children: <Widget>[
Container(