Unverified Commit 7c9feea8 authored by Kate Lovett's avatar Kate Lovett Committed by GitHub

Expose onAttach and onDetach in ScrollController subclasses (#135721)

Fixes https://github.com/flutter/flutter/issues/135574
This exposes the onAttach and onDetach callbacks of ScrollController in all of its subclasses.
parent 0f947a09
......@@ -217,6 +217,8 @@ class FixedExtentScrollController extends ScrollController {
/// [initialItem] defaults to zero.
FixedExtentScrollController({
this.initialItem = 0,
super.onAttach,
super.onDetach,
});
/// The page to show when first creating the scroll view.
......
......@@ -116,6 +116,8 @@ class PageController extends ScrollController {
this.initialPage = 0,
this.keepPage = true,
this.viewportFraction = 1.0,
super.onAttach,
super.onDetach,
}) : assert(viewportFraction > 0.0);
/// The page to show when first creating the [PageView].
......
......@@ -380,6 +380,8 @@ class TrackingScrollController extends ScrollController {
super.initialScrollOffset,
super.keepScrollOffset,
super.debugLabel,
super.onAttach,
super.onDetach,
});
final Map<ScrollPosition, VoidCallback> _positionToListener = <ScrollPosition, VoidCallback>{};
......
......@@ -83,6 +83,37 @@ void main() {
expect(tester.getSize(find.byType(ListWheelScrollView)), const Size(800.0, 600.0));
});
testWidgetsWithLeakTracking('FixedExtentScrollController onAttach, onDetach', (WidgetTester tester) async {
int attach = 0;
int detach = 0;
final FixedExtentScrollController controller = FixedExtentScrollController(
onAttach: (_) { attach++; },
onDetach: (_) { detach++; },
);
addTearDown(controller.dispose);
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: ListWheelScrollView(
controller: controller,
itemExtent: 50.0,
children: const <Widget>[],
),
),
);
await tester.pumpAndSettle();
expect(attach, 1);
expect(detach, 0);
await tester.pumpWidget(Container());
await tester.pumpAndSettle();
expect(attach, 1);
expect(detach, 1);
});
testWidgets('ListWheelScrollView needs positive magnification', (WidgetTester tester) async {
expect(
() {
......
......@@ -1265,4 +1265,39 @@ void main() {
// Check the stretch factor in the first element of the transform matrix.
expect(transform.transform.storage.first, 1.0);
});
testWidgetsWithLeakTracking('PageController onAttach, onDetach', (WidgetTester tester) async {
int attach = 0;
int detach = 0;
final PageController controller = PageController(
onAttach: (_) { attach++; },
onDetach: (_) { detach++; },
);
addTearDown(controller.dispose);
await tester.pumpWidget(MaterialApp(
theme: ThemeData(useMaterial3: true),
home: Center(
child: PageView(
controller: controller,
physics: const PageScrollPhysics().applyTo(const ClampingScrollPhysics()),
children: const <Widget>[
Center(child: Text('First Page')),
Center(child: Text('Second Page')),
Center(child: Text('Third Page')),
],
),
),
));
await tester.pumpAndSettle();
expect(attach, 1);
expect(detach, 0);
await tester.pumpWidget(Container());
await tester.pumpAndSettle();
expect(attach, 1);
expect(detach, 1);
});
}
......@@ -60,4 +60,43 @@ void main() {
expect(controller.initialScrollOffset, 0.0);
});
testWidgetsWithLeakTracking('TrackingScrollController saves offset', (WidgetTester tester) async {
int attach = 0;
int detach = 0;
final TrackingScrollController controller = TrackingScrollController(
onAttach: (_) { attach++; },
onDetach: (_) { detach++; },
);
addTearDown(controller.dispose);
const double listItemHeight = 100.0;
await tester.pumpWidget(Directionality(
textDirection: TextDirection.ltr,
child: PageView.builder(
itemBuilder: (BuildContext context, int index) {
return ListView(
controller: controller,
children: List<Widget>.generate(
10,
(int i) => SizedBox(
height: listItemHeight,
child: Text('Page$index-Item$i'),
),
).toList(),
);
},
),
));
await tester.pumpAndSettle();
expect(attach, 1);
expect(detach, 0);
await tester.pumpWidget(Container());
await tester.pumpAndSettle();
expect(attach, 1);
expect(detach, 1);
});
}
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