在颤动中自动滚动到交互式查看器中的偏移量

问题描述

通过 Interactive viewer 文档,我开始知道我们可以使用 Interactive viewer 方法自动滚动到 toScene 中的特定位置。但我尝试过但一切都白费了。如何自动滚动到给定交互式查看器中的位置的偏移

 import 'dart:async';

import 'package:Flutter/material.dart';
import 'package:Flutter/scheduler.dart';

class MyPlanet extends StatefulWidget {
  @override
  _MyPlanetState createState() => _MyPlanetState();
}

class _MyPlanetState extends State<MyPlanet> {
  final TransformationController transformationController =
      TransformationController();

  @override
  void initState() {
    SchedulerBinding.instance.addPostFrameCallback((_) async {
      Timer(Duration(seconds: 5),() {
        setState(() {
          transformationController.value = Matrix4.identity()
            ..translate(800,0.0);
          transformationController.toScene(Offset(800,0.0));
        });
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return InteractiveViewer(
      minScale: 0.5,maxScale: 1,constrained: false,transformationController: transformationController,child: Container(
        height: 896,width: 2000,child: Image.asset(
          'assets/images/rain_forest.png',fit: BoxFit.cover,height: double.infinity,),);
  }
}

解决方法

谢谢@pskink。 可以使用 TransformationControllermatrix4

实现交互式查看器中的自动滚动
@override
void initState() {
TransformationController transformationController =
      TransformationController();
transformationController.value = Matrix4.identity()
        ..translate(-200.0,0.0); // translate(x,y);
}
,

试试

  @override
  void initState() {  
   SchedulerBinding.instance.addPostFrameCallback((_) async {
      transformationController.toScene(Offset(100,0));
    });
   super.initState();
  }