Skip to content

Commit aa88f07

Browse files
[ADJUSTMENT] - Repeat size snapping changes to PixelSnapCenter and PixelSnapAlign (forgot to do this). (#68)
1 parent 52ff903 commit aa88f07

File tree

2 files changed

+105
-114
lines changed

2 files changed

+105
-114
lines changed

lib/src/flutter/flutter_pixel_alignment.dart

Lines changed: 103 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,10 @@ class RenderPositionedBoxAtPixel extends RenderPositionedBox {
9696
);
9797
}
9898

99-
if (_snapSize &&
100-
(child.size.width != child.size.width.floorToDouble() ||
101-
child.size.height != child.size.height.floorToDouble())) {
99+
if (_snapSize && (!child.size.isInteger)) {
102100
// This child doesn't have an integer width/height - run layout again,
103-
// forcing the nearest smaller size.
104-
child.layout(
105-
BoxConstraints.tightFor(
106-
width: child.size.width.floorToDouble(),
107-
height: child.size.height.floorToDouble(),
108-
),
109-
);
101+
// forcing an integer size. Prefer to expand, but fallback to shrink.
102+
_resizeToIntegerSizeStartingWithWidth(constraints, child);
110103
}
111104
}
112105
}
@@ -310,131 +303,129 @@ class RenderPixelSnapFlex extends RenderFlex {
310303
// For this reason, we treat Columns and Rows differently for re-sizing - we
311304
// try to fit the cross-axis first, and then the main-axis second.
312305
if (direction == Axis.vertical) {
313-
_resizeChildForColumn(child);
306+
_resizeToIntegerSizeStartingWithWidth(constraints, child);
314307
} else {
315-
_resizeChildForRow(child);
308+
_resizeToIntegerSizeStartingWithHeight(constraints, child);
316309
}
317310
}
318311
}
319312
}
313+
}
320314

