Search code examples
flutterflutter-layoutflutter-animation

AutoScroll to a offset in interactive viewer in flutter


With the Interactive viewer documentation i came to know that we can autoscroll to a particular position in Interactive viewer with toScene method.But i tried but everything in vain.How to autoscroll to an offset given the positions in interactive viewer

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


Solution

  • Thanks @pskink. Autoscroll in interactive viewer can be achieved by using TransformationController and matrix4

    @override
    void initState() {
    TransformationController transformationController =
          TransformationController();
    transformationController.value = Matrix4.identity()
            ..translate(-200.0, 0.0); // translate(x,y);
    }