Handle image load states (loading, completed, failed)
masterYou can customize the UI shown during different stages of an image's lifecycle using the loadStateChanged callback.
Key Concepts
- States: The
LoadStateenum providesloading,completed, andfailedstates. - Enabling States: For network images,
enableLoadStateistrueby default. For other sources (like local assets), you must manually setenableLoadState: trueif the image takes time to load. - Overriding the Completed State:
- To add decorations (like animations) without losing gestures or editor functionality, use
state.completedWidget. - To override size or
sourceRectat the completed state, useExtendedRawImagewith the providedstate.extendedImageInfo?.image.
- To add decorations (like animations) without losing gestures or editor functionality, use
- Reloading: If an image fails to load, you can trigger a retry by calling
state.reLoadImage()within thefailedstate handler.
ExtendedImage.network(
url,
loadStateChanged: (ExtendedImageState state) {
switch (state.extendedImageLoadState) {
case LoadState.loading:
return CircularProgressIndicator();
case LoadState.completed:
// Use state.completedWidget to preserve gestures/editor
return state.completedWidget;
case LoadState.failed:
return GestureDetector(
onTap: () => state.reLoadImage(),
child: Icon(Icons.error),
);
}
},
)