点击后 Flutter Rive动画

oalqel3c  于 2023-05-30  发布在  Flutter
关注(0)|答案(1)|浏览(216)

我是新的使用Rive和我想使用它在一个proyect,但我不知道如何动画后,水龙头,我使用https://editor.rive.app/preview/2063-4080-flutter-puzzle-hack-project/381484?mode=animate&artboard=birb&animation=birb
我尝试使用此代码,但抛出一个错误“期望类型为'SMITrigger'的值,但得到类型为'SMIBool'的值
我试了一下鸟在轻拍后向上看

class GlassCard extends StatefulWidget {
  const GlassCard({super.key});

  @override
  State<GlassCard> createState() => _GlassCardState();
}

class _GlassCardState extends State<GlassCard> {
  String animation = 'idle';
  //animation
  Artboard? _birdArtboard;
  SMITrigger? trigger;
  StateMachineController? stateMachineController;

  @override
  void initState() {
    super.initState();
    rootBundle.load('assets/bird.riv').then(
      (data) {
        final file = RiveFile.import(data);
        final artboard = file.mainArtboard;
        stateMachineController =
            StateMachineController.fromArtboard(artboard, "birb");
        if (stateMachineController != null) {
          artboard.addController(stateMachineController!);
          trigger = stateMachineController!.findSMI('look up');

          for (var e in stateMachineController!.inputs) {
            debugPrint(e.runtimeType.toString());
            debugPrint("name ${e.name} End");
          }
          trigger = stateMachineController!.inputs.first as SMITrigger;
        }

        setState(() => _birdArtboard = artboard);
      },
    );
  }

  void lookup() {
    trigger?.fire();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: SizedBox(
        height: 400,
        width: 400,
        child: _birdArtboard == null
            ? const SizedBox()
            : Center(
                child: GestureDetector(
                  onTap: () {
                    lookup();
                  },
                  child: Rive(artboard: _birdArtboard!),
                ),
              ),
      ),
    );
  }
}

谢谢你

wtlkbnrh

wtlkbnrh1#

发生此错误的原因是代码中出现了以下行:-

trigger = stateMachineController!.inputs.first as SMITrigger;

原因是类型转换无效。
你不需要那行代码来运行和与rive输入交互。要与输入进行交互,您只需为变量使用适当的数据类型。我已经添加了一个小例子,一个单一的点击鸟看起来和双击,它启用和禁用鸟的舞蹈。
完整代码:-

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:rive/rive.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'List',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      debugShowCheckedModeBanner: false,
      home: const GlassCard(),
    );
  }
}

class GlassCard extends StatefulWidget {
  const GlassCard({super.key});

  @override
  State<GlassCard> createState() => _GlassCardState();
}

class _GlassCardState extends State<GlassCard> {
  String animation = 'idle';

  Artboard? _birdArtboard;
  SMITrigger? trigger;
  SMIBool? dance;

  StateMachineController? stateMachineController;

  @override
  void initState() {
    super.initState();
    rootBundle.load('assets/bird.riv').then(
      (data) {
        final file = RiveFile.import(data);
        final artboard = file.mainArtboard;
        stateMachineController =
            StateMachineController.fromArtboard(artboard, "birb");
        if (stateMachineController != null) {
          artboard.addController(stateMachineController!);

          trigger = stateMachineController!.findSMI('look up');

          dance = stateMachineController!.findSMI('dance');
        }

        setState(() => _birdArtboard = artboard);
      },
    );
  }

  void lookup() {
    trigger?.fire();
  }

  void danceBird() {
    dance?.change(!dance!.value);
  }

  @override

      Widget build(BuildContext context) {
        return Center(
          child: SizedBox(
            height: 400,
            width: 400,
            child: _birdArtboard == null
                ? const SizedBox()
                : Center(
                    child: GestureDetector(
                      onTap: () {
                        lookup();
                      },
                      onDoubleTap: () {
                        danceBird();
                      },
                      child: Rive(artboard: _birdArtboard!),
                    ),
                  ),
          ),
        );
      }
    }

输出:-

相关问题