Advertisement
antasofa

Custom Interactive View

Sep 23rd, 2024
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.96 KB | Source Code | 0 0
  1. import 'package:flutter/material.dart';
  2.  
  3. class InteractiveViewTest extends StatefulWidget {
  4.   const InteractiveViewTest({super.key});
  5.  
  6.   @override
  7.   State<InteractiveViewTest> createState() => _InteractiveViewTestState();
  8. }
  9.  
  10. /// [AnimationController]s can be created with `vsync: this` because of
  11. /// [TickerProviderStateMixin].
  12. class _InteractiveViewTestState extends State<InteractiveViewTest>
  13.     with TickerProviderStateMixin {
  14.   TransformationController controller = TransformationController();
  15.   TapDownDetails? tapDownDetails;
  16.  
  17.   late AnimationController animationController;
  18.   late Animation<Matrix4> animation;
  19.  
  20.   @override
  21.   void initState() {
  22.     super.initState();
  23.     // controller = TransformationController();
  24.     animationController = AnimationController(
  25.       vsync: this,
  26.       duration: const Duration(milliseconds: 300),
  27.     )..addListener(() {
  28.         controller.value = animation.value;
  29.       });
  30.  
  31.     initZoom();
  32.   }
  33.  
  34.   void initZoom() {
  35.     final position = tapDownDetails?.localPosition;
  36.     double scale = 6.0;
  37.     final x = position != null ? -position.dx * (scale - 1) : -816.0;
  38.     final y = position != null ? -position.dy * (scale - 1) : -1726.0;
  39.     print('x: $x');
  40.     print('y: $y');
  41.     final zoomed = Matrix4.identity()
  42.       ..translate(x, y)
  43.       ..scale(scale);
  44.     // final end = controller.value.isIdentity() ? zoomed : Matrix4.identity();
  45.     animation = Matrix4Tween(
  46.       begin: controller.value,
  47.       end: zoomed,
  48.     ).animate(CurveTween(curve: Curves.easeInOut).animate(animationController));
  49.     animationController.forward(from: 0);
  50.   }
  51.  
  52.   @override
  53.   void dispose() {
  54.     controller.dispose();
  55.     animationController.dispose();
  56.     super.dispose();
  57.   }
  58.  
  59.   @override
  60.   Widget build(BuildContext context) {
  61.     return Scaffold(
  62.         // backgroundColor: Theme.of(context).colorScheme.primary,
  63.         appBar: AppBar(
  64.             // automaticallyImplyLeading: false,
  65.             // title: const Text('Controller demo'),
  66.             ),
  67.         body: Center(
  68.           child: InteractiveViewer(
  69.             transformationController: controller,
  70.             // clipBehavior: widget.clipBehavior ? Clip.hardEdge : Clip.none,
  71.             // panEnabled: false,
  72.             // scaleEnabled: false,
  73.             maxScale: 11.0,
  74.             minScale: .1,
  75.             child: GestureDetector(
  76.                 onTapDown: (TapDownDetails details) => tapDownDetails = details,
  77.                 onDoubleTapDown: (TapDownDetails details) =>
  78.                     tapDownDetails = details,
  79.                 onDoubleTap: () {
  80.                   final position = tapDownDetails!.localPosition;
  81.                   double scale = 6.0;
  82.                   final x = -position.dx * (scale - 1);
  83.                   final y = -position.dy * (scale - 1);
  84.                   print('x: $x');
  85.                   print('y: $y');
  86.                   final zoomed = Matrix4.identity()
  87.                     ..translate(x, y)
  88.                     ..scale(scale);
  89.                   final end = controller.value.isIdentity()
  90.                       ? zoomed
  91.                       : Matrix4.identity();
  92.                   animation = Matrix4Tween(
  93.                     begin: controller.value,
  94.                     end: end,
  95.                   ).animate(CurveTween(curve: Curves.easeInOut)
  96.                       .animate(animationController));
  97.                   animationController.forward(from: 0);
  98.                 },
  99.                 child: Container(
  100.                   width: MediaQuery.of(context).size.width,
  101.                   decoration: const BoxDecoration(
  102.                       image: DecorationImage(
  103.                     image:
  104.                         AssetImage('assets/Three-Kingdom-The-Journey-Map.jpg'),
  105.                   )),
  106.                 )),
  107.           ),
  108.         ),
  109.         floatingActionButton: FloatingActionButton(
  110.           onPressed: initZoom,
  111.           child: const Icon(Icons.center_focus_strong_rounded),
  112.         ));
  113.   }
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement