Skip to content

Commit 3f531e5

Browse files
MairramerPiinksvictorsanni
authored
Make ScrollPhysics reactive by introducing shouldUpdate (flutter#189664)
Closes flutter#80051 https://github.com/user-attachments/assets/92834288-a59c-4910-86d1-4471fcce0914 ## 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. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- 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/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Kate Lovett <[email protected]> Co-authored-by: Victor Sanni <[email protected]>
1 parent e100f30 commit 3f531e5

6 files changed

Lines changed: 344 additions & 9 deletions

File tree

‎packages/flutter/lib/src/widgets/page_view.dart‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,14 @@ class _ForceImplicitScrollPhysics extends ScrollPhysics {
553553

554554
@override
555555
final bool allowImplicitScrolling;
556+
557+
@override
558+
bool shouldUpdate(covariant _ForceImplicitScrollPhysics old) {
559+
if (allowImplicitScrolling != old.allowImplicitScrolling) {
560+
return true;
561+
}
562+
return super.shouldUpdate(old);
563+
}
556564
}
557565

558566
/// Scroll physics used by a [PageView].

‎packages/flutter/lib/src/widgets/scroll_physics.dart‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,42 @@ class ScrollPhysics {
499499
/// Whether a viewport is allowed to change the scroll position as the result of user input.
500500
bool get allowUserScrolling => true;
501501

502+
/// Called whenever a [Scrollable] is rebuilt with a new [ScrollPhysics]
503+
/// of the same [runtimeType].
504+
///
505+
/// If the new instance represents different information than the old
506+
/// instance, then the method should return true, otherwise it should return
507+
/// false.
508+
///
509+
/// If this method returns true, the [Scrollable] will update its
510+
/// [ScrollPosition] with the new [ScrollPhysics]. If this method returns
511+
/// false, the physics update on the existing [ScrollPosition] is skipped,
512+
/// though the position may still be recreated if other properties on
513+
/// [Scrollable] force a reset.
514+
///
515+
/// Subclasses that contain configuration parameters should override this
516+
/// method to return true when those parameters change, and should call
517+
/// `super.shouldUpdate(old)` to also update when the [parent] changes.
518+
///
519+
/// The base class implementation returns false if `this` and [old] are
520+
/// [identical], and returns true if the [runtimeType] or [parent] changes.
521+
@mustCallSuper
522+
bool shouldUpdate(covariant ScrollPhysics old) {
523+
if (identical(this, old)) {
524+
return false;
525+
}
526+
if (old.runtimeType != runtimeType) {
527+
return true;
528+
}
529+
if (parent?.runtimeType != old.parent?.runtimeType) {
530+
return true;
531+
}
532+
if (parent != null) {
533+
return parent!.shouldUpdate(old.parent!);
534+
}
535+
return false;
536+
}
537+
502538
@override
503539
String toString() {
504540
if (parent == null) {
@@ -690,6 +726,14 @@ class BouncingScrollPhysics extends ScrollPhysics {
690726
return BouncingScrollPhysics(parent: buildParent(ancestor), decelerationRate: decelerationRate);
691727
}
692728

729+
@override
730+
bool shouldUpdate(covariant BouncingScrollPhysics old) {
731+
if (decelerationRate != old.decelerationRate) {
732+
return true;
733+
}
734+
return super.shouldUpdate(old);
735+
}
736+
693737
/// The multiple applied to overscroll to make it appear that scrolling past
694738
/// the edge of the scrollable contents is harder than scrolling within bounds.
695739
/// This is done by reducing the ratio of the scroll effect output vs the

‎packages/flutter/lib/src/widgets/scrollable.dart‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -684,16 +684,18 @@ class ScrollableState extends State<Scrollable>
684684
widget.scrollBehavior!.shouldNotify(oldWidget.scrollBehavior!)) {
685685
return true;
686686
}
687-
ScrollPhysics? newPhysics = widget.physics ?? widget.scrollBehavior?.getScrollPhysics(context);
688-
ScrollPhysics? oldPhysics =
687+
688+
final ScrollPhysics? newPhysics =
689+
widget.physics ?? widget.scrollBehavior?.getScrollPhysics(context);
690+
final ScrollPhysics? oldPhysics =
689691
oldWidget.physics ?? oldWidget.scrollBehavior?.getScrollPhysics(context);
690-
do {
691-
if (newPhysics?.runtimeType != oldPhysics?.runtimeType) {
692-
return true;
693-
}
694-
newPhysics = newPhysics?.parent;
695-
oldPhysics = oldPhysics?.parent;
696-
} while (newPhysics != null || oldPhysics != null);
692+
693+
if (newPhysics?.runtimeType != oldPhysics?.runtimeType) {
694+
return true;
695+
}
696+
if (newPhysics != null && oldPhysics != null && newPhysics.shouldUpdate(oldPhysics)) {
697+
return true;
698+
}
697699

698700
return widget.controller?.runtimeType != oldWidget.controller?.runtimeType;
699701
}

‎packages/flutter/test/widgets/page_view_test.dart‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,4 +1874,65 @@ void main() {
18741874
expect(controller.page, 1.0);
18751875
},
18761876
);
1877+
1878+
testWidgets('PageView updates _ForceImplicitScrollPhysics when allowImplicitScrolling changes', (
1879+
WidgetTester tester,
1880+
) async {
1881+
await tester.pumpWidget(
1882+
Directionality(
1883+
textDirection: TextDirection.ltr,
1884+
child: PageView(children: const <Widget>[Text('Page 1'), Text('Page 2')]),
1885+
),
1886+
);
1887+
1888+
ScrollableState scrollable = tester.state(find.byType(Scrollable));
1889+
final ScrollPhysics initialPhysics = scrollable.position.physics;
1890+
expect(initialPhysics.allowImplicitScrolling, isFalse);
1891+
1892+
await tester.pumpWidget(
1893+
Directionality(
1894+
textDirection: TextDirection.ltr,
1895+
child: PageView(
1896+
allowImplicitScrolling: true,
1897+
children: const <Widget>[Text('Page 1'), Text('Page 2')],
1898+
),
1899+
),
1900+
);
1901+
1902+
scrollable = tester.state(find.byType(Scrollable));
1903+
final ScrollPhysics updatedPhysics = scrollable.position.physics;
1904+
expect(updatedPhysics.allowImplicitScrolling, isTrue);
1905+
expect(identical(initialPhysics, updatedPhysics), isFalse);
1906+
});
1907+
1908+
testWidgets(
1909+
'PageView skips updating _ForceImplicitScrollPhysics when allowImplicitScrolling is unchanged',
1910+
(WidgetTester tester) async {
1911+
await tester.pumpWidget(
1912+
Directionality(
1913+
textDirection: TextDirection.ltr,
1914+
child: PageView(
1915+
allowImplicitScrolling: true,
1916+
children: const <Widget>[Text('Page 1'), Text('Page 2')],
1917+
),
1918+
),
1919+
);
1920+
1921+
final ScrollableState scrollable = tester.state(find.byType(Scrollable));
1922+
final ScrollPhysics initialPhysics = scrollable.position.physics;
1923+
1924+
await tester.pumpWidget(
1925+
Directionality(
1926+
textDirection: TextDirection.ltr,
1927+
child: PageView(
1928+
allowImplicitScrolling: true,
1929+
children: const <Widget>[Text('Page 1'), Text('Page 2')],
1930+
),
1931+
),
1932+
);
1933+
1934+
final ScrollPhysics currentPhysics = scrollable.position.physics;
1935+
expect(identical(initialPhysics, currentPhysics), isTrue);
1936+
},
1937+
);
18771938
}

