import 'package:flutter/material.dart'; import 'package:yumi/ui_kit/components/sc_compontent.dart'; import 'package:yumi/ui_kit/widgets/svga/sc_network_svga_widget.dart'; class RoomEmojiAssetImage extends StatefulWidget { const RoomEmojiAssetImage({ super.key, required this.asset, required this.width, required this.height, this.fit = BoxFit.contain, }); final String asset; final double width; final double height; final BoxFit fit; @override State createState() => _RoomEmojiAssetImageState(); } class _RoomEmojiAssetImageState extends State with AutomaticKeepAliveClientMixin { AssetImage? _imageProvider; @override void initState() { super.initState(); _syncImageProvider(); } @override void didChangeDependencies() { super.didChangeDependencies(); _precache(); } @override void didUpdateWidget(covariant RoomEmojiAssetImage oldWidget) { super.didUpdateWidget(oldWidget); if (oldWidget.asset != widget.asset) { _syncImageProvider(); _precache(); } } @override Widget build(BuildContext context) { super.build(context); final resource = widget.asset.trim(); if (resource.isEmpty) { return SizedBox(width: widget.width, height: widget.height); } if (SCNetworkSvgaWidget.isSvga(resource)) { return RepaintBoundary( child: SCNetworkSvgaWidget( resource: resource, width: widget.width, height: widget.height, loop: true, fit: widget.fit, ), ); } if (!_isLocalAsset(resource)) { return RepaintBoundary( child: netImage( url: resource, width: widget.width, height: widget.height, fit: widget.fit, gifFit: widget.fit, noDefaultImg: true, loadingWidget: SizedBox(width: widget.width, height: widget.height), errorWidget: SizedBox(width: widget.width, height: widget.height), ), ); } final imageProvider = _imageProvider; if (imageProvider == null) { return SizedBox(width: widget.width, height: widget.height); } return RepaintBoundary( child: Image( image: imageProvider, width: widget.width, height: widget.height, fit: widget.fit, gaplessPlayback: true, filterQuality: FilterQuality.low, ), ); } void _precache() { final imageProvider = _imageProvider; if (imageProvider != null) { precacheImage(imageProvider, context); } } void _syncImageProvider() { final resource = widget.asset.trim(); _imageProvider = _isLocalAsset(resource) ? AssetImage(resource) : null; } bool _isLocalAsset(String resource) { return resource.startsWith("assets/") || resource.startsWith("sc_images/"); } @override bool get wantKeepAlive => true; }