Skip to content

Commit

Permalink
Swap scope with gesture (flutter#21157)
Browse files Browse the repository at this point in the history
  • Loading branch information
rrousselGit authored and goderbauer committed Jan 17, 2019
1 parent 56039b4 commit 649f49d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
38 changes: 23 additions & 15 deletions packages/flutter/lib/src/widgets/scrollable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -524,21 +524,29 @@ class ScrollableState extends State<Scrollable> with TickerProviderStateMixin
@override
Widget build(BuildContext context) {
assert(position != null);
// TODO(ianh): Having all these global keys is sad.
Widget result = RawGestureDetector(
key: _gestureDetectorKey,
gestures: _gestureRecognizers,
behavior: HitTestBehavior.opaque,
excludeFromSemantics: widget.excludeFromSemantics,
child: Semantics(
explicitChildNodes: !widget.excludeFromSemantics,
child: IgnorePointer(
key: _ignorePointerKey,
ignoring: _shouldIgnorePointer,
ignoringSemantics: false,
child: _ScrollableScope(
scrollable: this,
position: position,
// _ScrollableScope must be placed above the BuildContext returned by notificationContext
// so that we can get this ScrollableState by doing the following:
//
// ScrollNotification notification;
// Scrollable.of(notification.context)
//
// Since notificationContext is pointing to _gestureDetectorKey.context, _ScrollableScope
// must be placed above the widget using it: RawGestureDetector
Widget result = _ScrollableScope(
scrollable: this,
position: position,
// TODO(ianh): Having all these global keys is sad.
child: RawGestureDetector(
key: _gestureDetectorKey,
gestures: _gestureRecognizers,
behavior: HitTestBehavior.opaque,
excludeFromSemantics: widget.excludeFromSemantics,
child: Semantics(
explicitChildNodes: !widget.excludeFromSemantics,
child: IgnorePointer(
key: _ignorePointerKey,
ignoring: _shouldIgnorePointer,
ignoringSemantics: false,
child: widget.viewportBuilder(context, position),
),
),
Expand Down
20 changes: 20 additions & 0 deletions packages/flutter/test/widgets/scrollable_of_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,24 @@ void main() {
controller.jumpTo(400.0);
expect(logValue, 'listener 400.0');
});

testWidgets('Scrollable.of() is possible using ScrollNotification context', (WidgetTester tester) async {
ScrollNotification notification;

await tester.pumpWidget(NotificationListener<ScrollNotification>(
onNotification: (ScrollNotification value) {
notification = value;
return false;
},
child: SingleChildScrollView(
child: const SizedBox(height: 1200.0)
)
));

await tester.startGesture(const Offset(100.0, 100.0));
await tester.pump(const Duration(seconds: 1));

final StatefulElement scrollableElement = find.byType(Scrollable).evaluate().first;
expect(Scrollable.of(notification.context), equals(scrollableElement.state));
});
}

0 comments on commit 649f49d

Please sign in to comment.