| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:math' as math; | 5 import 'dart:math' as math; |
| 6 | 6 |
| 7 import 'box.dart'; | 7 import 'box.dart'; |
| 8 import 'object.dart'; | 8 import 'object.dart'; |
| 9 | 9 |
| 10 class FlexBoxParentData extends BoxParentData with ContainerParentDataMixin<Rend
erBox> { | 10 class FlexBoxParentData extends BoxParentData with ContainerParentDataMixin<Rend
erBox> { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 end, | 26 end, |
| 27 center, | 27 center, |
| 28 spaceBetween, | 28 spaceBetween, |
| 29 spaceAround, | 29 spaceAround, |
| 30 } | 30 } |
| 31 | 31 |
| 32 enum FlexAlignItems { | 32 enum FlexAlignItems { |
| 33 start, | 33 start, |
| 34 end, | 34 end, |
| 35 center, | 35 center, |
| 36 stretch, |
| 36 } | 37 } |
| 37 | 38 |
| 38 typedef double _ChildSizingFunction(RenderBox child, BoxConstraints constraints)
; | 39 typedef double _ChildSizingFunction(RenderBox child, BoxConstraints constraints)
; |
| 39 | 40 |
| 40 class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
exBoxParentData>, | 41 class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
exBoxParentData>, |
| 41 RenderBoxContainerDefaultsMixin<RenderBo
x, FlexBoxParentData> { | 42 RenderBoxContainerDefaultsMixin<RenderBo
x, FlexBoxParentData> { |
| 42 // lays out RenderBox children using flexible layout | 43 // lays out RenderBox children using flexible layout |
| 43 | 44 |
| 44 RenderFlex({ | 45 RenderFlex({ |
| 45 FlexDirection direction: FlexDirection.horizontal, | 46 FlexDirection direction: FlexDirection.horizontal, |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 double crossSize = 0.0; // This will be determined after laying out the chi
ldren | 276 double crossSize = 0.0; // This will be determined after laying out the chi
ldren |
| 276 double freeSpace = mainSize; | 277 double freeSpace = mainSize; |
| 277 RenderBox child = firstChild; | 278 RenderBox child = firstChild; |
| 278 while (child != null) { | 279 while (child != null) { |
| 279 assert(child.parentData is FlexBoxParentData); | 280 assert(child.parentData is FlexBoxParentData); |
| 280 totalChildren++; | 281 totalChildren++; |
| 281 int flex = _getFlex(child); | 282 int flex = _getFlex(child); |
| 282 if (flex > 0) { | 283 if (flex > 0) { |
| 283 totalFlex += child.parentData.flex; | 284 totalFlex += child.parentData.flex; |
| 284 } else { | 285 } else { |
| 285 BoxConstraints innerConstraints = new BoxConstraints(maxHeight: constrai
nts.maxHeight, | 286 BoxConstraints innerConstraints; |
| 286 maxWidth: constrain
ts.maxWidth); | 287 if (alignItems == FlexAlignItems.stretch) { |
| 288 switch (_direction) { |
| 289 case FlexDirection.horizontal: |
| 290 innerConstraints = new BoxConstraints(maxWidth: constraints.maxWid
th, |
| 291 minHeight: constraints.minHe
ight, |
| 292 maxHeight: constraints.maxHe
ight); |
| 293 break; |
| 294 case FlexDirection.vertical: |
| 295 innerConstraints = new BoxConstraints(minWidth: constraints.minWid
th, |
| 296 maxWidth: constraints.maxWid
th, |
| 297 maxHeight: constraints.maxHe
ight); |
| 298 break; |
| 299 } |
| 300 } else { |
| 301 innerConstraints = constraints.loosen(); |
| 302 } |
| 287 child.layout(innerConstraints, parentUsesSize: true); | 303 child.layout(innerConstraints, parentUsesSize: true); |
| 288 freeSpace -= _getMainSize(child); | 304 freeSpace -= _getMainSize(child); |
| 289 crossSize = math.max(crossSize, _getCrossSize(child)); | 305 crossSize = math.max(crossSize, _getCrossSize(child)); |
| 290 } | 306 } |
| 291 child = child.parentData.nextSibling; | 307 child = child.parentData.nextSibling; |
| 292 } | 308 } |
| 293 | 309 |
| 294 // Steps 4-5. Distribute remaining space to flexible children. | 310 // Steps 4-5. Distribute remaining space to flexible children. |
| 295 double spacePerFlex = totalFlex > 0 ? (freeSpace / totalFlex) : 0.0; | 311 double spacePerFlex = totalFlex > 0 ? (freeSpace / totalFlex) : 0.0; |
| 296 double usedSpace = 0.0; | 312 double usedSpace = 0.0; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 break; | 375 break; |
| 360 } | 376 } |
| 361 | 377 |
| 362 // Position elements. For now, center the flex items in the cross direction | 378 // Position elements. For now, center the flex items in the cross direction |
| 363 double childMainPosition = leadingSpace; | 379 double childMainPosition = leadingSpace; |
| 364 child = firstChild; | 380 child = firstChild; |
| 365 while (child != null) { | 381 while (child != null) { |
| 366 assert(child.parentData is FlexBoxParentData); | 382 assert(child.parentData is FlexBoxParentData); |
| 367 double childCrossPosition; | 383 double childCrossPosition; |
| 368 switch (_alignItems) { | 384 switch (_alignItems) { |
| 385 case FlexAlignItems.stretch: |
| 369 case FlexAlignItems.start: | 386 case FlexAlignItems.start: |
| 370 childCrossPosition = 0.0; | 387 childCrossPosition = 0.0; |
| 371 break; | 388 break; |
| 372 case FlexAlignItems.end: | 389 case FlexAlignItems.end: |
| 373 childCrossPosition = crossSize - _getCrossSize(child); | 390 childCrossPosition = crossSize - _getCrossSize(child); |
| 374 break; | 391 break; |
| 375 case FlexAlignItems.center: | 392 case FlexAlignItems.center: |
| 376 childCrossPosition = crossSize / 2.0 - _getCrossSize(child) / 2.0; | 393 childCrossPosition = crossSize / 2.0 - _getCrossSize(child) / 2.0; |
| 377 break; | 394 break; |
| 378 } | 395 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 390 } | 407 } |
| 391 | 408 |
| 392 void hitTestChildren(HitTestResult result, { Point position }) { | 409 void hitTestChildren(HitTestResult result, { Point position }) { |
| 393 defaultHitTestChildren(result, position: position); | 410 defaultHitTestChildren(result, position: position); |
| 394 } | 411 } |
| 395 | 412 |
| 396 void paint(PaintingCanvas canvas, Offset offset) { | 413 void paint(PaintingCanvas canvas, Offset offset) { |
| 397 defaultPaint(canvas, offset); | 414 defaultPaint(canvas, offset); |
| 398 } | 415 } |
| 399 } | 416 } |
| OLD | NEW |