mirror of
https://github.com/rive-app/rive-flutter.git
synced 2025-06-27 10:18:12 +08:00
Adds high level RiveAnimation widget
This commit is contained in:
@ -1,6 +1,5 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import 'package:flutter/widgets.dart';
|
|
||||||
import 'package:rive/rive.dart';
|
import 'package:rive/rive.dart';
|
||||||
|
|
||||||
class ExampleAnimation extends StatefulWidget {
|
class ExampleAnimation extends StatefulWidget {
|
||||||
|
@ -3,6 +3,7 @@ import 'package:rive_example/example_animation.dart';
|
|||||||
import 'package:rive_example/example_state_machine.dart';
|
import 'package:rive_example/example_state_machine.dart';
|
||||||
import 'package:rive_example/liquid_download.dart';
|
import 'package:rive_example/liquid_download.dart';
|
||||||
import 'package:rive_example/little_machine.dart';
|
import 'package:rive_example/little_machine.dart';
|
||||||
|
import 'package:rive_example/simple_animation.dart';
|
||||||
import 'package:rive_example/state_machine_skills.dart';
|
import 'package:rive_example/state_machine_skills.dart';
|
||||||
|
|
||||||
void main() => runApp(MaterialApp(
|
void main() => runApp(MaterialApp(
|
||||||
@ -22,7 +23,21 @@ class Home extends StatelessWidget {
|
|||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: [
|
children: [
|
||||||
ElevatedButton(
|
ElevatedButton(
|
||||||
child: const Text('Animation'),
|
child: const Text('Simple Animation'),
|
||||||
|
onPressed: () {
|
||||||
|
Navigator.push(
|
||||||
|
context,
|
||||||
|
MaterialPageRoute<void>(
|
||||||
|
builder: (context) => const SimpleAnimation(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
const SizedBox(
|
||||||
|
height: 10,
|
||||||
|
),
|
||||||
|
ElevatedButton(
|
||||||
|
child: const Text('Play/Pause Animation'),
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
Navigator.push(
|
Navigator.push(
|
||||||
context,
|
context,
|
||||||
|
22
example/lib/simple_animation.dart
Normal file
22
example/lib/simple_animation.dart
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:rive/rive.dart';
|
||||||
|
|
||||||
|
class SimpleAnimation extends StatelessWidget {
|
||||||
|
const SimpleAnimation({Key? key}) : super(key: key);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Scaffold(
|
||||||
|
appBar: AppBar(
|
||||||
|
title: const Text('Simple Animation'),
|
||||||
|
),
|
||||||
|
body: const Center(
|
||||||
|
// child: RiveAnimation.asset('assets/off_road_car.riv'),
|
||||||
|
child: RiveAnimation.network(
|
||||||
|
'https://cdn.rive.app/animations/truck.riv',
|
||||||
|
fit: BoxFit.cover,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -6,6 +6,8 @@
|
|||||||
<true/>
|
<true/>
|
||||||
<key>com.apple.security.cs.allow-jit</key>
|
<key>com.apple.security.cs.allow-jit</key>
|
||||||
<true/>
|
<true/>
|
||||||
|
<key>com.apple.security.network.client</key>
|
||||||
|
<true/>
|
||||||
<key>com.apple.security.network.server</key>
|
<key>com.apple.security.network.server</key>
|
||||||
<true/>
|
<true/>
|
||||||
</dict>
|
</dict>
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
name: rive_example
|
name: rive_example
|
||||||
description: A new Flutter project.
|
description: A collection of Rive Flutter examples
|
||||||
|
|
||||||
publish_to: "none" # Remove this line if you wish to publish to pub.dev
|
publish_to: "none" # Remove this line if you wish to publish to pub.dev
|
||||||
|
|
||||||
|
@ -21,3 +21,4 @@ export 'package:rive/src/rive_core/shapes/shape.dart';
|
|||||||
export 'package:rive/src/rive_file.dart';
|
export 'package:rive/src/rive_file.dart';
|
||||||
export 'package:rive/src/runtime_artboard.dart';
|
export 'package:rive/src/runtime_artboard.dart';
|
||||||
export 'package:rive/src/state_machine_controller.dart';
|
export 'package:rive/src/state_machine_controller.dart';
|
||||||
|
export 'package:rive/src/widgets/rive_animation.dart';
|
||||||
|
@ -33,9 +33,10 @@ class Rive extends LeafRenderObjectWidget {
|
|||||||
const Rive({
|
const Rive({
|
||||||
required this.artboard,
|
required this.artboard,
|
||||||
this.useArtboardSize = false,
|
this.useArtboardSize = false,
|
||||||
this.fit = BoxFit.contain,
|
BoxFit? fit,
|
||||||
this.alignment = Alignment.center,
|
Alignment? alignment,
|
||||||
});
|
}) : fit = fit ?? BoxFit.contain,
|
||||||
|
alignment = alignment ?? Alignment.center;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
RenderObject createRenderObject(BuildContext context) {
|
RenderObject createRenderObject(BuildContext context) {
|
||||||
|
133
lib/src/widgets/rive_animation.dart
Normal file
133
lib/src/widgets/rive_animation.dart
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
import 'dart:io';
|
||||||
|
import 'dart:typed_data';
|
||||||
|
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:flutter/widgets.dart';
|
||||||
|
import 'package:rive/rive.dart';
|
||||||
|
import 'package:rive/src/rive_core/artboard.dart';
|
||||||
|
|
||||||
|
enum _Source {
|
||||||
|
asset,
|
||||||
|
network,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// High level widget that plays an animation from a Rive file. If artboard or
|
||||||
|
/// animation are not specified, the default artboard and first animation fonund
|
||||||
|
/// within it are used.
|
||||||
|
class RiveAnimation extends StatefulWidget {
|
||||||
|
final String name;
|
||||||
|
final _Source src;
|
||||||
|
final String? artboard;
|
||||||
|
final String? animation;
|
||||||
|
final BoxFit? fit;
|
||||||
|
final Alignment? alignment;
|
||||||
|
|
||||||
|
/// Creates a new RiveAnimation from an asset bundle
|
||||||
|
const RiveAnimation.asset(
|
||||||
|
this.name, {
|
||||||
|
this.artboard,
|
||||||
|
this.animation,
|
||||||
|
this.fit,
|
||||||
|
this.alignment,
|
||||||
|
}) : src = _Source.asset;
|
||||||
|
|
||||||
|
const RiveAnimation.network(
|
||||||
|
this.name, {
|
||||||
|
this.artboard,
|
||||||
|
this.animation,
|
||||||
|
this.fit,
|
||||||
|
this.alignment,
|
||||||
|
}) : src = _Source.network;
|
||||||
|
|
||||||
|
@override
|
||||||
|
_RiveAnimationState createState() => _RiveAnimationState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _RiveAnimationState extends State<RiveAnimation> {
|
||||||
|
/// Rive controller
|
||||||
|
late RiveAnimationController _controller;
|
||||||
|
|
||||||
|
/// Active artboard
|
||||||
|
Artboard? _artboard;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
// Load the Rive file from the asset bundle
|
||||||
|
switch (widget.src) {
|
||||||
|
case _Source.asset:
|
||||||
|
_loadAsset();
|
||||||
|
break;
|
||||||
|
case _Source.network:
|
||||||
|
_loadNetwork();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Loads a Rive file from an asset bundle and configures artboard, animation,
|
||||||
|
/// and controller.
|
||||||
|
void _loadAsset() {
|
||||||
|
rootBundle.load(widget.name).then(
|
||||||
|
(data) async {
|
||||||
|
_init(data);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Loads a Rive file from an HTTP source and configures artboard, animation,
|
||||||
|
/// and controller.
|
||||||
|
void _loadNetwork() {
|
||||||
|
final client = HttpClient();
|
||||||
|
final contents = <int>[];
|
||||||
|
|
||||||
|
client
|
||||||
|
.getUrl(Uri.parse(widget.name))
|
||||||
|
.then(
|
||||||
|
(req) async => req.close(),
|
||||||
|
)
|
||||||
|
.then(
|
||||||
|
(res) => res.listen(
|
||||||
|
contents.addAll,
|
||||||
|
onDone: () {
|
||||||
|
final data = ByteData.view(Uint8List.fromList(contents).buffer);
|
||||||
|
_init(data);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Initializes the artboard, animation, and controller
|
||||||
|
void _init(ByteData data) {
|
||||||
|
final file = RiveFile.import(data);
|
||||||
|
final artboard = widget.artboard != null
|
||||||
|
? file.artboardByName(widget.artboard!)
|
||||||
|
: file.mainArtboard;
|
||||||
|
|
||||||
|
if (artboard == null) {
|
||||||
|
throw const FormatException('Unable to load artboard');
|
||||||
|
}
|
||||||
|
if (artboard.animations.isEmpty) {
|
||||||
|
throw FormatException('No animations in artboard ${artboard.name}');
|
||||||
|
}
|
||||||
|
|
||||||
|
final animationName = widget.animation ?? artboard.animations.first.name;
|
||||||
|
|
||||||
|
artboard.addController(_controller = SimpleAnimation(animationName));
|
||||||
|
setState(() => _artboard = artboard);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_controller.dispose();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) => _artboard != null
|
||||||
|
? Rive(
|
||||||
|
artboard: _artboard!,
|
||||||
|
fit: widget.fit,
|
||||||
|
alignment: widget.alignment,
|
||||||
|
)
|
||||||
|
: const SizedBox();
|
||||||
|
}
|
Reference in New Issue
Block a user