create static method

Future<AssetMapBitmap> create(
  1. ImageConfiguration configuration,
  2. String assetName, {
  3. AssetBundle? bundle,
  4. String? package,
  5. double? width,
  6. double? height,
  7. double? imagePixelRatio,
  8. MapBitmapScaling bitmapScaling = MapBitmapScaling.auto,
})

Creates a AssetMapBitmap from an asset image with asset resolving and mipmapping enabled.

This method dynamically resolves the correct asset version based on the device's pixel ratio, ensuring optimal resolution without manual configuration. It is the preferred method for creating instances of AssetMapBitmap due to its automatic asset resolution capabilities.

assetName is the name of the asset. The asset is resolved in the context of the specified bundle and package.

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.

bitmapScaling controls the scaling behavior:

  • MapBitmapScaling.auto automatically upscales and downscales the image to match the device's pixel ratio or the specified dimensions, maintaining consistency across devices.
  • MapBitmapScaling.none disables automatic scaling, which is useful when performance is a concern or if the asset is already scaled appropriately.

Asset mipmap is resolved using the devices pixel ratio from the ImageConfiguration.devicePixelRatio parameter. To initialize the ImageConfiguration with the devices pixel ratio, use the createLocalImageConfiguration method.

imagePixelRatio can be provided to override the resolved asset's pixel ratio. Specifying imagePixelRatio can be useful in scenarios where custom scaling is needed. imagePixelRatio is ignored if width or height is provided.

Returns a Future that completes with an AssetMapBitmap instance.

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 asset's dimensions:

Future<void> _getAssetMapBitmap(BuildContext context) async {
  final ImageConfiguration imageConfiguration = createLocalImageConfiguration(
    context,
   );
  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;
}

Implementation

//    );
///   AssetMapBitmap assetMapBitmap = await AssetMapBitmap.create(
///     imageConfiguration,
///     'assets/images/map_icon.png',
///   );
///   return assetMapBitmap;
/// }
/// ```
///
/// Optionally, [width] and [height] can be specified to control the
/// asset's dimensions:
///
/// ```dart
/// Future<void> _getAssetMapBitmap(BuildContext context) async {
///   final ImageConfiguration imageConfiguration = createLocalImageConfiguration(
///     context,
///    );
///   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;
/// }
/// ```
static Future<AssetMapBitmap> create(
  ImageConfiguration configuration,
  String assetName, {
  AssetBundle? bundle,
  String? package,
  double? width,
  double? height,
  double? imagePixelRatio,
  MapBitmapScaling bitmapScaling = MapBitmapScaling.auto,
}) async {
  assert(assetName.isNotEmpty, 'The asset name must not be empty.');
  final AssetImage assetImage =
      AssetImage(assetName, package: package, bundle: bundle);
  final AssetBundleImageKey assetBundleImageKey =
      await assetImage.obtainKey(configuration);

  return AssetMapBitmap._(
      assetName: assetBundleImageKey.name,
      imagePixelRatio: imagePixelRatio ?? assetBundleImageKey.scale,
      bitmapScaling: bitmapScaling,
      width: width ?? configuration.size?.width,
      height: height ?? configuration.size?.height);
}