fluffychat/lib/components/dialogs/send_file_dialog.dart

104 lines
3.0 KiB
Dart
Raw Normal View History

2020-09-04 10:56:25 +00:00
import 'package:famedlysdk/famedlysdk.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
2020-09-04 10:56:25 +00:00
2020-12-25 08:58:34 +00:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
2020-09-04 10:56:25 +00:00
import '../../utils/matrix_file_extension.dart';
import '../../utils/room_send_file_extension.dart';
import '../../utils/resize_image.dart';
2020-09-04 10:56:25 +00:00
class SendFileDialog extends StatefulWidget {
final Room room;
final MatrixFile file;
2021-01-18 09:43:00 +00:00
const SendFileDialog({
this.room,
this.file,
Key key,
}) : super(key: key);
2020-09-04 10:56:25 +00:00
@override
_SendFileDialogState createState() => _SendFileDialogState();
}
class _SendFileDialogState extends State<SendFileDialog> {
bool origImage = false;
bool _isSending = false;
2020-09-04 10:56:25 +00:00
Future<void> _send() async {
var file = widget.file;
if (file is MatrixImageFile && !origImage) {
try {
file = await resizeImage(file, max: 1600);
2020-09-04 10:56:25 +00:00
} catch (e) {
// couldn't resize
}
}
await widget.room.sendFileEventWithThumbnail(file);
}
@override
Widget build(BuildContext context) {
2021-02-24 11:17:23 +00:00
var sendStr = L10n.of(context).sendFile;
2020-09-04 10:56:25 +00:00
if (widget.file is MatrixImageFile) {
2021-02-24 11:17:23 +00:00
sendStr = L10n.of(context).sendImage;
2020-09-04 10:56:25 +00:00
} else if (widget.file is MatrixAudioFile) {
2021-02-24 11:17:23 +00:00
sendStr = L10n.of(context).sendAudio;
2020-09-04 10:56:25 +00:00
} else if (widget.file is MatrixVideoFile) {
2021-02-24 11:17:23 +00:00
sendStr = L10n.of(context).sendVideo;
2020-09-04 10:56:25 +00:00
}
Widget contentWidget;
if (widget.file is MatrixImageFile) {
contentWidget = Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
Flexible(
child: Image.memory(
widget.file.bytes,
fit: BoxFit.contain,
),
),
Text(widget.file.name),
Row(
children: <Widget>[
Checkbox(
value: origImage,
onChanged: (v) => setState(() => origImage = v),
),
InkWell(
onTap: () => setState(() => origImage = !origImage),
2021-02-24 11:17:23 +00:00
child: Text(L10n.of(context).sendOriginal +
' (${widget.file.sizeString})'),
2020-09-04 10:56:25 +00:00
),
],
)
]);
} else {
contentWidget = Text('${widget.file.name} (${widget.file.sizeString})');
}
return AlertDialog(
title: Text(sendStr),
content: contentWidget,
actions: <Widget>[
2021-02-27 06:53:34 +00:00
TextButton(
2021-02-24 11:17:23 +00:00
child: Text(L10n.of(context).cancel),
2020-09-04 10:56:25 +00:00
onPressed: () {
// just close the dialog
2021-02-24 11:17:23 +00:00
Navigator.of(context, rootNavigator: false).pop();
2020-09-04 10:56:25 +00:00
},
),
2021-02-27 06:53:34 +00:00
TextButton(
2021-02-24 11:17:23 +00:00
child: Text(L10n.of(context).send),
onPressed: _isSending
? null
: () async {
setState(() {
_isSending = true;
});
2020-12-25 08:58:34 +00:00
await showFutureLoadingDialog(
context: context, future: () => _send());
2021-02-24 11:17:23 +00:00
await Navigator.of(context, rootNavigator: false).pop();
},
2020-09-04 10:56:25 +00:00
),
],
);
}
}