Skip to content

Commit

Permalink
feat: Create a specific utility to Transform.rotate (#434)
Browse files Browse the repository at this point in the history
* feat: create a specific utility to Transform.rotate

* Update transform_widget_modifier_test.dart
  • Loading branch information
tilucasoli authored Aug 8, 2024
1 parent d7533bd commit a3066ee
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 10 deletions.
25 changes: 18 additions & 7 deletions packages/mix/lib/src/modifiers/transform_widget_modifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:mix_annotations/mix_annotations.dart';
import 'dart:math' as math;

import '../core/attribute.dart';
import '../core/factory/mix_data.dart';
Expand Down Expand Up @@ -39,7 +40,7 @@ final class TransformModifierSpec

final class TransformModifierSpecUtility<T extends Attribute>
extends MixUtility<T, TransformModifierSpecAttribute> {
const TransformModifierSpecUtility(super.builder);
TransformModifierSpecUtility(super.builder);

T _flip(bool x, bool y) => builder(
TransformModifierSpecAttribute(
Expand All @@ -65,10 +66,20 @@ final class TransformModifierSpecUtility<T extends Attribute>
),
);

T rotate(double value) => builder(
TransformModifierSpecAttribute(
transform: Matrix4.rotationZ(value),
alignment: Alignment.center,
),
);
late final rotate = TransformRotateModifierSpecUtility(
(value) => TransformModifierSpecAttribute(
transform: value,
alignment: Alignment.center,
),
);
}

final class TransformRotateModifierSpecUtility<T extends Attribute>
extends MixUtility<T, Matrix4> {
const TransformRotateModifierSpecUtility(super.builder);
T d90() => call(math.pi / 2);
T d180() => call(math.pi);
T d270() => call(3 * math.pi / 2);

T call(double value) => builder(Matrix4.rotationZ(value));
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:math' as math;

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
Expand Down Expand Up @@ -136,9 +138,57 @@ void main() {
final utility =
TransformModifierSpecUtility<TransformModifierSpecAttribute>(
(attr) => attr);
final result = utility.rotate(0.5);
expect(result.transform, Matrix4.rotationZ(0.5));
final result = utility.rotate.d90();

expect(result.transform, Matrix4.rotationZ(math.pi / 2));
expect(result.alignment, Alignment.center);

final spec = result.resolve(EmptyMixData);
expect(spec.transform, Matrix4.rotationZ(math.pi / 2));
});
});

group('TransformRotateModifierSpecUtility', () {
late TransformRotateModifierSpecUtility<TransformModifierSpecAttribute>
utility;

setUp(() {
utility = TransformRotateModifierSpecUtility(
(value) => TransformModifierSpecAttribute(
transform: value,
alignment: Alignment.center,
),
);
});

test('d90 returns correct rotation', () {
final result = utility.d90();
expect(result.transform, Matrix4.rotationZ(math.pi / 2));
});

test('d180 returns correct rotation', () {
final result = utility.d180();
expect(result.transform, Matrix4.rotationZ(math.pi));
});

test('d270 returns correct rotation', () {
final result = utility.d270();
expect(result.transform, Matrix4.rotationZ(3 * math.pi / 2));
});

test('call with custom value returns correct rotation', () {
final result = utility.call(math.pi / 4);
expect(result.transform, Matrix4.rotationZ(math.pi / 4));
});

test('call with zero returns identity matrix', () {
final result = utility.call(0);
expect(result.transform, Matrix4.identity());
});

test('call with negative value returns correct rotation', () {
final result = utility.call(-math.pi / 2);
expect(result.transform, Matrix4.rotationZ(-math.pi / 2));
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void main() {
AspectRatioModifierSpecUtility(UtilityTestAttribute.new);
const flexible = FlexibleModifierSpecUtility(UtilityTestAttribute.new);
const visibility = VisibilityModifierSpecUtility(UtilityTestAttribute.new);
const transform = TransformModifierSpecUtility(UtilityTestAttribute.new);
final transform = TransformModifierSpecUtility(UtilityTestAttribute.new);

const opacity = OpacityModifierSpecUtility(UtilityTestAttribute.new);
const rotate = RotatedBoxModifierSpecUtility(UtilityTestAttribute.new);
Expand Down

0 comments on commit a3066ee

Please sign in to comment.