Update Scrollable scrollBehavior when ScrollConfiguration changes (#5647)
This commit is contained in:
parent
e0f6c0db84
commit
c76decb842
@ -264,6 +264,13 @@ class ScrollableState<T extends Scrollable> extends State<T> {
|
|||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dependenciesChanged() {
|
||||||
|
_scrollBehavior = createScrollBehavior();
|
||||||
|
didUpdateScrollBehavior(scrollOffset);
|
||||||
|
super.dependenciesChanged();
|
||||||
|
}
|
||||||
|
|
||||||
/// The current scroll offset.
|
/// The current scroll offset.
|
||||||
///
|
///
|
||||||
/// The scroll offset is applied to the child widget along the scroll
|
/// The scroll offset is applied to the child widget along the scroll
|
||||||
@ -351,8 +358,6 @@ class ScrollableState<T extends Scrollable> extends State<T> {
|
|||||||
/// or its createScrollBehavior callback is null, then return a new instance
|
/// or its createScrollBehavior callback is null, then return a new instance
|
||||||
/// of [OverscrollWhenScrollableBehavior].
|
/// of [OverscrollWhenScrollableBehavior].
|
||||||
ExtentScrollBehavior createScrollBehavior() {
|
ExtentScrollBehavior createScrollBehavior() {
|
||||||
// TODO(hansmuller): this will not be called when the ScrollConfiguration changes.
|
|
||||||
// An override of dependenciesChanged() is probably needed.
|
|
||||||
return ScrollConfiguration.of(context)?.createScrollBehavior();
|
return ScrollConfiguration.of(context)?.createScrollBehavior();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,17 +2,37 @@
|
|||||||
// Use of this source code is governed by a BSD-style license that can be
|
// Use of this source code is governed by a BSD-style license that can be
|
||||||
// found in the LICENSE file.
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:flutter/widgets.dart';
|
import 'package:flutter/widgets.dart';
|
||||||
import 'package:test/test.dart';
|
|
||||||
|
class TestScrollConfigurationDelegate extends ScrollConfigurationDelegate {
|
||||||
|
TestScrollConfigurationDelegate(this.flag);
|
||||||
|
|
||||||
|
final bool flag;
|
||||||
|
|
||||||
|
@override
|
||||||
|
TargetPlatform get platform => defaultTargetPlatform;
|
||||||
|
|
||||||
|
@override
|
||||||
|
ExtentScrollBehavior createScrollBehavior() {
|
||||||
|
return flag
|
||||||
|
? new BoundedBehavior(platform: platform)
|
||||||
|
: new UnboundedBehavior(platform: platform);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool updateShouldNotify(TestScrollConfigurationDelegate old) => flag != old.flag;
|
||||||
|
}
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
test('BoundedBehavior min scroll offset', () {
|
test('BoundedBehavior min scroll offset', () {
|
||||||
BoundedBehavior behavior = new BoundedBehavior(
|
BoundedBehavior behavior = new BoundedBehavior(
|
||||||
contentExtent: 150.0,
|
contentExtent: 150.0,
|
||||||
containerExtent: 75.0,
|
containerExtent: 75.0,
|
||||||
minScrollOffset: -100.0,
|
minScrollOffset: -100.0,
|
||||||
platform: TargetPlatform.iOS
|
platform: TargetPlatform.iOS
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(behavior.minScrollOffset, equals(-100.0));
|
expect(behavior.minScrollOffset, equals(-100.0));
|
||||||
expect(behavior.maxScrollOffset, equals(-25.0));
|
expect(behavior.maxScrollOffset, equals(-25.0));
|
||||||
|
|
||||||
@ -35,4 +55,51 @@ void main() {
|
|||||||
expect(behavior.maxScrollOffset, equals(125.0));
|
expect(behavior.maxScrollOffset, equals(125.0));
|
||||||
expect(scrollOffset, equals(50.0));
|
expect(scrollOffset, equals(50.0));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testWidgets('Inherited ScrollConfiguration changed', (WidgetTester tester) async {
|
||||||
|
final GlobalKey scrollableKey = new GlobalKey(debugLabel: 'scrollable');
|
||||||
|
TestScrollConfigurationDelegate delegate;
|
||||||
|
ExtentScrollBehavior behavior;
|
||||||
|
|
||||||
|
await tester.pumpWidget(
|
||||||
|
new ScrollConfiguration(
|
||||||
|
delegate: new TestScrollConfigurationDelegate(true),
|
||||||
|
child: new ScrollableViewport(
|
||||||
|
scrollableKey: scrollableKey,
|
||||||
|
child: new Builder(
|
||||||
|
builder: (BuildContext context) {
|
||||||
|
delegate = ScrollConfiguration.of(context);
|
||||||
|
behavior = Scrollable.of(context).scrollBehavior;
|
||||||
|
return new Container(height: 1000.0);
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(delegate, isNotNull);
|
||||||
|
expect(delegate.flag, isTrue);
|
||||||
|
expect(behavior, new isInstanceOf<BoundedBehavior>());
|
||||||
|
|
||||||
|
// Same Scrollable, different ScrollConfiguration
|
||||||
|
await tester.pumpWidget(
|
||||||
|
new ScrollConfiguration(
|
||||||
|
delegate: new TestScrollConfigurationDelegate(false),
|
||||||
|
child: new ScrollableViewport(
|
||||||
|
scrollableKey: scrollableKey,
|
||||||
|
child: new Builder(
|
||||||
|
builder: (BuildContext context) {
|
||||||
|
delegate = ScrollConfiguration.of(context);
|
||||||
|
behavior = Scrollable.of(context).scrollBehavior;
|
||||||
|
return new Container(height: 1000.0);
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(delegate, isNotNull);
|
||||||
|
expect(delegate.flag, isFalse);
|
||||||
|
expect(behavior, new isInstanceOf<UnboundedBehavior>());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user