mirror of
https://github.com/rive-app/rive-flutter.git
synced 2025-06-22 23:07:49 +08:00
Cleanup based on previous PR
This commit is contained in:
@ -9,22 +9,17 @@ class StateMachineAction extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _StateMachineActionState extends State<StateMachineAction> {
|
class _StateMachineActionState extends State<StateMachineAction> {
|
||||||
void _onRiveInit(Artboard artboard) {
|
|
||||||
final controller = StateMachineController.fromArtboard(artboard, 'Switch');
|
|
||||||
artboard.addController(controller!);
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: const Text('Light Switch'),
|
title: const Text('Light Switch'),
|
||||||
),
|
),
|
||||||
body: Center(
|
body: const Center(
|
||||||
child: RiveAnimation.asset(
|
child: RiveAnimation.asset(
|
||||||
'assets/light_switch.riv',
|
'assets/light_switch.riv',
|
||||||
fit: BoxFit.contain,
|
fit: BoxFit.contain,
|
||||||
onInit: _onRiveInit,
|
stateMachines: ['Switch'],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
@ -206,19 +206,21 @@ abstract class RiveRenderBox extends RenderBox {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Mat2D computeAlignment([Offset offset = Offset.zero]) {
|
Mat2D computeAlignment([Offset offset = Offset.zero]) {
|
||||||
AABB bounds = aabb;
|
AABB frame = AABB.fromValues(
|
||||||
|
offset.dx, offset.dy, offset.dx + size.width, offset.dy + size.height);
|
||||||
|
AABB content = aabb;
|
||||||
|
|
||||||
double contentWidth = bounds[2] - bounds[0];
|
double contentWidth = content[2] - content[0];
|
||||||
double contentHeight = bounds[3] - bounds[1];
|
double contentHeight = content[3] - content[1];
|
||||||
|
|
||||||
if (contentWidth == 0 || contentHeight == 0) {
|
if (contentWidth == 0 || contentHeight == 0) {
|
||||||
return Mat2D();
|
return Mat2D();
|
||||||
}
|
}
|
||||||
|
|
||||||
double x = -1 * bounds[0] -
|
double x = -1 * content[0] -
|
||||||
contentWidth / 2.0 -
|
contentWidth / 2.0 -
|
||||||
(_alignment.x * contentWidth / 2.0);
|
(_alignment.x * contentWidth / 2.0);
|
||||||
double y = -1 * bounds[1] -
|
double y = -1 * content[1] -
|
||||||
contentHeight / 2.0 -
|
contentHeight / 2.0 -
|
||||||
(_alignment.y * contentHeight / 2.0);
|
(_alignment.y * contentHeight / 2.0);
|
||||||
|
|
||||||
@ -226,25 +228,25 @@ abstract class RiveRenderBox extends RenderBox {
|
|||||||
|
|
||||||
switch (_fit) {
|
switch (_fit) {
|
||||||
case BoxFit.fill:
|
case BoxFit.fill:
|
||||||
scaleX = size.width / contentWidth;
|
scaleX = frame.width / contentWidth;
|
||||||
scaleY = size.height / contentHeight;
|
scaleY = frame.height / contentHeight;
|
||||||
break;
|
break;
|
||||||
case BoxFit.contain:
|
case BoxFit.contain:
|
||||||
double minScale =
|
double minScale =
|
||||||
min(size.width / contentWidth, size.height / contentHeight);
|
min(frame.width / contentWidth, frame.height / contentHeight);
|
||||||
scaleX = scaleY = minScale;
|
scaleX = scaleY = minScale;
|
||||||
break;
|
break;
|
||||||
case BoxFit.cover:
|
case BoxFit.cover:
|
||||||
double maxScale =
|
double maxScale =
|
||||||
max(size.width / contentWidth, size.height / contentHeight);
|
max(frame.width / contentWidth, frame.height / contentHeight);
|
||||||
scaleX = scaleY = maxScale;
|
scaleX = scaleY = maxScale;
|
||||||
break;
|
break;
|
||||||
case BoxFit.fitHeight:
|
case BoxFit.fitHeight:
|
||||||
double minScale = size.height / contentHeight;
|
double minScale = frame.height / contentHeight;
|
||||||
scaleX = scaleY = minScale;
|
scaleX = scaleY = minScale;
|
||||||
break;
|
break;
|
||||||
case BoxFit.fitWidth:
|
case BoxFit.fitWidth:
|
||||||
double minScale = size.width / contentWidth;
|
double minScale = frame.width / contentWidth;
|
||||||
scaleX = scaleY = minScale;
|
scaleX = scaleY = minScale;
|
||||||
break;
|
break;
|
||||||
case BoxFit.none:
|
case BoxFit.none:
|
||||||
@ -252,15 +254,15 @@ abstract class RiveRenderBox extends RenderBox {
|
|||||||
break;
|
break;
|
||||||
case BoxFit.scaleDown:
|
case BoxFit.scaleDown:
|
||||||
double minScale =
|
double minScale =
|
||||||
min(size.width / contentWidth, size.height / contentHeight);
|
min(frame.width / contentWidth, frame.height / contentHeight);
|
||||||
scaleX = scaleY = minScale < 1.0 ? minScale : 1.0;
|
scaleX = scaleY = minScale < 1.0 ? minScale : 1.0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
Mat2D transform = Mat2D();
|
Mat2D transform = Mat2D();
|
||||||
|
|
||||||
transform[4] = size.width / 2.0 + (_alignment.x * size.width / 2.0);
|
transform[4] = frame.width / 2.0 + (_alignment.x * frame.width / 2.0);
|
||||||
transform[5] = size.height / 2.0 + (_alignment.y * size.height / 2.0);
|
transform[5] = frame.height / 2.0 + (_alignment.y * frame.height / 2.0);
|
||||||
if (offsetViewTransform) {
|
if (offsetViewTransform) {
|
||||||
transform[4] += offset.dx;
|
transform[4] += offset.dx;
|
||||||
transform[5] += offset.dy;
|
transform[5] += offset.dy;
|
||||||
|
Reference in New Issue
Block a user