Skip to content

Commit a9c7f70

Browse files
content [nfc]: Pull out MessageMediaPreview widget
1 parent 03fb937 commit a9c7f70

File tree

1 file changed

+74
-76
lines changed

1 file changed

+74
-76
lines changed

lib/widgets/content.dart

Lines changed: 74 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -380,30 +380,17 @@ class MessageImage extends StatelessWidget {
380380
final resolvedSrc = store.tryResolveUrl(src);
381381
// TODO if src fails to parse, show an explicit "broken image"
382382

383-
return GestureDetector(
383+
return MessageMediaPreview(
384384
onTap: resolvedSrc == null ? null : () { // TODO(log)
385385
Navigator.of(context).push(getLightboxRoute(
386386
context: context, message: message, src: resolvedSrc));
387387
},
388-
child: UnconstrainedBox(
389-
alignment: Alignment.centerLeft,
390-
child: Padding(
391-
// TODO clean up this padding by imitating web less precisely;
392-
// in particular, avoid adding loose whitespace at end of message.
393-
padding: const EdgeInsets.only(right: 5, bottom: 5),
394-
child: ColoredBox(
395-
color: const Color.fromRGBO(0, 0, 0, 0.03),
396-
child: Padding(
397-
padding: const EdgeInsets.all(1),
398-
child: SizedBox(
399-
height: 100,
400-
width: 150,
401-
child: resolvedSrc == null ? null : LightboxHero(
402-
message: message,
403-
src: resolvedSrc,
404-
child: RealmContentNetworkImage(
405-
resolvedSrc,
406-
filterQuality: FilterQuality.medium))))))));
388+
child: resolvedSrc == null ? null : LightboxHero(
389+
message: message,
390+
src: resolvedSrc,
391+
child: RealmContentNetworkImage(
392+
resolvedSrc,
393+
filterQuality: FilterQuality.medium)));
407394
}
408395
}
409396

@@ -435,7 +422,7 @@ class _MessageInlineVideoState extends State<MessageInlineVideo> {
435422
Future<void> _initialize() async {
436423
try {
437424
final store = PerAccountStoreWidget.of(context);
438-
// _resolvedSrcUrl = store.tryResolveUrl(widget.node.srcUrl);
425+
_resolvedSrcUrl = store.tryResolveUrl(widget.node.srcUrl);
439426
if (_resolvedSrcUrl == null) {
440427
return; // TODO(log)
441428
}
@@ -480,7 +467,7 @@ class _MessageInlineVideoState extends State<MessageInlineVideo> {
480467
Widget build(BuildContext context) {
481468
final message = InheritedMessage.of(context);
482469

483-
return GestureDetector(
470+
return MessageMediaPreview(
484471
onTap: !_didAttemptInitialization
485472
? null
486473
: () { // TODO(log)
@@ -503,43 +490,33 @@ class _MessageInlineVideoState extends State<MessageInlineVideo> {
503490
));
504491
}
505492
},
506-
child: UnconstrainedBox(
507-
alignment: Alignment.centerLeft,
508-
child: Padding(
509-
// TODO clean up this padding by imitating web less precisely;
510-
// in particular, avoid adding loose whitespace at end of message.
511-
padding: const EdgeInsets.only(right: 5, bottom: 5),
512-
child: Container(
513-
height: 100,
514-
width: 150,
515-
color: Colors.black,
516-
child: !_didAttemptInitialization
517-
? Container(
518-
color: Colors.black,
519-
child: const Center(
520-
child: SizedBox(
521-
height: 16,
522-
width: 16,
523-
child: CircularProgressIndicator(
524-
color: Colors.white,
525-
strokeWidth: 2))))
526-
: Stack(
527-
alignment: Alignment.center,
528-
children: [
529-
if (_resolvedSrcUrl == null || _controller!.value.hasError)
530-
Container(color: Colors.black)
531-
else
532-
LightboxHero(
533-
message: message,
534-
src: _resolvedSrcUrl!,
535-
child: AspectRatio(
536-
aspectRatio: _controller!.value.aspectRatio,
537-
child: VideoPlayer(_controller!))),
538-
const Icon(
539-
Icons.play_arrow_rounded,
540-
color: Colors.white,
541-
size: 32)
542-
])))));
493+
child: !_didAttemptInitialization
494+
? Container(
495+
color: Colors.black,
496+
child: const Center(
497+
child: SizedBox(
498+
height: 16,
499+
width: 16,
500+
child: CircularProgressIndicator(
501+
color: Colors.white,
502+
strokeWidth: 2))))
503+
: Stack(
504+
alignment: Alignment.center,
505+
children: [
506+
if (_resolvedSrcUrl == null || _controller!.value.hasError)
507+
Container(color: Colors.black)
508+
else
509+
LightboxHero(
510+
message: message,
511+
src: _resolvedSrcUrl!,
512+
child: AspectRatio(
513+
aspectRatio: _controller!.value.aspectRatio,
514+
child: VideoPlayer(_controller!))),
515+
const Icon(
516+
Icons.play_arrow_rounded,
517+
color: Colors.white,
518+
size: 32)
519+
]));
543520
}
544521
}
545522

@@ -555,30 +532,51 @@ class MessageEmbedVideo extends StatelessWidget {
555532
final store = PerAccountStoreWidget.of(context);
556533
final previewImageSrcUrl = store.tryResolveUrl(node.previewImageSrcUrl);
557534

558-
return GestureDetector(
535+
return MessageMediaPreview(
559536
onTap: () => _launchUrl(context, node.hrefUrl),
537+
child: Stack(
538+
alignment: Alignment.center,
539+
children: [
540+
if (previewImageSrcUrl != null)
541+
RealmContentNetworkImage(
542+
previewImageSrcUrl,
543+
filterQuality: FilterQuality.medium),
544+
const Icon(
545+
Icons.play_arrow_rounded,
546+
color: Colors.white,
547+
size: 32),
548+
]));
549+
}
550+
}
551+
552+
class MessageMediaPreview extends StatelessWidget {
553+
const MessageMediaPreview({
554+
super.key,
555+
required this.onTap,
556+
required this.child,
557+
});
558+
559+
final void Function()? onTap;
560+
final Widget? child;
561+
562+
@override
563+
Widget build(BuildContext context) {
564+
return GestureDetector(
565+
onTap: onTap,
560566
child: UnconstrainedBox(
561567
alignment: Alignment.centerLeft,
562568
child: Padding(
563569
// TODO clean up this padding by imitating web less precisely;
564570
// in particular, avoid adding loose whitespace at end of message.
565571
padding: const EdgeInsets.only(right: 5, bottom: 5),
566-
child: Container(
567-
height: 100,
568-
width: 150,
569-
color: Colors.black,
570-
child: Stack(
571-
alignment: Alignment.center,
572-
children: [
573-
if (previewImageSrcUrl != null)
574-
RealmContentNetworkImage(
575-
previewImageSrcUrl,
576-
filterQuality: FilterQuality.medium),
577-
const Icon(
578-
Icons.play_arrow_rounded,
579-
color: Colors.white,
580-
size: 32),
581-
])))));
572+
child: ColoredBox(
573+
color: const Color.fromRGBO(0, 0, 0, 0.03),
574+
child: Padding(
575+
padding: const EdgeInsets.all(1),
576+
child: SizedBox(
577+
height: 100,
578+
width: 150,
579+
child: child))))));
582580
}
583581
}
584582

0 commit comments

Comments
 (0)