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