简体   繁体   English

从子小部件调用 function 动画 map 以移动到点击的 map 图标

[英]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.

相关问题 如何从其父窗口小部件中调用 Map 控制器 Flutter Map 的移动方法 - How to call the Map Controller's move method of Flutter Map from its parent widget 如何从子小部件调用父小部件中的 function | Flutter - How to Call a function in parent widget from child widget | Flutter 如何从父小部件调用子小部件 function - how to call child widget function from parent widget 如何从 Flutter 中的子小部件调用父小部件 function - How to call parent widget function from child widget in Flutter 将坐标 Latlng 从 map 小部件传递到同一屏幕中的 Void function - Pass coordinates Latlng from map widget to Void function in the same screen 从地图或列表加载小部件 - Load a widget from a map or list Flutter: Nested Map of Map from the Cloud Function call is typed as Map<object?, object?></object?,> - Flutter: Nested Map of Map from the Cloud Function call is typed as Map<Object?, Object?> Flutter - 从父类或另一个子类调用子类的动画函数 - Flutter - Call animate function of a child from parent, or from another child class 如何在Google Map Flutter上显示小部件图标? - How to display Widget Icon over Google Map Flutter? Flutter - 在点击时动画小部件从 GridView 移动到 BottomBar - Flutter - Animate a widget to move from GridView to BottomBar upon tapping
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM