Unverified Commit cff3211f authored by xubaolin's avatar xubaolin Committed by GitHub

fix a NestedScrollView's ScrollPosition access bug (#80573)

parent ef7b6ffb
......@@ -1317,6 +1317,7 @@ class _NestedScrollController extends ScrollController {
@override
void detach(ScrollPosition position) {
assert(position is _NestedScrollPosition);
(position as _NestedScrollPosition).setParent(null);
position.removeListener(_scheduleUpdateShadow);
super.detach(position);
_scheduleUpdateShadow();
......@@ -1621,12 +1622,6 @@ class _NestedScrollPosition extends ScrollPosition implements ScrollActivityDele
Drag drag(DragStartDetails details, VoidCallback dragCancelCallback) {
return coordinator.drag(details, dragCancelCallback);
}
@override
void dispose() {
_parent?.detach(this);
super.dispose();
}
}
enum _NestedBallisticScrollActivityMode { outer, inner, independent }
......
......@@ -2342,6 +2342,53 @@ void main() {
expect(isScrolled, false);
expect(tester.takeException(), isNull);
});
// Regression test of https://github.com/flutter/flutter/issues/74372
testWidgets('ScrollPosition can be accessed during `_updatePosition()`', (WidgetTester tester) async {
final ScrollController controller = ScrollController();
late ScrollPosition position;
Widget buildFrame({ScrollPhysics? physics}) {
return Directionality(
textDirection: TextDirection.ltr,
child: Localizations(
locale: const Locale('en', 'US'),
delegates: const <LocalizationsDelegate<dynamic>>[
DefaultMaterialLocalizations.delegate,
DefaultWidgetsLocalizations.delegate,
],
child: MediaQuery(
data: const MediaQueryData(),
child: NestedScrollView(
controller: controller,
physics: physics,
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
Builder(
builder: (BuildContext context) {
position = controller.position;
return const SliverAppBar(
floating: true,
title: Text('AA'),
);
},
),
];
},
body: Container(),
),
),
),
);
}
await tester.pumpWidget(buildFrame());
expect(position.pixels, 0.0);
//Trigger `_updatePosition()`.
await tester.pumpWidget(buildFrame(physics: const _CustomPhysics()));
expect(position.pixels, 0.0);
});
}
class TestHeader extends SliverPersistentHeaderDelegate {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment