简体   繁体   English

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

[英]How to get the value of the current scale of InteractiveViewer in flutter?

So what I want to do here is to change the size of a SingleChildScrollView proportionally whenever the scale of the InteractiveViewer.所以我想在这里做的是在 InteractiveViewer 的缩放比例时按比例更改 SingleChildScrollView 的大小。 For example: scale 1.0 = width of the scrollview is 1000, scale 2.0 = width of the scrollview is 2000 and so on.例如: scale 1.0 = scrollview 的宽度为 1000, scale 2.0 = scrollview 的宽度为 2000 等等。 I can't seem to find out how to be able to do this directly from the documentation.我似乎无法直接从文档中找出如何做到这一点。 So I considered it by getting the value of the property(scale) itself and re-render the SingleChildScrollView widget but I can't seem to find any way to do this either.所以我通过获取属性(比例)本身的值并重新渲染 SingleChildScrollView 小部件来考虑它,但我似乎也找不到任何方法来做到这一点。 If there is any need for more clarification feel free to ask.如果需要更多说明,请随时提出。 Thank you!谢谢!

You can get the actual and persistent scale value like this:您可以像这样获得实际和持久的比例值:

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,
      ),
    );
  }
}

The onInteractionUpdate is called when the user updates a pan or scale gesture on the widget.当用户更新小部件上的平移或缩放手势时,将调用onInteractionUpdate So you can get the current scale from the callback .所以你可以从callback获取当前的scale

I added a demo code below:我在下面添加了一个演示代码:

       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'),
        ),
      );

Use onInteractionUpdate property and get the value from ScaleUpdateDetails like:使用onInteractionUpdate属性并从ScaleUpdateDetails获取值,例如:

double scale;

InteractiveViewer(
  onInteractionUpdate: (details) {
    scale = details.scale; // This is your scale. 
  },
  child: FlutterLogo(),
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM