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 '../fn.dart'; | 6 import '../fn.dart'; |
7 import 'dart:math' as math; | 7 import 'dart:math' as math; |
8 import 'dart:sky' as sky; | 8 import 'dart:sky' as sky; |
| 9 import 'dart:async'; |
9 import 'scrollable.dart'; | 10 import 'scrollable.dart'; |
10 | 11 |
11 abstract class FixedHeightScrollable extends Scrollable { | 12 abstract class FixedHeightScrollable extends Scrollable { |
12 static final Style _style = new Style(''' | 13 static final Style _style = new Style(''' |
13 overflow: hidden; | 14 overflow: hidden; |
14 position: relative; | 15 position: relative; |
15 will-change: transform;''' | 16 will-change: transform;''' |
16 ); | 17 ); |
17 | 18 |
18 static final Style _scrollAreaStyle = new Style(''' | 19 static final Style _scrollAreaStyle = new Style(''' |
19 position:relative; | 20 position:relative; |
20 will-change: transform;''' | 21 will-change: transform;''' |
21 ); | 22 ); |
22 | 23 |
23 double _height = 0.0; | 24 double _height = 0.0; |
24 double _itemHeight; | 25 double _itemHeight; |
25 | 26 |
26 FixedHeightScrollable({ | 27 FixedHeightScrollable({ |
27 Object key, | 28 Object key, |
28 ScrollBehavior scrollBehavior | 29 ScrollBehavior scrollBehavior |
29 }) : super(key: key, scrollBehavior: scrollBehavior) { | 30 }) : super(key: key, scrollBehavior: scrollBehavior); |
30 onDidMount(_measureHeights); | |
31 } | |
32 | 31 |
33 void _measureHeights() { | 32 void _measureHeights() { |
| 33 if (_itemHeight != null) |
| 34 return; |
34 var root = getRoot(); | 35 var root = getRoot(); |
| 36 if (root == null) |
| 37 return; |
35 var item = root.firstChild.firstChild; | 38 var item = root.firstChild.firstChild; |
| 39 if (item == null) |
| 40 return; |
36 sky.ClientRect scrollRect = root.getBoundingClientRect(); | 41 sky.ClientRect scrollRect = root.getBoundingClientRect(); |
37 sky.ClientRect itemRect = item.getBoundingClientRect(); | 42 sky.ClientRect itemRect = item.getBoundingClientRect(); |
38 assert(scrollRect.height > 0); | 43 assert(scrollRect.height > 0); |
39 assert(itemRect.height > 0); | 44 assert(itemRect.height > 0); |
40 | 45 |
41 setState(() { | 46 setState(() { |
42 _height = scrollRect.height; | 47 _height = scrollRect.height; |
43 _itemHeight = itemRect.height; | 48 _itemHeight = itemRect.height; |
44 }); | 49 }); |
45 } | 50 } |
46 | 51 |
47 Node buildContent() { | 52 Node buildContent() { |
48 var itemNumber = 0; | 53 var itemNumber = 0; |
49 var drawCount = 1; | 54 var drawCount = 1; |
50 var transformStyle = ''; | 55 var transformStyle = ''; |
51 | 56 |
52 if (_height > 0.0) { | 57 if (_itemHeight == null) |
| 58 new Future.microtask(_measureHeights); |
| 59 |
| 60 if (_height > 0.0 && _itemHeight != null) { |
53 if (scrollOffset < 0.0) { | 61 if (scrollOffset < 0.0) { |
54 double visibleHeight = _height + scrollOffset; | 62 double visibleHeight = _height + scrollOffset; |
55 drawCount = (visibleHeight / _itemHeight).round() + 1; | 63 drawCount = (visibleHeight / _itemHeight).round() + 1; |
56 transformStyle = | 64 transformStyle = |
57 'transform: translateY(${(-scrollOffset).toStringAsFixed(2)}px)'; | 65 'transform: translateY(${(-scrollOffset).toStringAsFixed(2)}px)'; |
58 } else { | 66 } else { |
59 drawCount = (_height / _itemHeight).round() + 1; | 67 drawCount = (_height / _itemHeight).round() + 1; |
60 double alignmentOffset = math.max(0.0, scrollOffset); | 68 double alignmentOffset = math.max(0.0, scrollOffset); |
61 double alignmentDelta = -scrollOffset % _itemHeight; | 69 double alignmentDelta = -scrollOffset % _itemHeight; |
62 if (alignmentDelta != 0.0) | 70 if (alignmentDelta != 0.0) |
(...skipping 12 matching lines...) Expand all Loading... |
75 children: [ | 83 children: [ |
76 new Container( | 84 new Container( |
77 style: _scrollAreaStyle, | 85 style: _scrollAreaStyle, |
78 inlineStyle: transformStyle, | 86 inlineStyle: transformStyle, |
79 children: buildItems(itemNumber, drawCount) | 87 children: buildItems(itemNumber, drawCount) |
80 ) | 88 ) |
81 ] | 89 ] |
82 ); | 90 ); |
83 } | 91 } |
84 } | 92 } |
OLD | NEW |