AssetMapBitmap class

Represents a BitmapDescriptor that is created from an asset image.

This class extends BitmapDescriptor to support loading images from assets and mipmaps. It allows resolving the assets that are optimized for the device's screen resolution and pixel density.

Use AssetMapBitmap.create as the default method for generating instances of this class. It dynamically resolves the correct asset version based on the device's pixel ratio, ensuring optimal resolution without manual configuration. See https://flutter.dev/to/resolution-aware-images for more information on resolution-aware assets.

Note that it's important to either provide high-resolution assets to gain sharp images on high-density screens or set the imagePixelRatio, width or height values to control the render size.

Following example demonstrates how to create an AssetMapBitmap using asset resolving:

Future<void> _getAssetMapBitmap(BuildContext context) async {
  final ImageConfiguration imageConfiguration = createLocalImageConfiguration(
    context,
  AssetMapBitmap assetMapBitmap = await AssetMapBitmap.create(
    imageConfiguration,
    'assets/images/map_icon.png',
  );
  return assetMapBitmap;
}

Optionally, width and height can be specified to control the dimensions of the rendered image:

  • If both width and height are non-null, the image will have the specified dimensions, which might distort the original aspect ratio, similar to BoxFit.fill.
  • If only one of width and height is non-null, then the output image will be scaled to the associated width or height, and the other dimension will take whatever value is needed to maintain the image's original aspect ratio. These cases are similar to BoxFit.fitWidth and BoxFit.fitHeight, respectively.
Future<void> _getAssetMapBitmap(BuildContext context) async {
  final ImageConfiguration imageConfiguration = createLocalImageConfiguration(
    context,
   );
  // Render the image at exact size of 64x64 logical pixels.
  AssetMapBitmap assetMapBitmap = await AssetMapBitmap.create(
    imageConfiguration,
    'assets/images/map_icon.png',
    width: 64, // Desired width in logical pixels.
    height: 64, // Desired height in logical pixels.
  );
  return assetMapBitmap;
}

The following example demonstrates how to create an AssetMapBitmap from an asset image without automatic mipmap resolving:

AssetMapBitmap assetMapBitmap = AssetMapBitmap(
  'assets/images/map_icon.png',
);

To render the bitmap as sharply as possible, set the imagePixelRatio to the device's pixel ratio. This renders the asset at a pixel-to-pixel ratio on the screen, but may result in different logical marker sizes across devices with varying pixel densities.

AssetMapBitmap assetMapBitmap = AssetMapBitmap(
  'assets/images/map_icon.png',
  imagePixelRatio: MediaQuery.maybeDevicePixelRatioOf(context),
);
Inheritance

Constructors

AssetMapBitmap(String assetName, {MapBitmapScaling bitmapScaling = MapBitmapScaling.auto, double? imagePixelRatio, double? width, double? height})
Creates a AssetMapBitmap from an asset image.

Properties

assetName String
The name of the asset.
final
bitmapScaling MapBitmapScaling
The scaling method of the bitmap.
finalinherited
hashCode int
The hash code for this object.
no setterinherited
height double?
The target height of the bitmap in logical pixels.
finalinherited
imagePixelRatio double
The pixel ratio of the bitmap.
finalinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
width double?
The target width of the bitmap in logical pixels.
finalinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toJson() Object
Convert the object to a Json format.
override
toString() String
A string representation of this object.
inherited

Operators

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

Static Methods

create(ImageConfiguration configuration, String assetName, {AssetBundle? bundle, String? package, double? width, double? height, double? imagePixelRatio, MapBitmapScaling bitmapScaling = MapBitmapScaling.auto}) Future<AssetMapBitmap>
Creates a AssetMapBitmap from an asset image with asset resolving and mipmapping enabled.

Constants

type → const String
The type of the BitmapDescriptor object, used for the JSON serialization.