321-
void _resizeChildForColumn(RenderBox child) {
322-
if (child.size.isInteger) {
323-
return;
324-
}
315+
void _resizeToIntegerSizeStartingWithWidth(BoxConstraints constraints, RenderBox child) {
316+
if (child.size.isInteger) {
317+
return;
318+
}
325319

326-
// This is a column. Start by working on the cross-axis, which is more likely
327-
// to be bounded than the main-axis.
328-
final widest = constraints.biggest.width;
329-
late final int newWidth;
330-
late final bool didShrinkWidth;
331-
if (!child.size.widthIsInteger) {
332-
if (child.size.width <= widest) {
333-
// We prefer to increase size rather than decrease size, because text
334-
// wrapping becomes a problem when shrinking intrinsic size. We can fit
335-
// a bigger width, so use that.
336-
newWidth = child.size.width.ceil();
337-
didShrinkWidth = false;
338-
} else {
339-
// We prefer wider over narrower, but we can't fit any wider. Shrink instead.
340-
newWidth = child.size.width.floor();
341-
didShrinkWidth = true;
342-
}
343-
} else {
344-
newWidth = child.size.width.toInt();
320+
// Start with width.
321+
final widest = constraints.biggest.width;
322+
late final int newWidth;
323+
late final bool didShrinkWidth;
324+
if (!child.size.widthIsInteger) {
325+
if (child.size.width <= widest) {
326+
// We prefer to increase size rather than decrease size, because text
327+
// wrapping becomes a problem when shrinking intrinsic size. We can fit
328+
// a bigger width, so use that.
329+
newWidth = child.size.width.ceil();
345330
didShrinkWidth = false;
331+
} else {
332+
// We prefer wider over narrower, but we can't fit any wider. Shrink instead.
333+
newWidth = child.size.width.floor();
334+
didShrinkWidth = true;
346335
}
336+
} else {
337+
newWidth = child.size.width.toInt();
338+
didShrinkWidth = false;
339+
}
347340

348-
if (didShrinkWidth) {
349-
// Shrinking the width has a non-trivial chance of significantly impacting the
350-
// height, so run layout again with the new width and then deal with the height.
351-
child.layout(BoxConstraints.tightFor(width: newWidth.toDouble()), parentUsesSize: true);
352-
}
341+
if (didShrinkWidth) {
342+
// Shrinking the width has a non-trivial chance of significantly impacting the
343+
// height, so run layout again with the new width and then deal with the height.
344+
child.layout(BoxConstraints.tightFor(width: newWidth.toDouble()), parentUsesSize: true);
345+
}
353346

354-
// Now do the main-axis.
355-
final tallest = constraints.biggest.height;
356-
late final int newHeight;
357-
if (!child.size.heightIsInteger) {
358-
if (child.size.height <= tallest) {
359-
newHeight = child.size.height.ceil();
360-
} else {
361-
newHeight = child.size.height.floor();
362-
}
347+
// Now do the main-axis.
348+
final tallest = constraints.biggest.height;
349+
late final int newHeight;
350+
if (!child.size.heightIsInteger) {
351+
if (child.size.height <= tallest) {
352+
newHeight = child.size.height.ceil();
363353
} else {
364-
newHeight = child.size.height.toInt();
354+
newHeight = child.size.height.floor();
365355
}
366-
367-
// Note: This layout process can fail if a situation arises in which both the
368-
// width and height need to contract, or if contracting the width produces a
369-
// much taller height that violates constraints. If this happens to you, please
370-
// file an issue on GitHub for flutter_test_goldens and provide us with the exact
371-
// situation that's breaking for you.
372-
child.layout(
373-
BoxConstraints.tightFor(
374-
width: newWidth.toDouble(),
375-
height: newHeight.toDouble(),
376-
),
377-
);
356+
} else {
357+
newHeight = child.size.height.toInt();
378358
}
379359

380-
void _resizeChildForRow(RenderBox child) {
381-
if (child.size.isInteger) {
382-
return;
383-
}
360+
// Note: This layout process can fail if a situation arises in which both the
361+
// width and height need to contract, or if contracting the width produces a
362+
// much taller height that violates constraints. If this happens to you, please
363+
// file an issue on GitHub for flutter_test_goldens and provide us with the exact
364+
// situation that's breaking for you.
365+
child.layout(
366+
BoxConstraints.tightFor(
367+
width: newWidth.toDouble(),
368+
height: newHeight.toDouble(),
369+
),
370+
);
371+
}
384372

385-
// This is a row. Start by working on the cross-axis, which is more likely
386-
// to be bounded than the main-axis.
387-
final tallest = constraints.biggest.height;
388-
late final int newHeight;
389-
late final bool didShrinkHeight;
390-
if (!child.size.heightIsInteger) {
391-
if (child.size.height <= tallest) {
392-
// We prefer to increase size rather than decrease size, because text
393-
// wrapping (and other layouts) becomes a problem when shrinking intrinsic
394-
// size. We can fit a bigger height, so use that.
395-
newHeight = child.size.height.ceil();
396-
didShrinkHeight = false;
397-
} else {
398-
// We prefer taller over shorter, but we can't fit any taller. Shrink instead.
399-
newHeight = child.size.height.floor();
400-
didShrinkHeight = true;
401-
}
402-
} else {
403-
newHeight = child.size.height.toInt();
373+
void _resizeToIntegerSizeStartingWithHeight(BoxConstraints constraints, RenderBox child) {
374+
if (child.size.isInteger) {
375+
return;
376+
}
377+
378+
// Start with the height.
379+
final tallest = constraints.biggest.height;
380+
late final int newHeight;
381+
late final bool didShrinkHeight;
382+
if (!child.size.heightIsInteger) {
383+
if (child.size.height <= tallest) {
384+
// We prefer to increase size rather than decrease size, because text
385+
// wrapping (and other layouts) becomes a problem when shrinking intrinsic
386+
// size. We can fit a bigger height, so use that.
387+
newHeight = child.size.height.ceil();
404388
didShrinkHeight = false;
389+
} else {
390+
// We prefer taller over shorter, but we can't fit any taller. Shrink instead.
391+
newHeight = child.size.height.floor();
392+
didShrinkHeight = true;
405393
}
394+
} else {
395+
newHeight = child.size.height.toInt();
396+
didShrinkHeight = false;
397+
}
406398

407-
if (didShrinkHeight) {
408-
// Shrinking the height has a non-trivial chance of significantly impacting the
409-
// width, so run layout again with the new height and then deal with the width.
410-
child.layout(BoxConstraints.tightFor(height: newHeight.toDouble()), parentUsesSize: true);
411-
}
399+
if (didShrinkHeight) {
400+
// Shrinking the height has a non-trivial chance of significantly impacting the
401+
// width, so run layout again with the new height and then deal with the width.
402+
child.layout(BoxConstraints.tightFor(height: newHeight.toDouble()), parentUsesSize: true);
403+
}
412404

413-
// Now do the main-axis.
414-
final widest = constraints.biggest.width;
415-
late final int newWidth;
416-
if (!child.size.widthIsInteger) {
417-
if (child.size.width <= widest) {
418-
newWidth = child.size.width.ceil();
419-
} else {
420-
newWidth = child.size.width.floor();
421-
}
405+
// Now do the main-axis.
406+
final widest = constraints.biggest.width;
407+
late final int newWidth;
408+
if (!child.size.widthIsInteger) {
409+
if (child.size.width <= widest) {
410+
newWidth = child.size.width.ceil();
422411
} else {
423-
newWidth = child.size.width.toInt();
412+
newWidth = child.size.width.floor();
424413
}
425-
426-
// Note: This layout process can fail if a situation arises in which both the
427-
// width and height need to contract, or if contracting the width produces a
428-
// much taller height that violates constraints. If this happens to you, please
429-
// file an issue on GitHub for flutter_test_goldens and provide us with the exact
430-
// situation that's breaking for you.
431-
child.layout(
432-
BoxConstraints.tightFor(
433-
width: newWidth.toDouble(),
434-
height: newHeight.toDouble(),
435-
),
436-
);
414+
} else {
415+
newWidth = child.size.width.toInt();
437416
}
417+
418+
// Note: This layout process can fail if a situation arises in which both the
419+
// width and height need to contract, or if contracting the width produces a
420+
// much taller height that violates constraints. If this happens to you, please
421+
// file an issue on GitHub for flutter_test_goldens and provide us with the exact
422+
// situation that's breaking for you.
423+
child.layout(
424+
BoxConstraints.tightFor(
425+
width: newWidth.toDouble(),
426+
height: newHeight.toDouble(),
427+
),
428+
);
438429
}
439430

440431
extension on Size {

test/flutter/pixel_snapping_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void main() {
3535
tester.getTopLeft(find.byKey(contentKey)),
3636
Offset(25, 25),
3737
);
38-
expect(tester.getSize(find.byKey(contentKey)), Size(49, 49));
38+
expect(tester.getSize(find.byKey(contentKey)), Size(50, 50));
3939
});
4040

4141
testWidgets("PixelSnapAlign", (tester) async {
@@ -69,7 +69,7 @@ void main() {
6969
tester.getTopLeft(find.byKey(contentKey)),
7070
Offset(13, 13),
7171
);
72-
expect(tester.getSize(find.byKey(contentKey)), Size(49, 49));
72+
expect(tester.getSize(find.byKey(contentKey)), Size(50, 50));
7373
});
7474

7575
testWidgets("PixelSnapRow", (tester) async {

0 commit comments

Comments
 (0)