Flutter 中类似 Image Map 的功能?

Chr*_*isH 3 dart flutter

我已经搜索了很多,但没有找到可靠的答案,所以如果这是一个骗局,我真的尝试过。

我有一个应用程序,我正在重写并远离基于 html 的混合平台(特别是 Trigger.io);在 Flutter 和 Dart 中快速重写。

该应用程序的一部分包括一个非常简单的屏幕,用户可以在其中单击人体图像,并通过图像映射获取身体部位(右前臂、左膝、头部等)的标识符和标题。

我在 Flutter 中根本找不到这种行为和能力的类比。我是否错过了一些简单的事情,因为我完全想多了?

非常感谢。

jam*_*lin 6

您可以将您的包裹Image在 a 中GestureDetector并指定onTapDown(或onTapUp)检查点击坐标并采取相应行动的回调。

(要将全局坐标转换为局部坐标,请参阅:flutter : Get Local position of Gesture Detector

这是一个粗略的尝试:

import 'package:quiver/iterables.dart' show enumerate;

class ImageMap extends StatelessWidget {
  const ImageMap({
    Key key,
    @required this.image,
    @required this.onTap,
    @required this.regions,
  }) : super(key: key);

  final Widget image;
  final List<Path> regions;

  /// Callback that will be invoked with the index of the tapped region.
  final void Function(int) onTap;

  void _onTap(BuildContext context, Offset globalPosition) {
    RenderObject renderBox = context.findRenderObject();
    if (renderBox is RenderBox) {
      final localPosition = renderBox.globalToLocal(globalPosition);
      for (final indexedRegion in enumerate(regions)) {
        if (indexedRegion.value.contains(localPosition)) {
          onTap(indexedRegion.index);
          return;
        }
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTapDown: (details) => _onTap(context, details.globalPosition),
      child: image,
    );
  }
}
Run Code Online (Sandbox Code Playgroud)