Skip to content

Commit abb7f63

Browse files
authored
Fix NestedScrollView example crash when switching tabs on desktop (flutter#186993)
Fixes flutter#183199 The `NestedScrollView` API example that uses a `TabBar`/`TabBarView` throws on desktop when you scroll an inner list and then switch tabs. The inner `CustomScrollView`s are coordinated by the `NestedScrollView` through a shared controller, which can be attached to more than one `ScrollPosition` at a time (for example mid-transition between tabs). The automatic desktop scrollbar attaches to that shared controller, and a single scrollbar cannot represent multiple positions, so `RawScrollbar` throws the multiple-position assertion. This leaves the framework unchanged and fixes the example itself: the `TabBarView` body is wrapped in a `ScrollConfiguration` that disables the default scrollbars, since a single scrollbar cannot correctly represent the coordinated inner positions. A regression test reproduces the exact assertion from the issue (scroll, then a mouse-wheel pointer signal during a tab transition) and confirms it no longer throws. ## Tests - `flutter test examples/api/test/widgets/nested_scroll_view/nested_scroll_view.0_test.dart` - `flutter analyze examples/api/lib/widgets/nested_scroll_view/nested_scroll_view.0.dart examples/api/test/widgets/nested_scroll_view/nested_scroll_view.0_test.dart` ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent cb0b0dd commit abb7f63

2 files changed

Lines changed: 117 additions & 59 deletions

File tree

‎examples/api/lib/widgets/nested_scroll_view/nested_scroll_view.0.dart‎

Lines changed: 68 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -64,66 +64,75 @@ class NestedScrollViewExample extends StatelessWidget {
6464
),
6565
];
6666
},
67-
body: TabBarView(
68-
// These are the contents of the tab views, below the tabs.
69-
children: tabs.map((String name) {
70-
return SafeArea(
71-
top: false,
72-
bottom: false,
73-
child: Builder(
74-
// This Builder is needed to provide a BuildContext that is
75-
// "inside" the NestedScrollView, so that
76-
// sliverOverlapAbsorberHandleFor() can find the
77-
// NestedScrollView.
78-
builder: (BuildContext context) {
79-
return CustomScrollView(
80-
// The "controller" and "primary" members should be left
81-
// unset, so that the NestedScrollView can control this
82-
// inner scroll view.
83-
// If the "controller" property is set, then this scroll
84-
// view will not be associated with the NestedScrollView.
85-
// The PageStorageKey should be unique to this ScrollView;
86-
// it allows the list to remember its scroll position when
87-
// the tab view is not on the screen.
88-
key: PageStorageKey<String>(name),
89-
slivers: <Widget>[
90-
SliverOverlapInjector(
91-
// This is the flip side of the SliverOverlapAbsorber
92-
// above.
93-
handle:
94-
NestedScrollView.sliverOverlapAbsorberHandleFor(
95-
context,
96-
),
97-
),
98-
SliverPadding(
99-
padding: const .all(8.0),
100-
// In this example, the inner scroll view has
101-
// fixed-height list items, hence the use of
102-
// SliverFixedExtentList. However, one could use any
103-
// sliver widget here, e.g. SliverList or SliverGrid.
104-
sliver: SliverFixedExtentList.builder(
105-
// The items in this example are fixed to 48 pixels
106-
// high. This matches the Material Design spec for
107-
// ListTile widgets.
108-
itemExtent: 48.0,
109-
// The itemCount of the SliverFixedExtentList.builder
110-
// specifies how many children this inner list
111-
// has. In this example, each tab has a list of
112-
// exactly 30 items, but this is arbitrary.
113-
itemCount: 30,
114-
itemBuilder: (BuildContext context, int index) {
115-
// This builder is called for each child.
116-
// In this example, we just number each list item.
117-
return ListTile(title: Text('Item $index'));
118-
},
67+
body: ScrollConfiguration(
68+
// Scrollbars have different default behaviors based on platform
69+
// expectations. For the purpose of this sample, which can be run on
70+
// any platform, default scrollbars are disabled for the inner
71+
// scrollables.
72+
behavior: ScrollConfiguration.of(
73+
context,
74+
).copyWith(scrollbars: false),
75+
child: TabBarView(
76+
// These are the contents of the tab views, below the tabs.
77+
children: tabs.map((String name) {
78+
return SafeArea(
79+
top: false,
80+
bottom: false,
81+
child: Builder(
82+
// This Builder is needed to provide a BuildContext that is
83+
// "inside" the NestedScrollView, so that
84+
// sliverOverlapAbsorberHandleFor() can find the
85+
// NestedScrollView.
86+
builder: (BuildContext context) {
87+
return CustomScrollView(
88+
// The "controller" and "primary" members should be left
89+
// unset, so that the NestedScrollView can control this
90+
// inner scroll view.
91+
// If the "controller" property is set, then this scroll
92+
// view will not be associated with the NestedScrollView.
93+
// The PageStorageKey should be unique to this ScrollView;
94+
// it allows the list to remember its scroll position when
95+
// the tab view is not on the screen.
96+
key: PageStorageKey<String>(name),
97+
slivers: <Widget>[
98+
SliverOverlapInjector(
99+
// This is the flip side of the SliverOverlapAbsorber
100+
// above.
101+
handle:
102+
NestedScrollView.sliverOverlapAbsorberHandleFor(
103+
context,
104+
),
119105
),
120-
),
121-
],
122-
);
123-
},
124-
),
125-
);
126-
}).toList(),
106+
SliverPadding(
107+
padding: const .all(8.0),
108+
// In this example, the inner scroll view has
109+
// fixed-height list items, hence the use of
110+
// SliverFixedExtentList. However, one could use any
111+
// sliver widget here, e.g. SliverList or SliverGrid.
112+
sliver: SliverFixedExtentList.builder(
113+
// The items in this example are fixed to 48 pixels
114+
// high. This matches the Material Design spec for
115+
// ListTile widgets.
116+
itemExtent: 48.0,
117+
// The itemCount of the SliverFixedExtentList.builder
118+
// specifies how many children this inner list
119+
// has. In this example, each tab has a list of
120+
// exactly 30 items, but this is arbitrary.
121+
itemCount: 30,
122+
itemBuilder: (BuildContext context, int index) {
123+
// This builder is called for each child.
124+
// In this example, we just number each list item.
125+
return ListTile(title: Text('Item $index'));
126+
},
127+
),
128+
),
129+
],
130+
);
131+
},
132+
),
133+
);
134+
}).toList(),
135+
),
127136
),
128137
),
129138
),

