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 'package:vector_math/vector_math.dart'; | 9 import 'package:vector_math/vector_math.dart'; |
10 import 'scrollable.dart'; | 10 import 'scrollable.dart'; |
11 | 11 |
12 abstract class FixedHeightScrollable extends Scrollable { | 12 abstract class FixedHeightScrollable extends Scrollable { |
13 | |
13 FixedHeightScrollable({ this.itemHeight, Object key }) : super(key: key) { | 14 FixedHeightScrollable({ this.itemHeight, Object key }) : super(key: key) { |
14 assert(itemHeight != null); | 15 assert(itemHeight != null); |
15 } | 16 } |
16 | 17 |
18 double itemHeight; | |
19 | |
20 void syncFields(FixedHeightScrollable source) { | |
21 itemHeight = source.itemHeight; | |
22 super.syncFields(source); | |
23 } | |
24 | |
17 ScrollBehavior createScrollBehavior() => new OverscrollBehavior(); | 25 ScrollBehavior createScrollBehavior() => new OverscrollBehavior(); |
18 OverscrollBehavior get scrollBehavior => super.scrollBehavior as OverscrollBeh avior; | 26 OverscrollBehavior get scrollBehavior => super.scrollBehavior as OverscrollBeh avior; |
19 | 27 |
20 double _height; | |
21 final double itemHeight; | |
22 | |
23 int _itemCount = 0; | 28 int _itemCount = 0; |
24 int get itemCount => _itemCount; | 29 int get itemCount => _itemCount; |
25 void set itemCount (int value) { | 30 void set itemCount (int value) { |
26 if (_itemCount != value) { | 31 if (_itemCount != value) { |
27 _itemCount = value; | 32 _itemCount = value; |
28 scrollBehavior.contentsHeight = itemHeight * _itemCount; | 33 scrollBehavior.contentsHeight = itemHeight * _itemCount; |
29 } | 34 } |
30 } | 35 } |
31 | 36 |
37 double _height; | |
32 void _handleSizeChanged(Size newSize) { | 38 void _handleSizeChanged(Size newSize) { |
33 setState(() { | 39 setState(() { |
34 _height = newSize.height; | 40 _height = newSize.height; |
35 scrollBehavior.containerHeight = _height; | 41 scrollBehavior.containerHeight = _height; |
36 }); | 42 }); |
37 } | 43 } |
38 | 44 |
39 UINode buildContent() { | 45 UINode buildContent() { |
40 var itemNumber = 0; | 46 var itemNumber = 0; |
abarth-chromium
2015/06/10 17:07:58
s/itemNumber/itemShowNumber/
| |
41 var itemCount = 0; | 47 var itemShowCount = 0; |
42 | 48 |
43 Matrix4 transform = new Matrix4.identity(); | 49 Matrix4 transform = new Matrix4.identity(); |
44 | 50 |
45 if (_height != null && _height > 0.0) { | 51 if (_height != null && _height > 0.0) { |
46 if (scrollOffset < 0.0) { | 52 if (scrollOffset < 0.0) { |
47 double visibleHeight = _height + scrollOffset; | 53 double visibleHeight = _height + scrollOffset; |
48 itemCount = (visibleHeight / itemHeight).round() + 1; | 54 itemShowCount = (visibleHeight / itemHeight).round() + 1; |
49 transform.translate(0.0, -scrollOffset); | 55 transform.translate(0.0, -scrollOffset); |
50 } else { | 56 } else { |
51 itemCount = (_height / itemHeight).ceil() + 1; | 57 itemShowCount = (_height / itemHeight).ceil() + 1; |
52 double alignmentDelta = -scrollOffset % itemHeight; | 58 double alignmentDelta = -scrollOffset % itemHeight; |
53 if (alignmentDelta != 0.0) | 59 if (alignmentDelta != 0.0) |
54 alignmentDelta -= itemHeight; | 60 alignmentDelta -= itemHeight; |
55 | 61 |
56 double drawStart = scrollOffset + alignmentDelta; | 62 double drawStart = scrollOffset + alignmentDelta; |
57 itemNumber = math.max(0, (drawStart / itemHeight).floor()); | 63 itemNumber = math.max(0, (drawStart / itemHeight).floor()); |
58 | 64 |
59 transform.translate(0.0, alignmentDelta); | 65 transform.translate(0.0, alignmentDelta); |
60 } | 66 } |
61 } | 67 } |
62 | 68 |
63 return new SizeObserver( | 69 return new SizeObserver( |
64 callback: _handleSizeChanged, | 70 callback: _handleSizeChanged, |
65 child: new Clip( | 71 child: new Clip( |
66 child: new DecoratedBox( | 72 child: new DecoratedBox( |
67 decoration: const BoxDecoration( | 73 decoration: const BoxDecoration( |
68 backgroundColor: const Color(0xFFFFFFFF) | 74 backgroundColor: const Color(0xFFFFFFFF) |
69 ), | 75 ), |
70 child: new Transform( | 76 child: new Transform( |
71 transform: transform, | 77 transform: transform, |
72 child: new BlockContainer( | 78 child: new BlockContainer( |
73 children: buildItems(itemNumber, itemCount)) | 79 children: buildItems(itemNumber, itemShowCount)) |
74 ) | 80 ) |
75 ) | 81 ) |
76 ) | 82 ) |
77 ); | 83 ); |
78 } | 84 } |
79 | 85 |
80 List<UINode> buildItems(int start, int count); | 86 List<UINode> buildItems(int start, int count); |
87 | |
81 } | 88 } |
OLD | NEW |