Changing Rive Animation Color via Code in Flutter (Android)

Hello, I'm trying to change the color of my Rive animation programmatically in Flutter for Android.

Here's what I've done in Rive:

  • I created a View Model named cor (color) and, inside it, a Color type cor property.

  • I right-clicked on the fill of my shape and selected "Data Binding."

  • Then, I selected the View Model I created. I went to the artboard and selected the created View Model.

  • I created a State Machine named stateMachine.

  • Finally, I exported it for runtime.

  • I am using the rive: ^0.13.20 dependency.



The code I'm using is as follows:

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

// Final code based on the official Rive documentation for Flutter Data Binding.
// PREREQUISITE: The 'rive' package in pubspec.yaml must be updated (e.g., ^0.14.0).

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Rive App',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        useMaterial3: true,
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.pinkAccent),
      ),
      home: const MyRiveAnimation(),
    );
  }
}

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

  @override
  State<MyRiveAnimation> createState() => _MyRiveAnimationState();
}

class _MyRiveAnimationState extends State<MyRiveAnimation> {
  Artboard? _artboard;
  StateMachineController? _controller;
  SMIInput<Color>? _corInput;

  @override
  void initState() {
    super.initState();
    _loadRiveFile();
  }

  void _loadRiveFile() async {
    final file = await RiveFile.asset('un.riv');
    final artboard = file.mainArtboard;
    final controller = StateMachineController.fromArtboard(artboard, 'stateMachine');
    if (controller != null) {
      artboard.addController(controller);
      _corInput = controller.findInput<Color>('cor');
    }
    setState(() {
      _artboard = artboard;
      _controller = controller;
    });
  }

  void _changeColor(Color color) {
    if (_corInput != null) {
      _corInput!.value = color;
    } else {
      debugPrint("ALERT: Input 'cor' not found in State Machine.");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Interactive Piggy Bank'),
      ),
      body: Center(
        child: _artboard == null
            ? const CircularProgressIndicator()
            : Rive(artboard: _artboard!),
      ),
      bottomNavigationBar: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            IconButton(
              icon: const Icon(Icons.color_lens, color: Colors.blue),
              iconSize: 40,
              onPressed: () => _changeColor(Colors.blue),
            ),
            IconButton(
              icon: const Icon(Icons.color_lens, color: Colors.green),
              iconSize: 40,
              onPressed: () => _changeColor(Colors.green),
            ),
            IconButton(
              icon: const Icon(Icons.color_lens, color: Colors.purple),
              iconSize: 40,
              onPressed: () => _changeColor(Colors.purple),
            ),
          ],
        ),
      ),
    );
  }
}

I'm getting the message: "ALERT: Input 'cor' not found in State Machine."

Can anyone help me, please?