fluffychat/lib/components/image_bubble.dart

96 lines
2.6 KiB
Dart
Raw Normal View History

2020-04-02 14:05:32 +02:00
import 'package:bubble/bubble.dart';
import 'package:famedlysdk/famedlysdk.dart';
2020-05-16 08:02:33 +02:00
import 'package:fluffychat/utils/app_route.dart';
import 'package:fluffychat/views/image_view.dart';
2020-04-02 14:05:32 +02:00
import 'package:flutter/material.dart';
2020-04-28 14:11:56 +02:00
2020-04-02 14:05:32 +02:00
class ImageBubble extends StatefulWidget {
final Event event;
2020-05-16 08:02:33 +02:00
final bool tapToView;
2020-05-16 09:16:46 +02:00
final BoxFit fit;
2020-05-20 19:29:26 +02:00
final bool maxSize;
final Color backgroundColor;
final double radius;
2020-04-02 14:05:32 +02:00
2020-05-16 09:16:46 +02:00
const ImageBubble(
this.event, {
this.tapToView = true,
2020-05-20 19:29:26 +02:00
this.maxSize = true,
this.backgroundColor,
2020-05-16 09:16:46 +02:00
this.fit = BoxFit.cover,
2020-05-20 19:29:26 +02:00
this.radius = 10.0,
2020-05-16 09:16:46 +02:00
Key key,
}) : super(key: key);
2020-04-02 14:05:32 +02:00
@override
_ImageBubbleState createState() => _ImageBubbleState();
}
class _ImageBubbleState extends State<ImageBubble> {
2020-05-13 15:58:59 +02:00
static final Map<String, MatrixFile> _matrixFileMap = {};
2020-04-09 10:00:18 +02:00
MatrixFile get _file => _matrixFileMap[widget.event.eventId];
set _file(MatrixFile file) {
_matrixFileMap[widget.event.eventId] = file;
}
2020-04-02 14:05:32 +02:00
dynamic _error;
Future<MatrixFile> _getFile() async {
if (_file != null) return _file;
2020-05-07 12:14:26 +02:00
return widget.event
.downloadAndDecryptAttachment(getThumbnail: widget.event.hasThumbnail);
2020-04-02 14:05:32 +02:00
}
@override
Widget build(BuildContext context) {
return Bubble(
padding: BubbleEdges.all(0),
2020-05-20 19:29:26 +02:00
radius: Radius.circular(widget.radius),
color: widget.backgroundColor ?? Theme.of(context).secondaryHeaderColor,
2020-04-02 14:05:32 +02:00
elevation: 0,
child: Container(
2020-05-20 19:29:26 +02:00
height: widget.maxSize ? 300 : null,
width: widget.maxSize ? 400 : null,
2020-04-02 14:05:32 +02:00
child: Builder(
builder: (BuildContext context) {
if (_error != null) {
return Center(
child: Text(
_error.toString(),
),
);
}
if (_file != null) {
return InkWell(
2020-05-16 08:02:33 +02:00
onTap: () {
if (!widget.tapToView) return;
Navigator.of(context).push(
AppRoute(
ImageView(widget.event),
),
2020-04-28 14:11:56 +02:00
);
},
2020-05-16 08:02:33 +02:00
child: Hero(
tag: widget.event.eventId,
child: Image.memory(
_file.bytes,
2020-05-16 09:16:46 +02:00
fit: widget.fit,
2020-05-16 08:02:33 +02:00
),
2020-04-02 14:05:32 +02:00
),
);
}
_getFile().then((MatrixFile file) {
setState(() => _file = file);
2020-05-13 15:58:59 +02:00
}, onError: (error, stacktrace) {
2020-04-02 14:05:32 +02:00
setState(() => _error = error);
});
return Center(
child: CircularProgressIndicator(),
);
},
),
),
);
}
}