Skip to content

Commit 2b126bc

Browse files
authored
Add Alignment, which will replace FractionalOffset (flutter#12342)
Unlike FractionalOffset, Alignment uses the center as the zero of the coordinate system, which makes the RTL math work out much cleaner. Also, make FractionalOffset into a subclass of Alignment so that clients can continue to use FractionalOffset.
1 parent 89566fe commit 2b126bc

File tree

79 files changed

+1424
-1210
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+1424
-1210
lines changed

packages/flutter/lib/painting.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
/// painting boxes.
1818
library painting;
1919

20+
export 'src/painting/alignment.dart';
2021
export 'src/painting/basic_types.dart';
2122
export 'src/painting/border_radius.dart';
2223
export 'src/painting/borders.dart';

packages/flutter/lib/src/cupertino/nav_bar.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ class _CupertinoLargeTitleNavigationBarSliverDelegate extends SliverPersistentHe
416416
child: new OverflowBox(
417417
minHeight: 0.0,
418418
maxHeight: double.INFINITY,
419-
alignment: FractionalOffsetDirectional.bottomStart,
419+
alignment: AlignmentDirectional.bottomStart,
420420
child: new Padding(
421421
padding: const EdgeInsetsDirectional.only(
422422
start: _kNavBarEdgePadding,

packages/flutter/lib/src/cupertino/route.dart

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ const double _kBackGestureWidth = 20.0;
1111
const double _kMinFlingVelocity = 1.0; // Screen widths per second.
1212

1313
// Fractional offset from offscreen to the right to fully on screen.
14-
final FractionalOffsetTween _kRightMiddleTween = new FractionalOffsetTween(
15-
begin: FractionalOffset.topRight,
16-
end: FractionalOffset.topLeft,
14+
final AlignmentTween _kRightMiddleTween = new AlignmentTween(
15+
begin: Alignment.centerRight * 2.0,
16+
end: Alignment.center,
1717
);
1818

1919
// Fractional offset from fully on screen to 1/3 offscreen to the left.
20-
final FractionalOffsetTween _kMiddleLeftTween = new FractionalOffsetTween(
21-
begin: FractionalOffset.topLeft,
22-
end: const FractionalOffset(-1.0/3.0, 0.0),
20+
final AlignmentTween _kMiddleLeftTween = new AlignmentTween(
21+
begin: Alignment.center,
22+
end: const Alignment(-2.0/3.0, 0.0),
2323
);
2424

2525
// Fractional offset from offscreen below to fully on screen.
26-
final FractionalOffsetTween _kBottomUpTween = new FractionalOffsetTween(
27-
begin: FractionalOffset.bottomLeft,
28-
end: FractionalOffset.topLeft,
26+
final AlignmentTween _kBottomUpTween = new AlignmentTween(
27+
begin: Alignment.bottomCenter * 2.0,
28+
end: Alignment.center,
2929
);
3030

3131
// Custom decoration from no shadow to page shadow mimicking iOS page
@@ -35,8 +35,8 @@ final DecorationTween _kGradientShadowTween = new DecorationTween(
3535
end: const _CupertinoEdgeShadowDecoration(
3636
edgeGradient: const LinearGradient(
3737
// Spans 5% of the page.
38-
begin: const FractionalOffset(0.95, 0.0),
39-
end: FractionalOffset.topRight,
38+
begin: const Alignment(0.90, 0.0),
39+
end: Alignment.centerRight,
4040
// Eyeballed gradient used to mimic a drop shadow on the left side only.
4141
colors: const <Color>[
4242
const Color(0x00000000),
@@ -318,9 +318,9 @@ class CupertinoPageTransition extends StatelessWidget {
318318
super(key: key);
319319

320320
// When this page is coming in to cover another page.
321-
final Animation<FractionalOffset> _primaryPositionAnimation;
321+
final Animation<Alignment> _primaryPositionAnimation;
322322
// When this page is becoming covered by another page.
323-
final Animation<FractionalOffset> _secondaryPositionAnimation;
323+
final Animation<Alignment> _secondaryPositionAnimation;
324324
final Animation<Decoration> _primaryShadowAnimation;
325325

326326
/// The widget below this widget in the tree.
@@ -361,7 +361,7 @@ class CupertinoFullscreenDialogTransition extends StatelessWidget {
361361
),
362362
super(key: key);
363363

364-
final Animation<FractionalOffset> _positionAnimation;
364+
final Animation<Alignment> _positionAnimation;
365365

366366
/// The widget below this widget in the tree.
367367
final Widget child;
@@ -562,7 +562,7 @@ class _CupertinoEdgeShadowDecoration extends Decoration {
562562
const _CupertinoEdgeShadowDecoration();
563563

564564
/// A gradient to draw to the left of the box being decorated.
565-
/// FractionalOffsets are relative to the original box translated one box
565+
/// Alignments are relative to the original box translated one box
566566
/// width to the left.
567567
final LinearGradient edgeGradient;
568568

packages/flutter/lib/src/cupertino/tab_scaffold.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class _CupertinoTabScaffoldState extends State<CupertinoTabScaffold> {
145145

146146
if (widget.tabBar != null) {
147147
stacked.add(new Align(
148-
alignment: FractionalOffset.bottomCenter,
148+
alignment: Alignment.bottomCenter,
149149
// Override the tab bar's currentIndex to the current tab and hook in
150150
// our own listener to update the _currentPage on top of a possibly user
151151
// provided callback.

packages/flutter/lib/src/material/app_bar.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ class _AppBarState extends State<AppBar> {
448448
}
449449

450450
appBar = new Align(
451-
alignment: FractionalOffset.topCenter,
451+
alignment: Alignment.topCenter,
452452
child: appBar,
453453
);
454454

packages/flutter/lib/src/material/bottom_navigation_bar.dart

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,14 @@ class _BottomNavigationBarState extends State<BottomNavigationBar> with TickerPr
215215
return (leftWeights + _flex(_animations[index]) / 2.0) / allWeights;
216216
}
217217

218-
FractionalOffset _circleOffset(int index) {
218+
Alignment _circleOffset(int index) {
219219
final double iconSize = widget.iconSize;
220220
final Tween<double> yOffsetTween = new Tween<double>(
221221
begin: (18.0 + iconSize / 2.0) / kBottomNavigationBarHeight, // 18dp + icon center
222222
end: (6.0 + iconSize / 2.0) / kBottomNavigationBarHeight // 6dp + icon center
223223
);
224224

225-
return new FractionalOffset(
225+
return new Alignment(
226226
_xOffset(index),
227227
yOffsetTween.evaluate(_animations[index])
228228
);
@@ -283,10 +283,10 @@ class _BottomNavigationBarState extends State<BottomNavigationBar> with TickerPr
283283
widget.onTap(i);
284284
},
285285
child: new Stack(
286-
alignment: FractionalOffset.center,
286+
alignment: Alignment.center,
287287
children: <Widget>[
288288
new Align(
289-
alignment: FractionalOffset.topCenter,
289+
alignment: Alignment.topCenter,
290290
child: new Container(
291291
margin: new EdgeInsets.only(
292292
top: new Tween<double>(
@@ -304,7 +304,7 @@ class _BottomNavigationBarState extends State<BottomNavigationBar> with TickerPr
304304
),
305305
),
306306
new Align(
307-
alignment: FractionalOffset.bottomCenter,
307+
alignment: Alignment.bottomCenter,
308308
child: new Container(
309309
margin: const EdgeInsets.only(bottom: 10.0),
310310
child: DefaultTextStyle.merge(
@@ -319,7 +319,7 @@ class _BottomNavigationBarState extends State<BottomNavigationBar> with TickerPr
319319
end: 1.0,
320320
).evaluate(_animations[i]),
321321
)),
322-
alignment: FractionalOffset.bottomCenter,
322+
alignment: Alignment.bottomCenter,
323323
child: widget.items[i].title,
324324
),
325325
),
@@ -352,10 +352,10 @@ class _BottomNavigationBarState extends State<BottomNavigationBar> with TickerPr
352352
widget.onTap(i);
353353
},
354354
child: new Stack(
355-
alignment: FractionalOffset.center,
355+
alignment: Alignment.center,
356356
children: <Widget>[
357357
new Align(
358-
alignment: FractionalOffset.topCenter,
358+
alignment: Alignment.topCenter,
359359
child: new Container(
360360
margin: new EdgeInsets.only(
361361
top: new Tween<double>(
@@ -373,7 +373,7 @@ class _BottomNavigationBarState extends State<BottomNavigationBar> with TickerPr
373373
),
374374
),
375375
new Align(
376-
alignment: FractionalOffset.bottomCenter,
376+
alignment: Alignment.bottomCenter,
377377
child: new Container(
378378
margin: const EdgeInsets.only(bottom: 10.0),
379379
child: new FadeTransition(
@@ -465,7 +465,7 @@ class _Circle {
465465
AnimationController controller;
466466
CurvedAnimation animation;
467467

468-
FractionalOffset get offset {
468+
Alignment get offset {
469469
return state._circleOffset(index);
470470
}
471471

@@ -487,11 +487,13 @@ class _RadialPainter extends CustomPainter {
487487
// bounding rectangle's corners touches the egde of the circle. Drawing a
488488
// circle beyond this radius is futile since there is no perceivable
489489
// difference within the cropped rectangle.
490-
double _maxRadius(FractionalOffset offset, Size size) {
491-
final double dx = offset.dx;
492-
final double dy = offset.dy;
493-
final double x = (dx > 0.5 ? dx : 1.0 - dx) * size.width;
494-
final double y = (dy > 0.5 ? dy : 1.0 - dy) * size.height;
490+
double _maxRadius(Alignment alignment, Size size) {
491+
final double dx = alignment.x;
492+
final double dy = alignment.y;
493+
final double halfWidth = size.width / 2.0;
494+
final double halfHeight = size.height / 2.0;
495+
final double x = halfWidth + dx.abs() * halfWidth;
496+
final double y = halfHeight + dy.abs() * halfHeight;
495497
return math.sqrt(x * x + y * y);
496498
}
497499

@@ -517,20 +519,22 @@ class _RadialPainter extends CustomPainter {
517519
for (_Circle circle in circles) {
518520
final Tween<double> radiusTween = new Tween<double>(
519521
begin: 0.0,
520-
end: _maxRadius(circle.offset, size)
522+
end: _maxRadius(circle.offset, size),
521523
);
522524
final Paint paint = new Paint()..color = circle.color;
523525
final Rect rect = new Rect.fromLTWH(0.0, 0.0, size.width, size.height);
524526
canvas.clipRect(rect);
525527
final double navWidth = math.min(bottomNavMaxWidth, size.width);
528+
final double halfNavWidth = navWidth / 2.0;
529+
final double halfHeight = size.height / 2.0;
526530
final Offset center = new Offset(
527-
(size.width - navWidth) / 2.0 + circle.offset.dx * navWidth,
528-
circle.offset.dy * size.height
531+
(size.width - navWidth) / 2.0 + halfNavWidth + circle.offset.x * halfNavWidth,
532+
halfHeight + circle.offset.y * halfHeight,
529533
);
530534
canvas.drawCircle(
531535
center,
532536
radiusTween.lerp(circle.animation.value),
533-
paint
537+
paint,
534538
);
535539
}
536540
}

packages/flutter/lib/src/material/data_table.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ class DataTable extends StatelessWidget {
408408
label = new Container(
409409
padding: padding,
410410
height: _kHeadingRowHeight,
411-
alignment: numeric ? FractionalOffset.centerRight : FractionalOffsetDirectional.centerStart,
411+
alignment: numeric ? Alignment.centerRight : AlignmentDirectional.centerStart,
412412
child: new AnimatedDefaultTextStyle(
413413
style: new TextStyle(
414414
// TODO(ianh): font family should be Roboto; see https://github.com/flutter/flutter/issues/3116
@@ -460,7 +460,7 @@ class DataTable extends StatelessWidget {
460460
label = new Container(
461461
padding: padding,
462462
height: _kDataRowHeight,
463-
alignment: numeric ? FractionalOffset.centerRight : FractionalOffsetDirectional.centerStart,
463+
alignment: numeric ? Alignment.centerRight : AlignmentDirectional.centerStart,
464464
child: new DefaultTextStyle(
465465
style: new TextStyle(
466466
// TODO(ianh): font family should be Roboto; see https://github.com/flutter/flutter/issues/3116
@@ -774,7 +774,7 @@ class _SortArrowState extends State<_SortArrow> with TickerProviderStateMixin {
774774
child: new Transform(
775775
transform: new Matrix4.rotationZ(_orientationOffset + _orientationAnimation.value)
776776
..setTranslationRaw(0.0, _kArrowIconBaselineOffset, 0.0),
777-
alignment: FractionalOffset.center,
777+
alignment: Alignment.center,
778778
child: new Icon(
779779
Icons.arrow_downward,
780780
size: _kArrowIconSize,

packages/flutter/lib/src/material/drawer.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ class DrawerControllerState extends State<DrawerController> with SingleTickerPro
267267
Widget _buildDrawer(BuildContext context) {
268268
if (_controller.status == AnimationStatus.dismissed) {
269269
return new Align(
270-
alignment: FractionalOffsetDirectional.centerStart,
270+
alignment: AlignmentDirectional.centerStart,
271271
child: new GestureDetector(
272272
key: _gestureDetectorKey,
273273
onHorizontalDragUpdate: _move,
@@ -299,9 +299,9 @@ class DrawerControllerState extends State<DrawerController> with SingleTickerPro
299299
),
300300
),
301301
new Align(
302-
alignment: FractionalOffsetDirectional.centerStart,
302+
alignment: AlignmentDirectional.centerStart,
303303
child: new Align(
304-
alignment: FractionalOffsetDirectional.centerEnd,
304+
alignment: AlignmentDirectional.centerEnd,
305305
widthFactor: _controller.value,
306306
child: new RepaintBoundary(
307307
child: new FocusScope(

packages/flutter/lib/src/material/dropdown.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ class DropdownMenuItem<T> extends StatelessWidget {
361361
Widget build(BuildContext context) {
362362
return new Container(
363363
height: _kMenuItemHeight,
364-
alignment: FractionalOffset.centerLeft,
364+
alignment: Alignment.centerLeft,
365365
child: child,
366366
);
367367
}
@@ -589,7 +589,7 @@ class _DropdownButtonState<T> extends State<DropdownButton<T>> with WidgetsBindi
589589
// the hint or nothing at all.
590590
new IndexedStack(
591591
index: _selectedIndex ?? hintIndex,
592-
alignment: FractionalOffset.centerLeft,
592+
alignment: Alignment.centerLeft,
593593
children: items,
594594
),
595595
new Icon(Icons.arrow_drop_down,

packages/flutter/lib/src/material/flexible_space_bar.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,16 @@ class _FlexibleSpaceBarState extends State<FlexibleSpaceBar> {
9292
return null;
9393
}
9494

95-
FractionalOffset _getTitleAlignment(bool effectiveCenterTitle) {
95+
Alignment _getTitleAlignment(bool effectiveCenterTitle) {
9696
if (effectiveCenterTitle)
97-
return FractionalOffset.bottomCenter;
97+
return Alignment.bottomCenter;
9898
final TextDirection textDirection = Directionality.of(context);
9999
assert(textDirection != null);
100100
switch (textDirection) {
101101
case TextDirection.rtl:
102-
return FractionalOffset.bottomRight;
102+
return Alignment.bottomRight;
103103
case TextDirection.ltr:
104-
return FractionalOffset.bottomLeft;
104+
return Alignment.bottomLeft;
105105
}
106106
return null;
107107
}
@@ -152,7 +152,7 @@ class _FlexibleSpaceBarState extends State<FlexibleSpaceBar> {
152152
final double scaleValue = new Tween<double>(begin: 1.5, end: 1.0).lerp(t);
153153
final Matrix4 scaleTransform = new Matrix4.identity()
154154
..scale(scaleValue, scaleValue, 1.0);
155-
final FractionalOffset titleAlignment = _getTitleAlignment(effectiveCenterTitle);
155+
final Alignment titleAlignment = _getTitleAlignment(effectiveCenterTitle);
156156
children.add(new Container(
157157
padding: new EdgeInsetsDirectional.only(
158158
start: effectiveCenterTitle ? 0.0 : 72.0,

0 commit comments

Comments
 (0)