Skip to content

Commit

Permalink
feat: provide a way to set the initial child's alignment (#114745)
Browse files Browse the repository at this point in the history
InteractiveViewer.alignment parameter
  • Loading branch information
pedromassango authored Nov 7, 2022
1 parent 3cde69e commit e1adc96
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/flutter/lib/src/widgets/interactive_viewer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class InteractiveViewer extends StatefulWidget {
this.scaleEnabled = true,
this.scaleFactor = 200.0,
this.transformationController,
this.alignment,
required Widget this.child,
}) : assert(alignPanAxis != null),
assert(panAxis != null),
Expand Down Expand Up @@ -141,6 +142,7 @@ class InteractiveViewer extends StatefulWidget {
this.scaleEnabled = true,
this.scaleFactor = 200.0,
this.transformationController,
this.alignment,
required InteractiveViewerWidgetBuilder this.builder,
}) : assert(panAxis != null),
assert(builder != null),
Expand All @@ -166,6 +168,9 @@ class InteractiveViewer extends StatefulWidget {
constrained = false,
child = null;

/// The alignment of the child's origin, relative to the size of the box.
final Alignment? alignment;

/// If set to [Clip.none], the child may extend beyond the size of the InteractiveViewer,
/// but it will not receive gestures in these areas.
/// Be sure that the InteractiveViewer is the desired size when using [Clip.none].
Expand Down Expand Up @@ -1096,6 +1101,7 @@ class _InteractiveViewerState extends State<InteractiveViewer> with TickerProvid
clipBehavior: widget.clipBehavior,
constrained: widget.constrained,
matrix: _transformationController!.value,
alignment: widget.alignment,
child: widget.child!,
);
} else {
Expand All @@ -1110,6 +1116,7 @@ class _InteractiveViewerState extends State<InteractiveViewer> with TickerProvid
childKey: _childKey,
clipBehavior: widget.clipBehavior,
constrained: widget.constrained,
alignment: widget.alignment,
matrix: matrix,
child: widget.builder!(
context,
Expand Down Expand Up @@ -1143,18 +1150,21 @@ class _InteractiveViewerBuilt extends StatelessWidget {
required this.clipBehavior,
required this.constrained,
required this.matrix,
required this.alignment,
});

final Widget child;
final GlobalKey childKey;
final Clip clipBehavior;
final bool constrained;
final Matrix4 matrix;
final Alignment? alignment;

@override
Widget build(BuildContext context) {
Widget child = Transform(
transform: matrix,
alignment: alignment,
child: KeyedSubtree(
key: childKey,
child: this.child,
Expand Down
16 changes: 16 additions & 0 deletions packages/flutter/test/widgets/interactive_viewer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1650,6 +1650,22 @@ void main() {
expect(scaleHighZoomedIn - scaleHighZoomedOut, lessThan(scaleZoomedIn - scaleZoomedOut));
});

testWidgets('alignment argument is used properly', (WidgetTester tester) async {
const Alignment alignment = Alignment.center;

await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: InteractiveViewer(
alignment: alignment,
child: Container(),
),
),
));

final Transform transform = tester.firstWidget(find.byType(Transform));
expect(transform.alignment, alignment);
});

testWidgets('interactionEndFrictionCoefficient', (WidgetTester tester) async {
// Use the default interactionEndFrictionCoefficient.
final TransformationController transformationController1 = TransformationController();
Expand Down

0 comments on commit e1adc96

Please sign in to comment.