‎packages/flutter/test/widgets/scroll_physics_test.dart‎

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,4 +364,187 @@ FlutterError
364364
);
365365
await tester.fling(find.text('Index 2'), const Offset(0.0, -300.0), 10000.0);
366366
});
367+
368+
testWidgets('ScrollPhysics updates position when shouldUpdate returns true', (
369+
WidgetTester tester,
370+
) async {
371+
var physicsValue = 0;
372+
373+
Widget buildScrollable() {
374+
return Directionality(
375+
textDirection: TextDirection.ltr,
376+
child: ListView.builder(
377+
physics: ReactiveScrollPhysics(value: physicsValue),
378+
itemBuilder: (BuildContext context, int index) => Text('Item $index'),
379+
itemCount: 10,
380+
),
381+
);
382+
}
383+
384+
await tester.pumpWidget(buildScrollable());
385+
386+
ScrollableState scrollable = tester.state<ScrollableState>(find.byType(Scrollable));
387+
final ScrollPosition firstPosition = scrollable.position;
388+
expect((firstPosition.physics as ReactiveScrollPhysics).value, 0);
389+
390+
// When the physics is updated with an identical runtimeType and shouldUpdate
391+
// returns false, the ScrollPosition is not recreated.
392+
await tester.pumpWidget(buildScrollable());
393+
scrollable = tester.state<ScrollableState>(find.byType(Scrollable));
394+
expect(scrollable.position, same(firstPosition));
395+
expect((scrollable.position.physics as ReactiveScrollPhysics).value, 0);
396+
397+
// When the physics is updated with an identical runtimeType but shouldUpdate
398+
// returns true, the ScrollPosition is recreated with the updated physics.
399+
physicsValue = 1;
400+
await tester.pumpWidget(buildScrollable());
401+
402+
scrollable = tester.state<ScrollableState>(find.byType(Scrollable));
403+
final ScrollPosition secondPosition = scrollable.position;
404+
expect(secondPosition, isNot(same(firstPosition)));
405+
expect((secondPosition.physics as ReactiveScrollPhysics).value, 1);
406+
});
407+
408+
testWidgets('BouncingScrollPhysics updates position when decelerationRate changes', (
409+
WidgetTester tester,
410+
) async {
411+
ScrollDecelerationRate decelerationRate = ScrollDecelerationRate.normal;
412+
413+
Widget buildScrollable() {
414+
return Directionality(
415+
textDirection: TextDirection.ltr,
416+
child: ListView.builder(
417+
physics: BouncingScrollPhysics(decelerationRate: decelerationRate),
418+
itemBuilder: (BuildContext context, int index) => Text('Item $index'),
419+
itemCount: 10,
420+
),
421+
);
422+
}
423+
424+
await tester.pumpWidget(buildScrollable());
425+
426+
ScrollableState scrollable = tester.state<ScrollableState>(find.byType(Scrollable));
427+
final ScrollPosition firstPosition = scrollable.position;
428+
expect(
429+
(firstPosition.physics as BouncingScrollPhysics).decelerationRate,
430+
ScrollDecelerationRate.normal,
431+
);
432+
433+
// Identical configuration should not recreate ScrollPosition.
434+
await tester.pumpWidget(buildScrollable());
435+
scrollable = tester.state<ScrollableState>(find.byType(Scrollable));
436+
expect(scrollable.position, same(firstPosition));
437+
expect(
438+
(scrollable.position.physics as BouncingScrollPhysics).decelerationRate,
439+
ScrollDecelerationRate.normal,
440+
);
441+
442+
// Different decelerationRate should recreate ScrollPosition.
443+
decelerationRate = ScrollDecelerationRate.fast;
444+
await tester.pumpWidget(buildScrollable());
445+
446+
scrollable = tester.state<ScrollableState>(find.byType(Scrollable));
447+
final ScrollPosition secondPosition = scrollable.position;
448+
expect(secondPosition, isNot(same(firstPosition)));
449+
expect(
450+
(secondPosition.physics as BouncingScrollPhysics).decelerationRate,
451+
ScrollDecelerationRate.fast,
452+
);
453+
});
454+
455+
testWidgets(
456+
'ScrollPhysics.shouldUpdate handles parent change to physics with covariant parameter without TypeError',
457+
(WidgetTester tester) async {
458+
ScrollPhysics parentPhysics = const ClampingScrollPhysics();
459+
460+
Widget buildScrollable() {
461+
return Directionality(
462+
textDirection: TextDirection.ltr,
463+
child: ListView.builder(
464+
physics: _TestWrapperPhysics(parent: parentPhysics),
465+
itemBuilder: (BuildContext context, int index) => Text('Item $index'),
466+
itemCount: 10,
467+
),
468+
);
469+
}
470+
471+
await tester.pumpWidget(buildScrollable());
472+
473+
final ScrollableState scrollable1 = tester.state<ScrollableState>(find.byType(Scrollable));
474+
final ScrollPosition firstPosition = scrollable1.position;
475+
476+
parentPhysics = const _CovariantParentPhysics();
477+
478+
await tester.pumpWidget(buildScrollable());
479+
480+
final ScrollableState scrollable2 = tester.state<ScrollableState>(find.byType(Scrollable));
481+
final ScrollPosition secondPosition = scrollable2.position;
482+
483+
expect(secondPosition, isNot(same(firstPosition)));
484+
},
485+
);
486+
487+
test('ScrollPhysics.shouldUpdate returns false immediately for identical instances', () {
488+
const ScrollPhysics physics = BouncingScrollPhysics();
489+
490+
expect(physics.shouldUpdate(physics), isFalse);
491+
});
492+
493+
test('ScrollPhysics.shouldUpdate evaluates non-identical instances correctly', () {
494+
const ScrollPhysics physicsB1 = BouncingScrollPhysics(parent: ClampingScrollPhysics());
495+
const ScrollPhysics physicsB2 = BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics());
496+
497+
expect(identical(physicsB1, physicsB2), isFalse);
498+
expect(physicsB1.shouldUpdate(physicsB2), isTrue);
499+
500+
const ScrollPhysics physicsC1 = BouncingScrollPhysics();
501+
const ScrollPhysics physicsC2 = BouncingScrollPhysics(parent: ClampingScrollPhysics());
502+
503+
expect(identical(physicsC1, physicsC2), isFalse);
504+
expect(physicsC1.shouldUpdate(physicsC2), isTrue);
505+
506+
expect(identical(physicsB1, physicsC2), isTrue);
507+
expect(physicsB1.shouldUpdate(physicsC2), isFalse);
508+
});
509+
}
510+
511+
class ReactiveScrollPhysics extends ScrollPhysics {
512+
const ReactiveScrollPhysics({required this.value, super.parent});
513+
final int value;
514+
515+
@override
516+
ReactiveScrollPhysics applyTo(ScrollPhysics? ancestor) {
517+
return ReactiveScrollPhysics(value: value, parent: buildParent(ancestor));
518+
}
519+
520+
@override
521+
bool shouldUpdate(covariant ReactiveScrollPhysics old) {
522+
if (value != old.value) {
523+
return true;
524+
}
525+
return super.shouldUpdate(old);
526+
}
527+
}
528+
529+
class _TestWrapperPhysics extends ScrollPhysics {
530+
const _TestWrapperPhysics({super.parent});
531+
532+
@override
533+
_TestWrapperPhysics applyTo(ScrollPhysics? ancestor) {
534+
return _TestWrapperPhysics(parent: buildParent(ancestor));
535+
}
536+
}
537+
538+
class _CovariantParentPhysics extends ScrollPhysics {
539+
const _CovariantParentPhysics({super.parent});
540+
541+
@override
542+
_CovariantParentPhysics applyTo(ScrollPhysics? ancestor) {
543+
return _CovariantParentPhysics(parent: buildParent(ancestor));
544+
}
545+
546+
@override
547+
bool shouldUpdate(covariant _CovariantParentPhysics old) {
548+
return super.shouldUpdate(old);
549+
}
367550
}

