2

I'm working on some photo editing app and i need to drag, rotate and scale the widget with fingers. But I can't use onPanUpdate and onScale functions how can i achieve this is there any trick or something. I'm new on flutter.

1 Answers1

0

You can copy paste run full code below
You can use package https://pub.dev/packages/matrix_gesture_detector
code snippet

 MatrixGestureDetector(
        onMatrixUpdate: (m, tm, sm, rm) {
          notifier.value = m;
        },
        child: AnimatedBuilder(
          animation: notifier,
          builder: (ctx, child) {
            return Transform(
              transform: notifier.value,
              child: Stack(

working demo

enter image description here

full code

import 'package:flutter/material.dart';
import 'package:matrix_gesture_detector/matrix_gesture_detector.dart';

class TransformDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final ValueNotifier<Matrix4> notifier = ValueNotifier(Matrix4.identity());
    return Scaffold(
      backgroundColor: Colors.grey,
      appBar: AppBar(
        title: Text('Transform Demo'),
      ),
      body: MatrixGestureDetector(
        onMatrixUpdate: (m, tm, sm, rm) {
          notifier.value = m;
        },
        child: AnimatedBuilder(
          animation: notifier,
          builder: (ctx, child) {
            return Transform(
              transform: notifier.value,
              child: Stack(
                children: <Widget>[
                  Container(
                    color: Colors.white30,
                  ),
                  Positioned.fill(
                    child: Container(
                      transform: notifier.value,
                      child: FittedBox(
                        fit: BoxFit.contain,
                        child: Icon(
                          Icons.favorite,
                          color: Colors.deepPurple.withOpacity(0.5),
                        ),
                      ),
                    ),
                  ),
                  Container(
                    decoration: FlutterLogoDecoration(),
                    padding: EdgeInsets.all(32),
                    alignment: Alignment(0, -0.5),
                    child: Text(
                      'use your two fingers to translate / rotate / scale ...',
                      style: Theme.of(context).textTheme.display2,
                      textAlign: TextAlign.center,
                    ),
                  ),
                ],
              ),
            );
          },
        ),
      ),
    );
  }
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {  
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(       
        primarySwatch: Colors.blue,       
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: TransformDemo(),
    );
  }
}
chunhunghan
  • 51,087
  • 5
  • 102
  • 120
  • Yes I tried that but I can't implement it for multiple child basically I need to attach emojis stickers and text to photo. If you implement as parent of child which needs to have that controls when you move the element it is turn into no controllable. do you have any idea about achieve this? I couldn't find anything about this. – Ahmet Safa BULBUL Jul 06 '20 at 13:41
  • Its pretty easy actually, just make the TransformDemo widget as big as the screen and then put multiple of these inside a stack. However you have to store the matrix information outside of the TransformDemo class in order to retain the transformation. – Ali Akber Apr 06 '21 at 16:28
  • @AhmetSafaBULBUL how did you solve it? I have same task, but above mentioned package seems to be abandoned – temirbek Mar 01 '23 at 08:43
  • Well, I found a better solution that doesn't use package: https://stackoverflow.com/a/74165192/614026 – temirbek Mar 01 '23 at 10:34