refactor: Use dart blurhash
This commit is contained in:
parent
1271441eb0
commit
25ec229ace
5 changed files with 67 additions and 22 deletions
|
|
@ -1,11 +1,11 @@
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:flutter_blurhash/flutter_blurhash.dart';
|
||||
import 'package:matrix/matrix.dart';
|
||||
|
||||
import 'package:fluffychat/config/app_config.dart';
|
||||
import 'package:fluffychat/pages/image_viewer/image_viewer.dart';
|
||||
import 'package:fluffychat/widgets/mxc_image.dart';
|
||||
import '../../../widgets/blur_hash.dart';
|
||||
|
||||
class ImageBubble extends StatelessWidget {
|
||||
final Event event;
|
||||
|
|
@ -40,15 +40,11 @@ class ImageBubble extends StatelessWidget {
|
|||
event.infoMap['xyz.amorgan.blurhash'] is String
|
||||
? event.infoMap['xyz.amorgan.blurhash']
|
||||
: 'LEHV6nWB2yk8pyo0adR*.7kCMdnj';
|
||||
return SizedBox(
|
||||
return BlurHash(
|
||||
blurhash: blurHashString,
|
||||
width: width,
|
||||
height: height,
|
||||
child: BlurHash(
|
||||
hash: blurHashString,
|
||||
decodingWidth: width.round(),
|
||||
decodingHeight: height.round(),
|
||||
imageFit: fit,
|
||||
),
|
||||
fit: fit,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ import 'package:flutter/foundation.dart';
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:chewie/chewie.dart';
|
||||
import 'package:flutter_blurhash/flutter_blurhash.dart';
|
||||
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||
import 'package:matrix/matrix.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
|
@ -14,6 +13,7 @@ import 'package:video_player/video_player.dart';
|
|||
import 'package:fluffychat/pages/chat/events/image_bubble.dart';
|
||||
import 'package:fluffychat/utils/localized_exception_extension.dart';
|
||||
import 'package:fluffychat/utils/matrix_sdk_extensions/event_extension.dart';
|
||||
import 'package:fluffychat/widgets/blur_hash.dart';
|
||||
import '../../../utils/error_reporter.dart';
|
||||
|
||||
class EventVideoPlayer extends StatefulWidget {
|
||||
|
|
@ -112,7 +112,7 @@ class EventVideoPlayerState extends State<EventVideoPlayer> {
|
|||
),
|
||||
)
|
||||
else
|
||||
BlurHash(hash: blurHash),
|
||||
BlurHash(blurhash: blurHash, width: 300, height: 300),
|
||||
Center(
|
||||
child: OutlinedButton.icon(
|
||||
style: OutlinedButton.styleFrom(
|
||||
|
|
|
|||
57
lib/widgets/blur_hash.dart
Normal file
57
lib/widgets/blur_hash.dart
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:blurhash_dart/blurhash_dart.dart' as b;
|
||||
import 'package:image/image.dart' as image;
|
||||
|
||||
class BlurHash extends StatefulWidget {
|
||||
final double width;
|
||||
final double height;
|
||||
final String blurhash;
|
||||
final BoxFit fit;
|
||||
|
||||
const BlurHash({
|
||||
super.key,
|
||||
String? blurhash,
|
||||
required this.width,
|
||||
required this.height,
|
||||
this.fit = BoxFit.cover,
|
||||
}) : blurhash = blurhash ?? 'LEHV6nWB2yk8pyo0adR*.7kCMdnj';
|
||||
|
||||
@override
|
||||
State<BlurHash> createState() => _BlurHashState();
|
||||
}
|
||||
|
||||
class _BlurHashState extends State<BlurHash> {
|
||||
Uint8List? _data;
|
||||
|
||||
Future<Uint8List> getBlurhashData() async {
|
||||
final blurhash = b.BlurHash.decode(widget.blurhash);
|
||||
final img = blurhash.toImage(widget.width.round(), widget.height.round());
|
||||
return _data ??= Uint8List.fromList(image.encodePng(img));
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return FutureBuilder<Uint8List>(
|
||||
future: getBlurhashData(),
|
||||
builder: (context, snapshot) {
|
||||
final data = snapshot.data;
|
||||
if (data == null) {
|
||||
return Container(
|
||||
width: widget.width,
|
||||
height: widget.height,
|
||||
color: Theme.of(context).colorScheme.surface,
|
||||
);
|
||||
}
|
||||
return Image.memory(
|
||||
data,
|
||||
fit: widget.fit,
|
||||
width: widget.width,
|
||||
height: widget.height,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue