【问题标题】:InteractiveViewer image is zoomed inInteractiveViewer 图像放大
【发布时间】:2021-01-22 17:50:36
【问题描述】:

我的问题应该很简单,但我卡住了:我需要显示带有标记的地图图像(这就是我使用堆栈的原因),并且可以缩放和平移此图像。唯一的问题是,当我打开应用程序时,图像完全放大了,我必须手动缩小。我尝试阅读文档和谷歌搜索,但只发现不符合我的需求或无法按预期工作的库。

所以,问题很简单:我怎样才能从头看到完全缩小的图像?

这是代码。

    return Container(
      height: 350,
      child: InteractiveViewer(
        // TODO the image is automatically zoomed in!
        minScale: .1,
        maxScale: 1,
        constrained: false,
        child: Stack(children: [
          Image.asset(itinerary.mapImage, fit: BoxFit.fitWidth),
          icon(...),
        ])
      )
    )

【问题讨论】:

    标签: android flutter zooming


    【解决方案1】:

    您需要使用 TransformationController 来控制 InteractiveViewer。 代码如下:

      TransformationController _controller;
    
      @override
      void initState() {
        _controller = TransformationController();
        _controller.value = Matrix4.identity() * 0.5;
        super.initState();
      }
    
      @override
      void dispose() {
        _controller.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Container(
            height: 350,
            child: InteractiveViewer(
              transformationController: _controller,
              minScale: .1,
              maxScale: 1,
              onInteractionEnd: (s) {
                print(_controller.value);
              },
              constrained: false,
              child: Stack(
                children: [
                  Image.asset(
                    "assets/Big.Buck.Bunny.-.Landscape.png",
                    fit: BoxFit.fitWidth,
                  ),
                ],
              ),
            ),
          ),
        );
      }
    

    请注意,您可能需要调整初始比例值(与 Matrix4 相乘的值)以获得所需的输出。

    【讨论】:

    • 嗯,完美!我根本没有找到这种解决方案。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    • 2011-07-12
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多