How can I rotate (and possibly animate) an image at an arbitrary point within the image?

I am using Flutter and I need the image to rotate at the point I define. For example, I would like the image to animate the rotation (swing down) in the upper right corner. How to do it?

+3


source to share


2 answers


Here is a solution that uses a class FractionalOffset

to specify a point to rotate around.

If you don't want to animate, Transform

does what you want.

    return new Transform(
      transform: new Matrix4.rotationZ(math.PI),
      alignment: FractionalOffset.bottomRight,
      child: child,
    );

      

If you want to spice RotationTransition

things up, it almost does what you want, except that the alignment is not customizable. You can customize your own version:



import 'dart:ui';
import 'dart:math' as math;
import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(
      title: "Rotation Demo",
      home: new RotateDemo(),
  ));
}

/// Animates the rotation of a widget around a pivot point.
class PivotTransition extends AnimatedWidget {
  /// Creates a rotation transition.
  ///
  /// The [turns] argument must not be null.
  PivotTransition({
    Key key,
    this.alignment: FractionalOffset.center,
    @required Animation<double> turns,
    this.child,
  }) : super(key: key, listenable: turns);

  /// The animation that controls the rotation of the child.
  ///
  /// If the current value of the turns animation is v, the child will be
  /// rotated v * 2 * pi radians before being painted.
  Animation<double> get turns => listenable;

  /// The pivot point to rotate around.
  final FractionalOffset alignment;

  /// The widget below this widget in the tree.
  final Widget child;

  @override
  Widget build(BuildContext context) {
    final double turnsValue = turns.value;
    final Matrix4 transform = new Matrix4.rotationZ(turnsValue * math.PI * 2.0);
    return new Transform(
      transform: transform,
      alignment: alignment,
      child: child,
    );
  }
}

class RotateDemo extends StatefulWidget {
  State createState() => new RotateDemoState();
}

class RotateDemoState extends State<RotateDemo> with TickerProviderStateMixin {
  AnimationController _animationController;

  @override initState() {
    super.initState();
    _animationController = new AnimationController(
      duration: const Duration(milliseconds: 3000),
      vsync: this,
    )..repeat();
  }

  @override dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body:
          new Center(
            child: new PivotTransition(
              turns: _animationController,
              alignment: FractionalOffset.bottomRight,
              child: new Container(
                decoration: new BoxDecoration(backgroundColor: Colors.grey.shade200),
                width: 100.0,
                child: new FlutterLogo(style: FlutterLogoStyle.horizontal),
              ),
            ),
          ),
    );
  }
}

      

This example rotates the Flutter logo around the bottom right corner.

enter image description here

If you're feeling adventurous, you can send Flutter a pull request to adjust the alignment RotationTransition

.

+6


source




Your code is working fine. But see below GIF image. The full container is spinning here, but I want to do something like https://dribbble.com/shots/4240318-Made-with-InVision-Studio-Music-Player . Please, help.

0


source







All Articles