Improving state machine api and including more examples.

This commit is contained in:
Luigi Rosso
2021-04-12 16:57:40 -07:00
parent 55d7996026
commit 2851203e67
15 changed files with 484 additions and 85 deletions

View File

@ -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

View 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!,
),
),
],
),
),
),
);
}
}

View File

@ -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(),
),
);
},
),
],
),
),

View 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,
),
],
),
),
],
),
),
);
}
}