1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 12:59:21 +00:00
comunicmobile/lib/ui/widgets/conversation_file_tile.dart

131 lines
3.8 KiB
Dart
Raw Permalink Normal View History

2021-03-13 07:33:55 +00:00
import 'package:cached_network_image/cached_network_image.dart';
2021-03-10 23:02:41 +00:00
/// Chat file tile
///
/// @author Pierre Hubert
import 'package:comunic/models/conversation_message.dart';
import 'package:comunic/ui/dialogs/audio_player_dialog.dart';
2021-03-11 19:31:06 +00:00
import 'package:comunic/ui/routes/main_route/main_route.dart';
import 'package:comunic/ui/routes/video_player_route.dart';
2021-03-10 23:02:41 +00:00
import 'package:comunic/ui/widgets/network_image_widget.dart';
2021-03-11 16:04:18 +00:00
import 'package:filesize/filesize.dart';
2021-03-10 23:02:41 +00:00
import 'package:flutter/material.dart';
2022-06-11 13:04:11 +00:00
import 'package:url_launcher/url_launcher_string.dart';
2021-03-10 23:02:41 +00:00
2021-03-13 07:33:55 +00:00
const _AreaWidth = 150.0;
const _AreaHeight = 100.0;
2021-03-10 23:02:41 +00:00
class ConversationFileWidget extends StatefulWidget {
final int messageID;
final ConversationMessageFile file;
const ConversationFileWidget({
Key? key,
required this.messageID,
required this.file,
2022-03-12 09:57:11 +00:00
}) : super(key: key);
2021-03-10 23:02:41 +00:00
@override
_ConversationFileWidgetState createState() => _ConversationFileWidgetState();
}
class _ConversationFileWidgetState extends State<ConversationFileWidget> {
ConversationMessageFile get file => widget.file;
2022-03-12 09:57:11 +00:00
String get _thumbCacheKey => "conv-msg-thumb-${widget.messageID}";
String get _fileCacheKey => "conv-msg-file-${widget.messageID}";
2021-03-10 23:02:41 +00:00
@override
2021-03-13 07:33:55 +00:00
Widget build(BuildContext context) => Stack(
children: [
2021-03-13 07:37:59 +00:00
!file.hasThumbnail ||
file.fileType == ConversationMessageFileType.IMAGE
? Container(
width: 0,
)
2021-03-13 07:33:55 +00:00
: Opacity(
opacity: 0.8,
child: CachedNetworkImage(
imageUrl: file.thumbnail!,
2022-03-12 09:57:11 +00:00
cacheKey: _thumbCacheKey,
2021-03-13 07:33:55 +00:00
width: _AreaWidth,
height: _AreaHeight,
fit: BoxFit.cover,
),
),
Container(
width: _AreaWidth,
height: _AreaHeight,
child: _buildContent(),
)
],
2021-03-10 23:23:11 +00:00
);
2021-03-10 23:02:41 +00:00
2021-03-10 23:13:05 +00:00
Widget _buildContent() {
2021-03-10 23:02:41 +00:00
switch (file.fileType) {
// Images
case ConversationMessageFileType.IMAGE:
return Center(
child: NetworkImageWidget(
url: file.url!,
2022-03-12 09:57:11 +00:00
cacheKey: _fileCacheKey,
2021-03-10 23:02:41 +00:00
thumbnailURL: file.thumbnail,
2022-03-12 09:57:11 +00:00
thumbnailCacheKey: _thumbCacheKey,
2021-03-10 23:02:41 +00:00
allowFullScreen: true,
),
);
// We open it in the browser
2021-03-10 23:02:41 +00:00
default:
2021-03-10 23:23:11 +00:00
return Container(
child: Center(
child: MaterialButton(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Spacer(flex: 2),
Icon(file.icon, color: Colors.white),
Spacer(),
2021-03-13 07:33:55 +00:00
Text(
file.name!.length < 23
? file.name!
: file.name!.substring(0, 20) + "...",
2021-03-13 07:40:37 +00:00
textAlign: TextAlign.center,
style: TextStyle(color: Colors.white),
),
2021-03-11 16:04:18 +00:00
Spacer(),
Text(
filesize(file.size),
2021-03-13 07:40:37 +00:00
style: TextStyle(fontSize: 10, color: Colors.white),
2021-03-11 16:04:18 +00:00
),
2021-03-10 23:23:11 +00:00
Spacer(flex: 2),
],
),
onPressed: _openFile,
2021-03-10 23:02:41 +00:00
),
),
);
}
}
void _openFile() {
switch (file.fileType) {
case ConversationMessageFileType.AUDIO:
showAudioPlayerDialog(context, file.url);
break;
2021-03-11 19:31:06 +00:00
case ConversationMessageFileType.VIDEO:
MainController.of(context)!.push(
VideoPlayerRoute(url: file.url!),
2021-03-11 19:31:06 +00:00
hideNavBar: true,
canShowAsDialog: true,
);
break;
default:
2022-06-11 13:04:11 +00:00
launchUrlString(file.url!);
}
}
2021-03-10 23:02:41 +00:00
}