‎packages/flutter/test/widgets/scrollable_test.dart‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,6 +1797,43 @@ void main() {
17971797
);
17981798
expect(tester.getSize(find.byType(Scrollable)), Size.zero);
17991799
});
1800+
1801+
testWidgets('Scrollable short-circuits shouldUpdate when physics reference is identical', (
1802+
WidgetTester tester,
1803+
) async {
1804+
const ScrollPhysics sharedPhysics = BouncingScrollPhysics();
1805+
1806+
await tester.pumpWidget(
1807+
Directionality(
1808+
textDirection: TextDirection.ltr,
1809+
child: Scrollable(
1810+
physics: sharedPhysics,
1811+
viewportBuilder: (BuildContext context, ViewportOffset offset) {
1812+
return const SizedBox();
1813+
},
1814+
),
1815+
),
1816+
);
1817+
1818+
final ScrollableState scrollable = tester.state(find.byType(Scrollable));
1819+
final ScrollPhysics initialPhysics = scrollable.position.physics;
1820+
1821+
await tester.pumpWidget(
1822+
Directionality(
1823+
textDirection: TextDirection.ltr,
1824+
child: Scrollable(
1825+
physics: sharedPhysics,
1826+
viewportBuilder: (BuildContext context, ViewportOffset offset) {
1827+
return const SizedBox();
1828+
},
1829+
),
1830+
),
1831+
);
1832+
1833+
final ScrollPhysics currentPhysics = scrollable.position.physics;
1834+
1835+
expect(identical(initialPhysics, currentPhysics), isTrue);
1836+
});
18001837
}
18011838

18021839
// ignore: must_be_immutable

0 commit comments

Comments
 (0)