| 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 '../animation/scroll_behavior.dart'; | 5 import '../animation/scroll_behavior.dart'; |
| 6 import '../fn2.dart'; | 6 import '../fn2.dart'; |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:math' as math; | 8 import 'dart:math' as math; |
| 9 import 'dart:sky' as sky; |
| 9 import 'package:vector_math/vector_math.dart'; | 10 import 'package:vector_math/vector_math.dart'; |
| 10 import 'scrollable.dart'; | 11 import 'scrollable.dart'; |
| 11 | 12 |
| 12 abstract class FixedHeightScrollable extends Scrollable { | 13 abstract class FixedHeightScrollable extends Scrollable { |
| 13 FixedHeightScrollable({ Object key }) : super(key: key); | 14 FixedHeightScrollable({ this.itemHeight, Object key }) : super(key: key) { |
| 15 assert(itemHeight != null); |
| 16 } |
| 14 | 17 |
| 15 ScrollBehavior createScrollBehavior() => new OverscrollBehavior(); | 18 ScrollBehavior createScrollBehavior() => new OverscrollBehavior(); |
| 16 OverscrollBehavior get scrollBehavior => super.scrollBehavior as OverscrollBeh
avior; | 19 OverscrollBehavior get scrollBehavior => super.scrollBehavior as OverscrollBeh
avior; |
| 17 | 20 |
| 18 double _height = 0.0; | 21 double _height; |
| 19 double _itemHeight; | 22 final double itemHeight; |
| 20 | 23 |
| 21 int _itemCount = 0; | 24 int _itemCount = 0; |
| 22 int get itemCount => _itemCount; | 25 int get itemCount => _itemCount; |
| 23 void set itemCount (int value) { | 26 void set itemCount (int value) { |
| 24 if (_itemCount != value) { | 27 if (_itemCount != value) { |
| 25 _itemCount = value; | 28 _itemCount = value; |
| 26 if (_itemHeight != null) | 29 scrollBehavior.contentsHeight = itemHeight * _itemCount; |
| 27 scrollBehavior.contentsHeight = _itemHeight * _itemCount; | |
| 28 } | 30 } |
| 29 } | 31 } |
| 30 | 32 |
| 31 void _measureHeights() { | 33 void _handleSizeChanged(sky.Size newSize) { |
| 32 if (_itemHeight != null) | |
| 33 return; | |
| 34 setState(() { | 34 setState(() { |
| 35 // TODO(abarth): Actually measure these heights. | 35 _height = newSize.height; |
| 36 _height = 500.0; // root.height; | |
| 37 assert(_height > 0); | |
| 38 _itemHeight = 100.0; // item.height; | |
| 39 assert(_itemHeight > 0); | |
| 40 scrollBehavior.containerHeight = _height; | 36 scrollBehavior.containerHeight = _height; |
| 41 scrollBehavior.contentsHeight = _itemHeight * _itemCount; | |
| 42 }); | 37 }); |
| 43 } | 38 } |
| 44 | 39 |
| 45 UINode buildContent() { | 40 UINode buildContent() { |
| 46 var itemNumber = 0; | 41 var itemNumber = 0; |
| 47 var drawCount = 1; | 42 var itemCount = 0; |
| 48 var transformStyle = ''; | |
| 49 | |
| 50 if (_itemHeight == null) | |
| 51 new Future.microtask(_measureHeights); | |
| 52 | 43 |
| 53 Matrix4 transform = new Matrix4.identity(); | 44 Matrix4 transform = new Matrix4.identity(); |
| 54 | 45 |
| 55 if (_height > 0.0 && _itemHeight != null) { | 46 if (_height != null && _height > 0.0) { |
| 56 if (scrollOffset < 0.0) { | 47 if (scrollOffset < 0.0) { |
| 57 double visibleHeight = _height + scrollOffset; | 48 double visibleHeight = _height + scrollOffset; |
| 58 drawCount = (visibleHeight / _itemHeight).round() + 1; | 49 itemCount = (visibleHeight / itemHeight).round() + 1; |
| 59 transform.translate(0.0, -scrollOffset); | 50 transform.translate(0.0, -scrollOffset); |
| 60 } else { | 51 } else { |
| 61 drawCount = (_height / _itemHeight).ceil() + 1; | 52 itemCount = (_height / itemHeight).ceil() + 1; |
| 62 double alignmentDelta = -scrollOffset % _itemHeight; | 53 double alignmentDelta = -scrollOffset % itemHeight; |
| 63 if (alignmentDelta != 0.0) | 54 if (alignmentDelta != 0.0) |
| 64 alignmentDelta -= _itemHeight; | 55 alignmentDelta -= itemHeight; |
| 65 | 56 |
| 66 double drawStart = scrollOffset + alignmentDelta; | 57 double drawStart = scrollOffset + alignmentDelta; |
| 67 itemNumber = math.max(0, (drawStart / _itemHeight).floor()); | 58 itemNumber = math.max(0, (drawStart / itemHeight).floor()); |
| 68 | 59 |
| 69 transform.translate(0.0, alignmentDelta); | 60 transform.translate(0.0, alignmentDelta); |
| 70 } | 61 } |
| 71 } | 62 } |
| 72 | 63 |
| 73 return new Clip( | 64 return new SizeObserver( |
| 74 child: new Transform( | 65 callback: _handleSizeChanged, |
| 75 transform: transform, | 66 child: new Clip( |
| 76 child: new BlockContainer(children: buildItems(itemNumber, drawCount)) | 67 child: new Transform( |
| 68 transform: transform, |
| 69 child: new BlockContainer( |
| 70 children: buildItems(itemNumber, itemCount)) |
| 71 ) |
| 77 ) | 72 ) |
| 78 ); | 73 ); |
| 79 } | 74 } |
| 80 | 75 |
| 81 List<UINode> buildItems(int start, int count); | 76 List<UINode> buildItems(int start, int count); |
| 82 } | 77 } |
| OLD | NEW |