В Flutter не работает анимация вращения - PullRequest
0 голосов
/ 03 мая 2020

Я пытаюсь сделать небольшую игру во Флаттере, в которой мне нужно повернуть FittedBox (родительский элемент изображения), который находится внутри Grid, как показано в приведенном ниже коде.

child: new AnimatedBuilder(
    animation: animationController,
    child: new InkWell(
      onTap: () {
        // setState(
        //   () {
            widget._angleCode = getNextDegree(widget._angleCode);
            animationController.forward();
            //checkAnswer();
        //   },
        // );
      },
      child: new FittedBox(
          fit: BoxFit.fill,
          child: new Image.asset(getImageByCode(widget._image),
              fit: BoxFit.cover)),
    ),
    builder: (BuildContext context, Widget _widget) {

      animationController.value = getAngleByCode(widget._angleCode); //returns 0,.5,.75 consequtively
      return new Transform.rotate(
        angle: animationController.value * 2 * pi,
        child: _widget,
      );
    },
  ),

Хотя приведенный выше код делает поворот изображения НО без анимации преобразования. Я переопределил свой класс с помощью SingleTickerProviderStateMixin и реализовал initState (), как показано ниже:

    @override
  void initState() {
    super.initState();
    animationController = new AnimationController(
      vsync: this,
      duration: new Duration(seconds: 2),
    );
    //animationController.forward();
  }

Как я могу оживить вращение?

1 Ответ

1 голос
/ 03 мая 2020

Пожалуйста, попробуйте это

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
  AnimationController _controller;

  double target = 0.0;

  @override
  void initState() {
    _controller = AnimationController(
      duration: const Duration(milliseconds: 5000),
      vsync: this,
    );
    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  void _onTapHandler() {
    target += 0.25;
    if (target > 1.0) {
      target = 0.25;
      _controller.reset();
    }
    _controller.animateTo(target);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: GestureDetector(
          onTap: _onTapHandler,
          child: RotationTransition(
            turns: Tween(begin: 0.0, end: 1.0).animate(_controller),
            child: Container(
              color: Colors.red,
              child: SizedBox(
                child: new Text("Lorem ipsum"),
                height: 100,
                width: 100,
              ),
            ),
          ),
        ),
      ),
    );
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...