flutter 如何更改BitmapDescriptor大小,当更改大小值时,它似乎不受影响

3b6akqbq  于 2023-05-19  发布在  Flutter
关注(0)|答案(1)|浏览(276)
Future<BitmapDescriptor> _getCustomMarkerIcon(double size) async {
    final icon = await BitmapDescriptor.fromAssetImage(
      ImageConfiguration(size: Size(size, size)),
      'assets/images/car.png',
    );
    return icon;
  }


    _getCustomMarkerIcon(2).then((icon) {
      setState(() {
        _customIcon = icon;
          _markers = recommended_cars
        .map((e) => _buildMarker(e, recommended_cars.indexOf(e).toString()))
        .toSet();

      });
    });

  
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Stack(
          children: [
            GoogleMap(
              zoomControlsEnabled: false,
              myLocationEnabled: false,
              initialCameraPosition: _initialCamPos,
              markers: _markers,
            )
          ],
        ),
      ),
    );
  }
}

这是我的小工具,我传递了值2到大小,我尝试了5或10,但图标大小根本没有改变。
当我编辑尺寸值时,我希望尺寸会改变,但什么也没有发生,我真的尝试了一切,我不知道问题到底在哪里。
我想是这里

_getCustomMarkerIcon(2).then((icon) {
      setState(() {
        _customIcon = icon;
          _markers = recommended_cars
        .map((e) => _buildMarker(e, recommended_cars.indexOf(e).toString()))
        .toSet();
pbwdgjma

pbwdgjma1#

根据我在评论中所说的:
更改以下代码:

Future<BitmapDescriptor> _getCustomMarkerIcon(double size) async {
    final icon = await BitmapDescriptor.fromAssetImage(
      ImageConfiguration(size: Size(size, size)),
      'assets/images/car.png',
    );
    return icon;
  }

致:

// Import also this
import 'dart:ui' as ui;
import 'package:flutter/services.dart';

  Future<Uint8List> getBytesFromAsset(int width) async {
    ByteData data = await rootBundle.load('assets/images/car.png');
    ui.Codec codec = await ui.instantiateImageCodec(data.buffer.asUint8List(), targetWidth: width);
    ui.FrameInfo fi = await codec.getNextFrame();
    return (await fi.image.toByteData(format: ui.ImageByteFormat.png))!.buffer.asUint8List();
  }

  Future<BitmapDescriptor> _getCustomMarkerIcon(int width) async {
    final Uint8List markerIcon = await getBytesFromAsset(width);
    return BitmapDescriptor.fromBytes(markerIcon);
  }

我已经测试过了,它很有效!注意,由于targetWidth只能接受整数,因此宽度类型从double更改为int

相关问题