Chromium Code Reviews| 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 'package:vector_math/vector_math.dart'; | 7 import 'package:vector_math/vector_math.dart'; |
| 8 | 8 |
| 9 import '../animation/scroll_behavior.dart'; | 9 import '../animation/scroll_behavior.dart'; |
| 10 import 'basic.dart'; | 10 import 'basic.dart'; |
| 11 import 'scrollable.dart'; | 11 import 'scrollable.dart'; |
| 12 | 12 |
| 13 abstract class FixedHeightScrollable extends Scrollable { | 13 abstract class FixedHeightScrollable extends Scrollable { |
| 14 | 14 |
| 15 FixedHeightScrollable({ String key, this.itemHeight, Color backgroundColor, th is.padding }) | 15 FixedHeightScrollable({ String key, this.itemHeight, Color backgroundColor, th is.padding }) |
| 16 : super(key: key, backgroundColor: backgroundColor) { | 16 : super(key: key, backgroundColor: backgroundColor) { |
| 17 assert(itemHeight != null); | 17 assert(itemHeight != null); |
| 18 } | 18 } |
| 19 | 19 |
| 20 EdgeDims padding; | 20 EdgeDims padding; |
| 21 double itemHeight; | 21 double itemHeight; |
| 22 | 22 |
| 23 /// Subclasses must implement `get itemCount` to tell FixedHeightScrollable | 23 /// Subclasses must implement `get itemCount` to tell FixedHeightScrollable |
| 24 /// how many items there are in the list. | 24 /// how many items there are in the list. |
| 25 int get itemCount; | 25 int get itemCount; |
| 26 int _previousItemCount; | |
| 26 | 27 |
| 27 void syncFields(FixedHeightScrollable source) { | 28 void syncFields(FixedHeightScrollable source) { |
| 28 padding = source.padding; | 29 padding = source.padding; |
| 29 itemHeight = source.itemHeight; | 30 itemHeight = source.itemHeight; |
| 30 super.syncFields(source); | 31 super.syncFields(source); |
| 31 } | 32 } |
| 32 | 33 |
| 33 ScrollBehavior createScrollBehavior() => new OverscrollBehavior(); | 34 ScrollBehavior createScrollBehavior() => new OverscrollBehavior(); |
| 34 OverscrollBehavior get scrollBehavior => super.scrollBehavior; | 35 OverscrollBehavior get scrollBehavior => super.scrollBehavior; |
| 35 | 36 |
| 36 double _height; | 37 double _height; |
| 37 void _handleSizeChanged(Size newSize) { | 38 void _handleSizeChanged(Size newSize) { |
| 38 setState(() { | 39 setState(() { |
| 39 _height = newSize.height; | 40 _height = newSize.height; |
| 40 scrollBehavior.containerHeight = _height; | 41 scrollBehavior.containerHeight = _height; |
| 41 }); | 42 }); |
| 42 } | 43 _updateScrollOffset(); |
|
eseidel
2015/07/09 20:40:07
Unneeded.
| |
| 43 | |
| 44 bool scrollTo(double newScrollOffset) { | |
| 45 if (_height != null && _height > 0.0) { | |
| 46 double maxScrollOffset = math.max(0.0, itemCount * itemHeight - _height); | |
| 47 newScrollOffset = math.min(newScrollOffset, maxScrollOffset); | |
| 48 } | |
| 49 return super.scrollTo(newScrollOffset); | |
| 50 } | 44 } |
| 51 | 45 |
| 52 void _updateContentsHeight() { | 46 void _updateContentsHeight() { |
| 53 double contentsHeight = itemHeight * itemCount; | 47 double contentsHeight = itemHeight * itemCount; |
| 54 if (padding != null) | 48 if (padding != null) |
| 55 contentsHeight += padding.top + padding.bottom; | 49 contentsHeight += padding.top + padding.bottom; |
| 56 scrollBehavior.contentsHeight = contentsHeight; | 50 scrollBehavior.contentsHeight = contentsHeight; |
| 57 } | 51 } |
| 58 | 52 |
| 53 void _updateScrollOffset() { | |
| 54 if (scrollOffset > scrollBehavior.maxScrollOffset) | |
| 55 settleScrollOffset(); | |
| 56 } | |
| 57 | |
| 59 Widget buildContent() { | 58 Widget buildContent() { |
| 60 _updateContentsHeight(); | 59 if (itemCount != _previousItemCount) { |
| 60 _previousItemCount = itemCount; | |
| 61 _updateContentsHeight(); | |
| 62 _updateScrollOffset(); | |
| 63 } | |
| 61 | 64 |
| 62 var itemShowIndex = 0; | 65 var itemShowIndex = 0; |
| 63 var itemShowCount = 0; | 66 var itemShowCount = 0; |
| 64 Matrix4 transform = new Matrix4.identity(); | 67 Matrix4 transform = new Matrix4.identity(); |
| 65 | 68 |
| 66 if (_height != null && _height > 0.0) { | 69 if (_height != null && _height > 0.0) { |
| 67 if (scrollOffset < 0.0) { | 70 if (scrollOffset < 0.0) { |
| 68 double visibleHeight = _height + scrollOffset; | 71 double visibleHeight = _height + scrollOffset; |
| 69 itemShowCount = (visibleHeight / itemHeight).round() + 1; | 72 itemShowCount = (visibleHeight / itemHeight).round() + 1; |
| 70 transform.translate(0.0, -scrollOffset); | 73 transform.translate(0.0, -scrollOffset); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 94 child: new Block(items) | 97 child: new Block(items) |
| 95 ) | 98 ) |
| 96 ) | 99 ) |
| 97 ) | 100 ) |
| 98 ); | 101 ); |
| 99 } | 102 } |
| 100 | 103 |
| 101 List<Widget> buildItems(int start, int count); | 104 List<Widget> buildItems(int start, int count); |
| 102 | 105 |
| 103 } | 106 } |
| OLD | NEW |