‎examples/api/test/widgets/nested_scroll_view/nested_scroll_view.0_test.dart‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'package:flutter/gestures.dart';
56
import 'package:flutter/material.dart';
67
import 'package:flutter_api_samples/widgets/nested_scroll_view/nested_scroll_view.0.dart'
78
as example;
@@ -50,4 +51,52 @@ void main() {
5051
lessThan(initialAppBarHeight),
5152
);
5253
});
54+
55+
testWidgets(
56+
'Does not crash when scrolling an inner list then switching tabs on desktop',
57+
(WidgetTester tester) async {
58+
// Regression test for https://github.com/flutter/flutter/issues/183199
59+
//
60+
// Without the ScrollConfiguration in this example, the default desktop
61+
// scrollbar attaches to the NestedScrollView's coordinated controller and
62+
// throws once more than one ScrollPosition is attached to it, which happens
63+
// mid tab transition.
64+
await tester.pumpWidget(const example.NestedScrollViewExampleApp());
65+
await tester.pumpAndSettle();
66+
67+
// The example opts out of the default scrollbars for its body.
68+
expect(find.byType(Scrollbar), findsNothing);
69+
70+
// Scroll the first tab's inner list.
71+
await tester.drag(
72+
find.text('Item 0'),
73+
const Offset(0.0, -100.0),
74+
touchSlopY: 0.0,
75+
);
76+
await tester.pump();
77+
78+
// Begin, but do not finish, a tab transition so both tabs' inner scroll
79+
// views are attached to the coordinated controller at the same time.
80+
await tester.fling(
81+
find.byType(TabBarView),
82+
const Offset(-300.0, 0.0),
83+
800.0,
84+
);
85+
await tester.pump();
86+
await tester.pump(const Duration(milliseconds: 30));
87+
88+
// A mouse-wheel pointer signal mid-transition previously drove the
89+
// scrollbar validation while more than one position was attached.
90+
final TestPointer pointer = TestPointer(1, PointerDeviceKind.mouse);
91+
final Offset center = tester.getCenter(find.byType(TabBarView));
92+
await tester.sendEventToBinding(pointer.hover(center));
93+
await tester.sendEventToBinding(pointer.scroll(const Offset(0.0, 60.0)));
94+
await tester.pump();
95+
await tester.pump(const Duration(milliseconds: 700));
96+
await tester.pumpAndSettle();
97+
98+
expect(tester.takeException(), isNull);
99+
},
100+
variant: TargetPlatformVariant.desktop(),
101+
);
53102
}

0 commit comments

Comments
 (0)