Enhance image handling in FileViewerDispatch for web and mobile, adding error handling and CORS support
This commit is contained in:
@@ -104,28 +104,47 @@ class FileViewerDispatch {
|
||||
),
|
||||
);
|
||||
} else if (mimeType.startsWith('image/')) {
|
||||
Widget child;
|
||||
if (kIsWeb) {
|
||||
// Use HTML img element for web to handle CORS
|
||||
ui_web.platformViewRegistry.registerViewFactory(viewerId, (int viewId) {
|
||||
final imgElement = web.HTMLImageElement()
|
||||
..src = url
|
||||
..style.width = '100%'
|
||||
..style.height = '100%'
|
||||
..style.objectFit = 'contain'
|
||||
..crossOrigin = 'anonymous';
|
||||
|
||||
imgElement.onError.listen((event) {
|
||||
// Handle error
|
||||
});
|
||||
|
||||
return imgElement;
|
||||
});
|
||||
|
||||
child = HtmlElementView(viewType: viewerId);
|
||||
} else {
|
||||
// For mobile, use Image.network
|
||||
child = Image.network(
|
||||
url,
|
||||
headers: headers,
|
||||
fit: BoxFit.contain,
|
||||
errorBuilder: (context, error, stackTrace) {
|
||||
return Center(
|
||||
child: Text(
|
||||
'Failed to load image',
|
||||
style: TextStyle(color: Colors.red[400]),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
return Container(
|
||||
color: AppTheme.primaryBackground,
|
||||
child: InteractiveViewer(
|
||||
minScale: 0.5,
|
||||
maxScale: 4.0,
|
||||
child: Image.network(
|
||||
url,
|
||||
headers: headers,
|
||||
fit: BoxFit.contain,
|
||||
errorBuilder: (context, error, stackTrace) {
|
||||
return Center(
|
||||
child: Text(
|
||||
'Failed to load image',
|
||||
style: TextStyle(color: Colors.red[400]),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
child: InteractiveViewer(minScale: 0.5, maxScale: 4.0, child: child),
|
||||
);
|
||||
} else {
|
||||
// Fallback
|
||||
return Container(
|
||||
color: AppTheme.primaryBackground,
|
||||
child: Center(
|
||||
|
||||
Reference in New Issue
Block a user