mirror of
https://github.com/rive-app/rive-flutter.git
synced 2025-06-27 10:18:12 +08:00
Improving state machine api and including more examples.
This commit is contained in:
@ -3,6 +3,7 @@ import 'package:flutter/services.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:rive/rive.dart';
|
||||
|
||||
/// An example showing how to drive two boolean state machine inputs.
|
||||
class ExampleStateMachine extends StatefulWidget {
|
||||
const ExampleStateMachine({Key? key}) : super(key: key);
|
||||
|
||||
@ -16,8 +17,8 @@ class _ExampleStateMachineState extends State<ExampleStateMachine> {
|
||||
|
||||
Artboard? _riveArtboard;
|
||||
StateMachineController? _controller;
|
||||
StateMachineInput<bool>? _hoverInput;
|
||||
StateMachineInput<bool>? _pressInput;
|
||||
SMIInput<bool>? _hoverInput;
|
||||
SMIInput<bool>? _pressInput;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@ -50,7 +51,7 @@ class _ExampleStateMachineState extends State<ExampleStateMachine> {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.grey,
|
||||
appBar: AppBar(
|
||||
title: const Text('State Machine Example'),
|
||||
title: const Text('Button State Machine'),
|
||||
),
|
||||
body: Center(
|
||||
child: _riveArtboard == null
|
||||
|
80
example/lib/little_machine.dart
Normal file
80
example/lib/little_machine.dart
Normal file
@ -0,0 +1,80 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:rive/rive.dart';
|
||||
|
||||
/// An example showing how to drive a StateMachine via a trigger input.
|
||||
class LittleMachine extends StatefulWidget {
|
||||
const LittleMachine({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
_LittleMachineState createState() => _LittleMachineState();
|
||||
}
|
||||
|
||||
class _LittleMachineState extends State<LittleMachine> {
|
||||
/// Tracks if the animation is playing by whether controller is running.
|
||||
bool get isPlaying => _controller?.isActive ?? false;
|
||||
|
||||
Artboard? _riveArtboard;
|
||||
StateMachineController? _controller;
|
||||
SMIInput<bool>? _trigger;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
// Load the animation file from the bundle, note that you could also
|
||||
// download this. The RiveFile just expects a list of bytes.
|
||||
rootBundle.load('assets/little_machine.riv').then(
|
||||
(data) async {
|
||||
// Load the RiveFile from the binary data.
|
||||
final file = RiveFile.import(data);
|
||||
|
||||
// The artboard is the root of the animation and gets drawn in the
|
||||
// Rive widget.
|
||||
final artboard = file.mainArtboard;
|
||||
var controller =
|
||||
StateMachineController.fromArtboard(artboard, 'State Machine 1');
|
||||
if (controller != null) {
|
||||
artboard.addController(controller);
|
||||
_trigger = controller.findInput('Trigger 1');
|
||||
}
|
||||
setState(() => _riveArtboard = artboard);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.grey,
|
||||
appBar: AppBar(
|
||||
title: const Text('Little Machine'),
|
||||
),
|
||||
body: Center(
|
||||
child: _riveArtboard == null
|
||||
? const SizedBox()
|
||||
: GestureDetector(
|
||||
onTapDown: (_) => _trigger?.value = true,
|
||||
child: Column(
|
||||
children: [
|
||||
const SizedBox(height: 10),
|
||||
const Text(
|
||||
'Press to activate!',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
Expanded(
|
||||
child: Rive(
|
||||
artboard: _riveArtboard!,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:rive_example/example_animation.dart';
|
||||
import 'package:rive_example/example_state_machine.dart';
|
||||
import 'package:rive_example/little_machine.dart';
|
||||
import 'package:rive_example/state_machine_skills.dart';
|
||||
|
||||
void main() => runApp(MaterialApp(
|
||||
title: 'Navigation Basics',
|
||||
@ -33,7 +35,7 @@ class Home extends StatelessWidget {
|
||||
height: 10,
|
||||
),
|
||||
ElevatedButton(
|
||||
child: const Text('State Machine'),
|
||||
child: const Text('Button State Machine'),
|
||||
onPressed: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
@ -43,6 +45,34 @@ class Home extends StatelessWidget {
|
||||
);
|
||||
},
|
||||
),
|
||||
const SizedBox(
|
||||
height: 10,
|
||||
),
|
||||
ElevatedButton(
|
||||
child: const Text('Skills Machine'),
|
||||
onPressed: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute<void>(
|
||||
builder: (context) => const StateMachineSkills(),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
const SizedBox(
|
||||
height: 10,
|
||||
),
|
||||
ElevatedButton(
|
||||
child: const Text('Little Machine'),
|
||||
onPressed: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute<void>(
|
||||
builder: (context) => const LittleMachine(),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
92
example/lib/state_machine_skills.dart
Normal file
92
example/lib/state_machine_skills.dart
Normal file
@ -0,0 +1,92 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:rive/rive.dart';
|
||||
|
||||
/// An example showing how to drive a StateMachine via one numeric input.
|
||||
class StateMachineSkills extends StatefulWidget {
|
||||
const StateMachineSkills({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
_StateMachineSkillsState createState() => _StateMachineSkillsState();
|
||||
}
|
||||
|
||||
class _StateMachineSkillsState extends State<StateMachineSkills> {
|
||||
/// Tracks if the animation is playing by whether controller is running.
|
||||
bool get isPlaying => _controller?.isActive ?? false;
|
||||
|
||||
Artboard? _riveArtboard;
|
||||
StateMachineController? _controller;
|
||||
SMIInput<double>? _levelInput;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
// Load the animation file from the bundle, note that you could also
|
||||
// download this. The RiveFile just expects a list of bytes.
|
||||
rootBundle.load('assets/skills.riv').then(
|
||||
(data) async {
|
||||
// Load the RiveFile from the binary data.
|
||||
final file = RiveFile.import(data);
|
||||
|
||||
// The artboard is the root of the animation and gets drawn in the
|
||||
// Rive widget.
|
||||
final artboard = file.mainArtboard;
|
||||
var controller =
|
||||
StateMachineController.fromArtboard(artboard, 'Designer\'s Test');
|
||||
if (controller != null) {
|
||||
artboard.addController(controller);
|
||||
_levelInput = controller.findInput('Level');
|
||||
}
|
||||
setState(() => _riveArtboard = artboard);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.grey,
|
||||
appBar: AppBar(
|
||||
title: const Text('Skills Machine'),
|
||||
),
|
||||
body: Center(
|
||||
child: _riveArtboard == null
|
||||
? const SizedBox()
|
||||
: Stack(
|
||||
children: [
|
||||
Positioned.fill(
|
||||
child: Rive(
|
||||
artboard: _riveArtboard!,
|
||||
),
|
||||
),
|
||||
Positioned.fill(
|
||||
bottom: 100,
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
ElevatedButton(
|
||||
child: const Text('Beginner'),
|
||||
onPressed: () => _levelInput?.value = 0,
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
ElevatedButton(
|
||||
child: const Text('Intermediate'),
|
||||
onPressed: () => _levelInput?.value = 1,
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
ElevatedButton(
|
||||
child: const Text('Expert'),
|
||||
onPressed: () => _levelInput?.value = 2,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user