Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Add transform flip (#116705)
Browse files Browse the repository at this point in the history
* Added Transform.flip

* Add tests for Transform.flip

* fix: failing tests

* fix the failing test

* fix test according to review

* Make z component constant, update doc

* Disabled changing alignment

* Apply suggested definition

Co-authored-by: Greg Spencer <gspencergoog@users.noreply.github.com>

Co-authored-by: nayeemtby <nayeemtby@gmail.com>
Co-authored-by: Nayeem Hasan <71181265+nayeemtby@users.noreply.github.com>
Co-authored-by: Greg Spencer <gspencergoog@users.noreply.github.com>
  • Loading branch information
4 people committed Jan 21, 2023
1 parent b1f4070 commit 8065887
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
30 changes: 30 additions & 0 deletions packages/flutter/lib/src/widgets/basic.dart
Expand Up @@ -1424,6 +1424,36 @@ class Transform extends SingleChildRenderObjectWidget {
assert(scale == null || (scaleX == null && scaleY == null), "If 'scale' is non-null then 'scaleX' and 'scaleY' must be left null"),
transform = Matrix4.diagonal3Values(scale ?? scaleX ?? 1.0, scale ?? scaleY ?? 1.0, 1.0);

/// Creates a widget that mirrors its child about the widget's center point.
///
/// If `flipX` is true, the child widget will be flipped horizontally. Defaults to false.
///
/// If `flipY` is true, the child widget will be flipped vertically. Defaults to false.
///
/// If both are true, the child widget will be flipped both vertically and horizontally, equivalent to a 180 degree rotation.
///
/// {@tool snippet}
///
/// This example flips the text horizontally.
///
/// ```dart
/// Transform.flip(
/// flipX: true,
/// child: const Text('Horizontal Flip'),
/// )
/// ```
/// {@end-tool}
Transform.flip({
super.key,
bool flipX = false,
bool flipY = false,
this.origin,
this.transformHitTests = true,
this.filterQuality,
super.child,
}) : alignment = Alignment.center,
transform = Matrix4.diagonal3Values(flipX ? -1.0 : 1.0, flipY ? -1.0 : 1.0, 1.0);

// Computes a rotation matrix for an angle in radians, attempting to keep rotations
// at integral values for angles of 0, π/2, π, 3π/2.
static Matrix4 _computeRotation(double radians) {
Expand Down
95 changes: 95 additions & 0 deletions packages/flutter/test/widgets/transform_test.dart
Expand Up @@ -787,6 +787,101 @@ void main() {

expect(tester.getBottomRight(find.byType(Container)), target.bottomRight(tester.getTopLeft(find.byType(Container))));
});

testWidgets(
'Transform.flip does flip child correctly',
(WidgetTester tester) async {
const Offset topRight = Offset(60, 20);
const Offset bottomLeft = Offset(20, 60);
const Offset bottomRight = Offset(60, 60);

bool tappedRed = false;

const Widget square = SizedBox.square(dimension: 40);
final Widget child = Column(
mainAxisSize: MainAxisSize.min,
children: <Widget> [
Row(mainAxisSize: MainAxisSize.min, children: <Widget>[
GestureDetector(
onTap: () => tappedRed = true,
child: const ColoredBox(color: Color(0xffff0000), child: square),
),
const ColoredBox(color: Color(0xff00ff00), child: square),
]),
Row(mainAxisSize: MainAxisSize.min, children: const <Widget>[
ColoredBox(color: Color(0xff0000ff), child: square),
ColoredBox(color: Color(0xffeeff00), child: square),
]),
],
);

await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: Align(
alignment: Alignment.topLeft,
child: Transform.flip(
flipX: true,
child: child,
),
),
),
);

await tester.pumpAndSettle();

await tester.tapAt(topRight);

expect(tappedRed, isTrue, reason: 'Transform.flip cannot flipX');

tappedRed = false;

await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: Align(
alignment: Alignment.topLeft,
child: Transform.flip(
flipY: true,
child: child,
),
),
),
);

await tester.pumpAndSettle();

await tester.tapAt(bottomLeft);

expect(tappedRed, isTrue, reason: 'Transform.flip cannot flipY');

tappedRed = false;

await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: Align(
alignment: Alignment.topLeft,
child: Transform.flip(
flipX: true,
flipY: true,
child: child,
),
),
),
);

await tester.pumpAndSettle();

await tester.tapAt(bottomRight);

expect(
tappedRed,
isTrue,
reason: 'Transform.flip cannot flipX and flipY together',
);
},
);
}

class TestRectPainter extends CustomPainter {
Expand Down

0 comments on commit 8065887

Please sign in to comment.