Files
rive-flutter/example/lib/skinning_demo.dart
HayesGordon 4e2547d08d Expose methods to easily get Rive state machine inputs
Our current API is quite verbose and requires casting to the relevant class types when using the current find API. These methods simplify the process.

Diffs=
1460f5366 Expose methods to easily get Rive state machine inputs (#7085)

Co-authored-by: Gordon <pggordonhayes@gmail.com>
2024-04-22 10:11:46 +00:00

107 lines
2.7 KiB
Dart

import 'package:flutter/material.dart';
import 'package:rive/rive.dart';
/// Basic skinning example. The skinning states is set in the Rive editor and
/// triggers are used to change the value.
class SkinningDemo extends StatefulWidget {
const SkinningDemo({Key? key}) : super(key: key);
@override
State<SkinningDemo> createState() => _SkinningDemoState();
}
class _SkinningDemoState extends State<SkinningDemo> {
static const _skinMapping = {
"Skin_0": "Plain",
"Skin_1": "Summer",
"Skin_2": "Elvis",
"Skin_3": "Superhero",
"Skin_4": "Astronaut"
};
String _currentState = 'Plain';
SMITrigger? _skin;
void _onRiveInit(Artboard artboard) {
final controller = StateMachineController.fromArtboard(
artboard,
'Motion',
onStateChange: _onStateChange,
);
artboard.addController(controller!);
_skin = controller.getTriggerInput('Skin');
}
void _onStateChange(String stateMachineName, String stateName) {
if (stateName.contains("Skin_")) {
setState(() {
_currentState = _skinMapping[stateName] ?? 'Plain';
});
}
}
void _swapSkin() {
_skin?.fire();
}
@override
Widget build(BuildContext context) {
const textColor = Color(0xFFefcb7d);
return Scaffold(
appBar: AppBar(
title: const Text('Skinning Demo'),
),
body: Stack(
children: [
Center(
child: RiveAnimation.asset(
'assets/skins_demo.riv',
fit: BoxFit.cover,
onInit: _onRiveInit,
),
),
Align(
alignment: Alignment.topCenter,
child: Column(
children: [
const Padding(
padding: EdgeInsets.all(24.0),
child: Text(
'Choose an Avatar',
style: TextStyle(
fontSize: 48,
fontWeight: FontWeight.bold,
color: textColor,
),
),
),
FilledButton(
onPressed: _swapSkin,
style: const ButtonStyle(
backgroundColor:
MaterialStatePropertyAll(Color(0xFF7d99ef)),
),
child: const Text('Swap Skin'),
),
const Spacer(),
Text(
'Skin: $_currentState',
style: const TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: textColor,
),
),
const SizedBox(height: 48)
],
),
)
],
),
);
}
}