ResponsiveImage class

A responsive image widget that displays different images based on the current screen type, optimizing memory usage and visual quality.

Performance Design

  • Only one ImageProvider is resolved per build (no unused images loaded)
  • Uses Image widget with gaplessPlayback: true for smooth transitions
  • Optional cacheWidth/cacheHeight via autoOptimize for memory savings
  • Fallback chain: desktop → tablet → mobile ensures no null images

Example

ResponsiveImage(
  mobile: AssetImage('assets/banner_sm.webp'),
  tablet: AssetImage('assets/banner_md.webp'),
  desktop: AssetImage('assets/banner_lg.webp'),
  fit: BoxFit.cover,
  height: 200.s,
)
Inheritance

Constructors

ResponsiveImage({Key? key, required ImageProvider<Object> mobile, ImageProvider<Object>? tablet, ImageProvider<Object>? desktop, BoxFit fit = BoxFit.cover, double? width, double? height, Alignment alignment = Alignment.center, Widget? placeholder, Widget? errorWidget, bool autoOptimize = false, BorderRadius? borderRadius, Color? color, BlendMode? colorBlendMode, String? semanticLabel})
Creates a ResponsiveImage.
const

Properties

alignment → Alignment
Alignment within the container. Default: Alignment.center.
final
autoOptimize → bool
Whether to automatically resize the decoded image to match the display size, reducing memory usage. Default: false.
final
borderRadius → BorderRadius?
Border radius for the image. If null, no clipping.
final
color → Color?
Color filter to apply on the image (e.g., for overlays).
final
colorBlendMode → BlendMode?
Blend mode for color. Default: BlendMode.srcIn.
final
desktop → ImageProvider<Object>?
Optional image for desktop/large desktop. Falls back to tablet → mobile.
final
errorWidget → Widget?
Widget to show if the image fails to load.
final
fit → BoxFit
How to inscribe the image into the space. Default: BoxFit.cover.
final
hashCode → int
The hash code for this object.
no setterinherited
height → double?
Optional fixed height for the image container.
final
key → Key?
Controls how one widget replaces another widget in the tree.
finalinherited
mobile → ImageProvider<Object>
The image to display on mobile/watch screens. Required as the fallback.
final
placeholder → Widget?
Widget to show while the image is loading.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
semanticLabel → String?
Semantic label for accessibility.
final
tablet → ImageProvider<Object>?
Optional image for tablet screens. Falls back to mobile.
final
width → double?
Optional fixed width for the image container.
final

Methods

build(BuildContext context) → Widget
Describes the part of the user interface represented by this widget.
override
createElement() → StatelessElement
Creates a StatelessElement to manage this widget's location in the tree.
inherited
debugDescribeChildren() → List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) → DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) → String
A string representation of this object.
inherited
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug, int wrapWidth = 65}) → String
Returns a string representation of this node and its descendants.
inherited
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) → String
Returns a one-line detailed description of the object.
inherited
toStringShort() → String
A short, textual description of this widget.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited