[英]Call function from child widget animate map to move to tapped map icon
I have been trying to figure this out but I am stuck.我一直试图弄清楚这一点,但我被困住了。 I have a map where I populates markers on which your are able to click.我有一个 map,我在其中填充了您可以单击的标记。 When I click the marker I want to animate to the marker I pressed.当我单击标记时,我想动画到我按下的标记。 I have the map animation working when I am pressing a dedicated button to test the function and the location of the map is changed.当我按下专用按钮测试 function 并且 Z1D78DC8ED51FE241445E 的位置已更改时,我有 map animation 工作。
My marker widget looks like this.我的标记小部件看起来像这样。
Marker(
width: 40,
height: 40,
point: LatLng(evcp.location.coordinates.latitude,
evcp.location.coordinates.longitude),
builder: (context) => MarkerIcon(
id: evcp.id,
latitude: evcp.location.coordinates.latitude,
longitude: evcp.location.coordinates.longitude,
animateToPin: _animateMapMove(LatLng(10, 30), 12),
),
anchorPos: AnchorPos.align(AnchorAlign.top),
),
And my MarkerIcon widget looks like this:我的 MarkerIcon 小部件如下所示:
class MarkerIcon extends StatefulWidget {
final String id;
final double latitude;
final double longitude;
final Function animateToPin;
MarkerIcon({
Key? key,
required this.id,
required this.latitude,
required this.longitude,
required this.animateToPin,
}) : super(key: key);
@override
State<MarkerIcon> createState() => _MarkerIconState();
}
class _MarkerIconState extends State<MarkerIcon> {
bool tapped = false;
@override
Widget build(BuildContext context) {
//var markerProvider = Provider.of<MarkerProvider>(context);
return IconButton(
onPressed: () => widget.animateToPin(),
icon: Icon(
Icons.location_on,
size: 30,
color: tapped ? Styles.redColor : Styles.greenColor,
));
}
And I am getting this error:我收到了这个错误:
type 'Null' is not a subtype of type 'Function'
Is the issue that I am trying to send the _animateMapMove function from my parent widget to the child widget.是我试图将_animateMapMove function 从我的父小部件发送到子小部件的问题。 If so how can this be solved?如果是这样,如何解决? Or is it because my Marker has a builder and then when the marker gets build its for some reason doing the animateMapMove ?或者是因为我的标记有一个构建器,然后当标记被构建时出于某种原因执行animateMapMove ?
Sometime the brain just don't work.有时大脑只是不工作。 To solve the issue I needed to use a anonymous function.为了解决这个问题,我需要使用匿名 function。
So in the Marker class I needed to change from this:所以在标记 class 我需要改变这个:
_animateMapMove()
To this:对此:
() => _animateMapMove()
And in the MarkerIcon I changed this:在 MarkerIcon 我改变了这个:
onPressed: () => widget.animateToPin()
To this:对此:
onPressed () { widget.animateToPin() }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.