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); file = await FilePicker.getFile(type: type);
} }
if (file == null) {
return;
}
final channel = StreamChannel.of(context).channel; final channel = StreamChannel.of(context).channel;
final bytes = await file.readAsBytes(); final bytes = await file.readAsBytes();
@@ -717,6 +721,7 @@ class _MessageInputState extends State<MessageInput> {
imageUrl: attachment.type == FileType.IMAGE ? attachment.url : null, imageUrl: attachment.type == FileType.IMAGE ? attachment.url : null,
assetUrl: attachment.url, assetUrl: attachment.url,
type: type, type: type,
localUri: attachment.file.uri,
); );
}); });
} }
+49 -28
View File
@@ -1,3 +1,4 @@
import 'dart:io';
import 'dart:math'; import 'dart:math';
import 'package:cached_network_image/cached_network_image.dart'; import 'package:cached_network_image/cached_network_image.dart';
@@ -450,22 +451,8 @@ class _MessageWidgetState extends State<MessageWidget>
); );
} }
Padding _buildMessageText( Widget _buildSendingError(Widget child) {
int nOfAttachmentWidgets, return Column(
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(
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[ children: <Widget>[
@@ -485,12 +472,32 @@ class _MessageWidgetState extends State<MessageWidget>
fontSize: 11, 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( MarkdownBody(
data: text, data: text,
onTapLink: (link) { onTapLink: (link) {
if (link.startsWith('@')) { if (link.startsWith('@')) {
final mentionedUser = final mentionedUser = widget.message.mentionedUsers.firstWhere(
widget.message.mentionedUsers.firstWhere(
(u) => '@${u.name.replaceAll(' ', '')}' == link, (u) => '@${u.name.replaceAll(' ', '')}' == link,
orElse: () => null, orElse: () => null,
); );
@@ -520,7 +527,6 @@ class _MessageWidgetState extends State<MessageWidget>
p: _messageTheme.messageText, p: _messageTheme.messageText,
), ),
), ),
],
), ),
), ),
); );
@@ -896,7 +902,12 @@ class _MessageWidgetState extends State<MessageWidget>
Widget _buildImage( Widget _buildImage(
Attachment attachment, Attachment attachment,
) { ) {
return Hero(
tag: attachment.imageUrl ?? attachment.assetUrl ?? attachment.thumbUrl,
child: CachedNetworkImage(
imageBuilder: (context, provider) {
return GestureDetector( return GestureDetector(
child: Image(image: provider),
onTap: () { onTap: () {
print(attachment.toJson()); print(attachment.toJson());
Navigator.push(context, MaterialPageRoute(builder: (_) { 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: (_, __) { placeholder: (_, __) {
return Container( return Container(
width: 200, width: 200,
@@ -918,8 +928,20 @@ class _MessageWidgetState extends State<MessageWidget>
}, },
imageUrl: imageUrl:
attachment.thumbUrl ?? attachment.imageUrl ?? attachment.assetUrl, attachment.thumbUrl ?? attachment.imageUrl ?? attachment.assetUrl,
errorWidget: (context, url, error) { errorWidget: (context, url, error) => _buildErrorImage(attachment),
return Container( fit: BoxFit.cover,
),
);
}
Widget _buildErrorImage(Attachment attachment) {
if (attachment.localUri != null) {
return Image.file(
File(attachment.localUri.path),
);
}
return Center(
child: Container(
width: 200, width: 200,
height: 140, height: 140,
color: Color(0xffd0021B).withAlpha(26), color: Color(0xffd0021B).withAlpha(26),
@@ -929,10 +951,6 @@ class _MessageWidgetState extends State<MessageWidget>
color: Colors.white, color: Colors.white,
), ),
), ),
);
},
fit: BoxFit.cover,
),
), ),
); );
} }
@@ -956,6 +974,9 @@ class _MessageWidgetState extends State<MessageWidget>
videoPlayerController: videoController, videoPlayerController: videoController,
autoInitialize: true, autoInitialize: true,
errorBuilder: (_, e) { errorBuilder: (_, e) {
if (attachment.thumbUrl == null) {
return _buildErrorImage(attachment);
}
return Stack( return Stack(
children: <Widget>[ children: <Widget>[
Container( Container(