Flutter技术问题:如何让InteractiveViewer内子部件保持固定尺寸?
解决InteractiveViewer中标记部件固定尺寸的问题
你当前的代码把标记和图片放在同一个Stack里作为InteractiveViewer的child,缩放操作会作用于整个Stack,导致标记跟着变大变小。要实现标记尺寸固定的效果,需要把标记移出InteractiveViewer的缩放范围,同时保证它能准确定位在图片的目标位置上。
可行方案:用逆变换抵消缩放影响
通过TransformationController获取InteractiveViewer的变换矩阵,然后给外层的标记部件应用逆矩阵变换,抵消缩放和平移对标记的影响,同时保持标记与图片目标点的位置对应。
代码示例:
import 'package:flutter/material.dart'; class FixedMarkerDemo extends StatefulWidget { const FixedMarkerDemo({super.key}); @override State<FixedMarkerDemo> createState() => _FixedMarkerDemoState(); } class _FixedMarkerDemoState extends State<FixedMarkerDemo> { final TransformationController _transformationController = TransformationController(); @override Widget build(BuildContext context) { return Scaffold( body: Stack( alignment: Alignment.topLeft, children: [ InteractiveViewer( transformationController: _transformationController, boundaryMargin: const EdgeInsets.all(double.infinity), child: Image.network("http://placekitten.com/400/400"), ), // 固定尺寸的标记 Positioned( left: 100, top: 100, child: Transform( // 用逆矩阵抵消InteractiveViewer的变换 transform: Matrix4.inverted(_transformationController.value), child: const SizedBox( width: 24, height: 24, child: Icon(Icons.pin_drop, color: Colors.red, size: 24), ), ), ), ], ), ); } }
原理说明
TransformationController会实时记录InteractiveViewer的缩放、平移变换矩阵- 给标记应用
Matrix4.inverted()得到的逆矩阵,相当于把标记做反向的缩放和平移,抵消掉InteractiveViewer的变换效果 - 标记放在外层
Stack,不受InteractiveViewer的直接缩放影响,配合逆变换就能保持固定尺寸,同时位置始终对应图片上的目标点
内容的提问来源于stack exchange,提问作者Jochen
相关产品推荐
相关产品推荐

