@@ -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}
0 commit comments