如何在flutter中获取InteractiveViewer当前比例的值?

ano*_*ous 3 flutter

所以我想在这里做的是在 InteractiveViewer 的缩放比例时按比例更改 SingleChildScrollView 的大小。例如: scale 1.0 = 滚动视图的宽度为 1000,缩放 2.0 = 滚动视图的宽度为 2000 等等。我似乎无法直接从文档中找出如何做到这一点。所以我通过获取属性(比例)本身的值并重新渲染 SingleChildScrollView 小部件来考虑它,但我似乎也找不到任何方法来做到这一点。如果需要更多说明,请随时提出。谢谢!

Ral*_*egi 9

您可以像这样获得实际和持久的比例值:

class InteractiveViewerTest extends StatefulWidget {
  @override
  _InteractiveViewerTestState createState() => _InteractiveViewerTestState();
}

class _InteractiveViewerTestState extends State<InteractiveViewerTest> {
  TransformationController _transformationController =
      TransformationController();

  @override
  Widget build(BuildContext context) {
    return InteractiveViewer(
      minScale: 0.5,
      maxScale: 2.0,
      transformationController: _transformationController,
      onInteractionEnd: (details) {
        // Details.scale can give values below 0.5 or above 2.0 and resets to 1
        // Use the Controller Matrix4 to get the correct scale.
        double correctScaleValue = _transformationController.value.getMaxScaleOnAxis();
      },
      child: Container(
        width: 100,
        height: 100,
        color: Colors.blue,
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 这是正确的答案,details.scale 是相对于当前交互的,因此,例如,如果您向内捏,它将始终返回一个小于 1 的值,如果您向外捏,它将返回一个大于零的值,这无关紧要正在交互的子部件的实际比例。 (2认同)

voi*_*oid 1

onInteractionUpdate当用户更新小部件上的平移或缩放手势时调用。这样你就可以scale从得到电流callback

我在下面添加了演示代码:

       InteractiveViewer(
        transformationController: transformationController,
        onInteractionUpdate: (ScaleUpdateDetails details){  // get the scale from the ScaleUpdateDetails callback
          var myScale = details.scale;
          print(myScale); // print the scale here
        },
        boundaryMargin: EdgeInsets.all(20.0),
        minScale: 0.1,
        maxScale: 4.6,
        scaleEnabled: true,
        panEnabled: true,
        child: ClipRRect(
          borderRadius: BorderRadius.circular(18.0),
          child: Image.asset('images/user_picture.png'),
        ),
      );
Run Code Online (Sandbox Code Playgroud)