首页 > 解决方案 > 如何在 Flutter 中处理 NimaAnimations

问题描述

我在我的一个 Dart 文件中使用了 Nima 瞄准。但是,一旦完成,我将无法处理它。NimaAnimation 有一个 NimaController 的抽象类,但不幸的是它没有任何 dispose 函数来停止它。

我因此面临的问题是性能问题。我什至尝试使用性能叠加来观察内存使用情况,我确实注意到动画没有被关闭,即使我导航到新屏幕,它也会继续在后端渲染。我为此编写的代码是:

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:maui/screens/tab_home.dart';
import 'package:nima/nima_actor.dart';

class WelcomeScreen extends StatefulWidget {
  @override
  State createState() => new WelcomeScreenState();
}

class WelcomeScreenState extends State<WelcomeScreen> {
  bool delayed;
  var animatecontrol = null;
  NimaController nimaController;
 NimaActor nima ;
  void initState() {
    super.initState();
       nima = new NimaActor(
      "assets/quack",
      alignment: Alignment.center,
      fit: BoxFit.scaleDown,
      animation: 'welcome with hello',
      mixSeconds: 0.2,

    );
    new Future.delayed(const Duration(milliseconds: 6200), () {
      Navigator.of(context).pushReplacementNamed('/tab');
      setState(() {
     nima.controller;
        delayed = true;
      });
    });
  }

  void dispose() {
    super.dispose();

  }

  @override
  Widget build(BuildContext context) {
    MediaQueryData media = MediaQuery.of(context);

    var size = media.size;


    // TODO: implement build
    return (delayed == true)
        ? new TabHome()
        : new Scaffold(
            body: new Container(
                decoration: new BoxDecoration(
                  color: Colors.purple,
                ),
                child: new Stack(
                    alignment: AlignmentDirectional.bottomCenter,
                    children: <Widget>[
                      new Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: <Widget>[
                            new AspectRatio(
                              aspectRatio: size.height > size.width ? 1.5 : 3.8,
                              child: nima,
                            ),
                          ])
                    ])));
  }
}

标签: android-studiodartflutterandroid-animation

解决方案


Nima Animator 具有暂停和完成的属性。这些可以帮助控制动画状态。有关详细信息,请参阅此链接(示例):

https://github.com/2d-inc/Nima-Flutter/blob/master/example/hop/lib/main.dart


推荐阅读