mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
Handle changes to scrollDirection in ScrollableList et al
Changed the pageable_list.dart example: tapping on the toolbar changes the scroll direction. This exposed some problems: - Scrollable.syncFields() didn't update scrollDirection - Viewport updated its RenderObject fields in the wrong order - FixedHeightScrollable scrollDirection changes didn't update the scrollBehavior There may be similar problems with VariableHeightList and ScrollableViewport. I will fix those in a separate CL.
This commit is contained in:
parent
72807ef8ec
commit
d662f7e6d2
@ -24,13 +24,14 @@ class CardModel {
|
|||||||
Key get key => new Key.fromObjectIdentity(this);
|
Key get key => new Key.fromObjectIdentity(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
class TestApp extends App {
|
class PageableListApp extends App {
|
||||||
|
|
||||||
static const TextStyle cardLabelStyle =
|
static const TextStyle cardLabelStyle =
|
||||||
const TextStyle(color: white, fontSize: 18.0, fontWeight: bold);
|
const TextStyle(color: white, fontSize: 18.0, fontWeight: bold);
|
||||||
|
|
||||||
List<CardModel> cardModels;
|
List<CardModel> cardModels;
|
||||||
Size pageSize = new Size(200.0, 200.0);
|
Size pageSize = new Size(200.0, 200.0);
|
||||||
|
ScrollDirection scrollDirection = ScrollDirection.horizontal;
|
||||||
|
|
||||||
void initState() {
|
void initState() {
|
||||||
List<Size> cardSizes = [
|
List<Size> cardSizes = [
|
||||||
@ -55,6 +56,15 @@ class TestApp extends App {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EventDisposition handleToolbarTap(_) {
|
||||||
|
setState(() {
|
||||||
|
scrollDirection = (scrollDirection == ScrollDirection.vertical)
|
||||||
|
? ScrollDirection.horizontal
|
||||||
|
: ScrollDirection.vertical;
|
||||||
|
});
|
||||||
|
return EventDisposition.processed;
|
||||||
|
}
|
||||||
|
|
||||||
Widget buildCard(CardModel cardModel) {
|
Widget buildCard(CardModel cardModel) {
|
||||||
Widget card = new Card(
|
Widget card = new Card(
|
||||||
color: cardModel.color,
|
color: cardModel.color,
|
||||||
@ -65,9 +75,14 @@ class TestApp extends App {
|
|||||||
child: new Center(child: new Text(cardModel.label, style: cardLabelStyle))
|
child: new Center(child: new Text(cardModel.label, style: cardLabelStyle))
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
BoxConstraints constraints = (scrollDirection == ScrollDirection.vertical)
|
||||||
|
? new BoxConstraints.tightFor(height: pageSize.height)
|
||||||
|
: new BoxConstraints.tightFor(width: pageSize.width);
|
||||||
|
|
||||||
return new Container(
|
return new Container(
|
||||||
key: cardModel.key,
|
key: cardModel.key,
|
||||||
width: pageSize.width,
|
constraints: constraints,
|
||||||
child: new Center(child: card)
|
child: new Center(child: card)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -76,8 +91,10 @@ class TestApp extends App {
|
|||||||
Widget list = new PageableList<CardModel>(
|
Widget list = new PageableList<CardModel>(
|
||||||
items: cardModels,
|
items: cardModels,
|
||||||
itemBuilder: buildCard,
|
itemBuilder: buildCard,
|
||||||
scrollDirection: ScrollDirection.horizontal,
|
scrollDirection: scrollDirection,
|
||||||
itemExtent: pageSize.width
|
itemExtent: (scrollDirection == ScrollDirection.vertical)
|
||||||
|
? pageSize.height
|
||||||
|
: pageSize.width
|
||||||
);
|
);
|
||||||
|
|
||||||
return new IconTheme(
|
return new IconTheme(
|
||||||
@ -91,7 +108,10 @@ class TestApp extends App {
|
|||||||
child: new TaskDescription(
|
child: new TaskDescription(
|
||||||
label: 'PageableList',
|
label: 'PageableList',
|
||||||
child: new Scaffold(
|
child: new Scaffold(
|
||||||
toolbar: new ToolBar(center: new Text('PageableList Demo')),
|
toolbar: new Listener(
|
||||||
|
onGestureTap: handleToolbarTap,
|
||||||
|
child: new ToolBar(center: new Text('PageableList: ${scrollDirection}'))
|
||||||
|
),
|
||||||
body: new SizeObserver(
|
body: new SizeObserver(
|
||||||
callback: updatePageSize,
|
callback: updatePageSize,
|
||||||
child: new Container(
|
child: new Container(
|
||||||
@ -107,5 +127,5 @@ class TestApp extends App {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(new TestApp());
|
runApp(new PageableListApp());
|
||||||
}
|
}
|
@ -285,21 +285,22 @@ class Baseline extends OneChildRenderObjectWrapper {
|
|||||||
class Viewport extends OneChildRenderObjectWrapper {
|
class Viewport extends OneChildRenderObjectWrapper {
|
||||||
Viewport({
|
Viewport({
|
||||||
Key key,
|
Key key,
|
||||||
this.scrollOffset: Offset.zero,
|
|
||||||
this.scrollDirection: ScrollDirection.vertical,
|
this.scrollDirection: ScrollDirection.vertical,
|
||||||
|
this.scrollOffset: Offset.zero,
|
||||||
Widget child
|
Widget child
|
||||||
}) : super(key: key, child: child);
|
}) : super(key: key, child: child);
|
||||||
|
|
||||||
final Offset scrollOffset;
|
|
||||||
final ScrollDirection scrollDirection;
|
final ScrollDirection scrollDirection;
|
||||||
|
final Offset scrollOffset;
|
||||||
|
|
||||||
RenderViewport createNode() => new RenderViewport(scrollOffset: scrollOffset, scrollDirection: scrollDirection);
|
RenderViewport createNode() => new RenderViewport(scrollDirection: scrollDirection, scrollOffset: scrollOffset);
|
||||||
RenderViewport get renderObject => super.renderObject;
|
RenderViewport get renderObject => super.renderObject;
|
||||||
|
|
||||||
void syncRenderObject(Viewport old) {
|
void syncRenderObject(Viewport old) {
|
||||||
super.syncRenderObject(old);
|
super.syncRenderObject(old);
|
||||||
renderObject.scrollOffset = scrollOffset;
|
// Order dependency: RenderViewport validates scrollOffset based on scrollDirection.
|
||||||
renderObject.scrollDirection = scrollDirection;
|
renderObject.scrollDirection = scrollDirection;
|
||||||
|
renderObject.scrollOffset = scrollOffset;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,7 +57,7 @@ abstract class Scrollable extends StatefulComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void syncFields(Scrollable source) {
|
void syncFields(Scrollable source) {
|
||||||
scrollDirection == source.scrollDirection;
|
scrollDirection = source.scrollDirection;
|
||||||
}
|
}
|
||||||
|
|
||||||
double _scrollOffset = 0.0;
|
double _scrollOffset = 0.0;
|
||||||
@ -358,6 +358,7 @@ abstract class FixedHeightScrollable extends Scrollable {
|
|||||||
|
|
||||||
EdgeDims padding;
|
EdgeDims padding;
|
||||||
double itemExtent;
|
double itemExtent;
|
||||||
|
Size containerSize = Size.zero;
|
||||||
|
|
||||||
/// Subclasses must implement `get itemCount` to tell FixedHeightScrollable
|
/// Subclasses must implement `get itemCount` to tell FixedHeightScrollable
|
||||||
/// how many items there are in the list.
|
/// how many items there are in the list.
|
||||||
@ -365,22 +366,32 @@ abstract class FixedHeightScrollable extends Scrollable {
|
|||||||
int _previousItemCount;
|
int _previousItemCount;
|
||||||
|
|
||||||
void syncFields(FixedHeightScrollable source) {
|
void syncFields(FixedHeightScrollable source) {
|
||||||
if (padding != source.padding || itemExtent != source.itemExtent) {
|
bool scrollBehaviorUpdateNeeded =
|
||||||
padding = source.padding;
|
padding != source.padding ||
|
||||||
itemExtent = source.itemExtent;
|
itemExtent != source.itemExtent ||
|
||||||
_updateContentsExtent();
|
scrollDirection != source.scrollDirection;
|
||||||
}
|
|
||||||
super.syncFields(source);
|
padding = source.padding;
|
||||||
|
itemExtent = source.itemExtent;
|
||||||
|
super.syncFields(source); // update scrollDirection
|
||||||
|
|
||||||
|
if (scrollBehaviorUpdateNeeded)
|
||||||
|
_updateScrollBehavior();
|
||||||
}
|
}
|
||||||
|
|
||||||
ScrollBehavior createScrollBehavior() => new OverscrollBehavior();
|
ScrollBehavior createScrollBehavior() => new OverscrollBehavior();
|
||||||
OverscrollBehavior get scrollBehavior => super.scrollBehavior;
|
OverscrollBehavior get scrollBehavior => super.scrollBehavior;
|
||||||
|
|
||||||
double _containerExtent;
|
double get _containerExtent {
|
||||||
|
return scrollDirection == ScrollDirection.vertical
|
||||||
|
? containerSize.height
|
||||||
|
: containerSize.width;
|
||||||
|
}
|
||||||
|
|
||||||
void _handleSizeChanged(Size newSize) {
|
void _handleSizeChanged(Size newSize) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_containerExtent = scrollDirection == ScrollDirection.vertical ? newSize.height : newSize.width;
|
containerSize = newSize;
|
||||||
scrollBehavior.containerSize = _containerExtent;
|
_updateScrollBehavior();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -404,7 +415,9 @@ abstract class FixedHeightScrollable extends Scrollable {
|
|||||||
return new EdgeDims.only(top: padding.top, bottom: padding.bottom);
|
return new EdgeDims.only(top: padding.top, bottom: padding.bottom);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _updateContentsExtent() {
|
void _updateScrollBehavior() {
|
||||||
|
scrollBehavior.containerSize = _containerExtent;
|
||||||
|
|
||||||
double contentsExtent = itemExtent * itemCount;
|
double contentsExtent = itemExtent * itemCount;
|
||||||
if (padding != null)
|
if (padding != null)
|
||||||
contentsExtent += _leadingPadding + _trailingPadding;
|
contentsExtent += _leadingPadding + _trailingPadding;
|
||||||
@ -425,7 +438,7 @@ abstract class FixedHeightScrollable extends Scrollable {
|
|||||||
Widget buildContent() {
|
Widget buildContent() {
|
||||||
if (itemCount != _previousItemCount) {
|
if (itemCount != _previousItemCount) {
|
||||||
_previousItemCount = itemCount;
|
_previousItemCount = itemCount;
|
||||||
_updateContentsExtent();
|
_updateScrollBehavior();
|
||||||
_updateScrollOffset();
|
_updateScrollOffset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user