如何使用flutter_animarker包禁用标记旋转动画
禁用flutter_animarker中标记的旋转动画
我正在使用 flutter_animarker: ^3.4.0-beta.1 包,当前标记会出现360度旋转过渡动画,希望仅禁用旋转时的动画,让标记直接切换到目标旋转角度,同时保留位置移动的动画效果。
我的代码如下:
更新位置的函数
void newLocationUpdate(LocationObj obj) { var marker = RippleMarker( markerId: markerId, position: obj.latlng, ripple: false, alpha: 1, rotation: obj.rotat, icon: sourceIcon, anchor: const Offset(0.5, 0.5), onTap: () { // print('Tapped! $obj'); }); setState(() => markers[markerId] = marker); }
Animarker组件使用代码
Animarker( curve: Curves.linear, useRotation: true, rippleRadius: 0.5, angleThreshold: 1.5, duration: duration, runExpressAfter: 1, rippleDuration: const Duration(seconds: 2), mapId: controller.future.then<int>((value) => value.mapId), markers: markers.values.toSet(), child: GoogleMap( polylines: { Polyline( width: 2, polylineId: const PolylineId('HTPolyline'), points: locArr.map((element) => element.latlng).toList(), color: const Color.fromARGB(179, 62, 191, 208)), }, mapType: MapType.normal, initialCameraPosition: startPosition, onMapCreated: (gController) { getPositionsHistory().then((latlng) { if (latlng.latitude > 0) { gController.animateCamera(CameraUpdate.newCameraPosition( CameraPosition(target: latlng, zoom: 14.5))); } stream.forEach((value) => newLocationUpdate(value)); controller.complete(gController); }); }), )
解决方案
要禁用旋转动画,同时保留标记的正确旋转角度和位置动画,可以尝试以下几种方式:
方式一:关闭Animarker的旋转动画控制
将Animarker的 useRotation 参数设为 false,Animarker将不再对标记的旋转属性添加过渡动画,标记的旋转会直接更新到你设置的 rotation 值:
Animarker( // 其他参数保持不变 useRotation: false, // ... )
方式二:自定义bearing函数跳过动画
如果关闭 useRotation 后旋转未生效,可自定义 bearing 回调,直接返回目标旋转角度,强制Animarker不做过渡插值:
Animarker( // 其他参数保持不变 useRotation: true, bearing: (_, newBearing) => newBearing, // 直接返回新角度,无动画过渡 // ... )
方式三:调整角度阈值
将 angleThreshold 设置为大于180的值(两个角度间的最小差值最大为180),这样任何角度变化都不会触发旋转动画:
Animarker( // 其他参数保持不变 angleThreshold: 181, // ... )
内容的提问来源于stack exchange,提问作者Hidayatullah Mukhlis
相关产品推荐
相关产品推荐

