mirror of
https://github.com/rive-app/rive-flutter.git
synced 2025-11-08 00:49:25 +08:00
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>
107 lines
2.7 KiB
Dart
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)
|
|
],
|
|
),
|
|
)
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|