Skip to content

Commit 52bf9db

Browse files
authored
update ScrollMetricsNotification (flutter#87421)
1 parent 5bdc698 commit 52bf9db

File tree

5 files changed

+33
-22
lines changed

5 files changed

+33
-22
lines changed

packages/flutter/lib/src/widgets/scroll_position.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ abstract class ScrollPosition extends ViewportOffset with ScrollMetrics {
558558
// It isn't safe to trigger the ScrollMetricsNotification if we are in
559559
// the middle of rendering the frame, the developer is likely to schedule
560560
// a new frame(build scheduled during frame is illegal).
561-
if (_lastMetrics != null && !_haveScheduledUpdateNotification) {
561+
if (!_haveScheduledUpdateNotification) {
562562
scheduleMicrotask(didUpdateScrollMetrics);
563563
_haveScheduledUpdateNotification = true;
564564
}

packages/flutter/lib/src/widgets/scrollbar.dart

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,25 +1224,16 @@ class RawScrollbarState<T extends RawScrollbar> extends State<T> with TickerProv
12241224
@override
12251225
void didChangeDependencies() {
12261226
super.didChangeDependencies();
1227-
_maybeTriggerScrollbar();
1227+
assert(_debugScheduleCheckHasValidScrollPosition());
12281228
}
12291229

1230-
// Waits one frame and cause an empty scroll event (zero delta pixels).
1231-
//
1232-
// This allows the thumb to show immediately when isAlwaysShown is true.
1233-
// A scroll event is required in order to paint the thumb.
1234-
void _maybeTriggerScrollbar() {
1230+
bool _debugScheduleCheckHasValidScrollPosition() {
1231+
if (!showScrollbar)
1232+
return true;
12351233
WidgetsBinding.instance!.addPostFrameCallback((Duration duration) {
1236-
final ScrollController? scrollController = widget.controller ?? PrimaryScrollController.of(context);
1237-
if (showScrollbar) {
1238-
_fadeoutTimer?.cancel();
1239-
// Wait one frame and cause an empty scroll event. This allows the
1240-
// thumb to show immediately when isAlwaysShown is true. A scroll
1241-
// event is required in order to paint the thumb.
1242-
assert(_debugCheckHasValidScrollPosition());
1243-
scrollController!.position.didUpdateScrollPositionBy(0);
1244-
}
1234+
assert(_debugCheckHasValidScrollPosition());
12451235
});
1236+
return true;
12461237
}
12471238

12481239
void _validateInteractions(AnimationStatus status) {
@@ -1365,7 +1356,8 @@ class RawScrollbarState<T extends RawScrollbar> extends State<T> with TickerProv
13651356
super.didUpdateWidget(oldWidget);
13661357
if (widget.isAlwaysShown != oldWidget.isAlwaysShown) {
13671358
if (widget.isAlwaysShown == true) {
1368-
_maybeTriggerScrollbar();
1359+
assert(_debugScheduleCheckHasValidScrollPosition());
1360+
_fadeoutTimer?.cancel();
13691361
_fadeoutAnimationController.animateTo(1.0);
13701362
} else {
13711363
_fadeoutAnimationController.reverse();

packages/flutter/test/cupertino/scrollbar_test.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:ui' as ui;
66

77
import 'package:flutter/cupertino.dart';
8+
import 'package:flutter/foundation.dart';
89
import 'package:flutter/services.dart';
910
import 'package:flutter_test/flutter_test.dart';
1011

@@ -389,9 +390,16 @@ void main() {
389390
);
390391
}
391392

393+
final FlutterExceptionHandler? handler = FlutterError.onError;
394+
FlutterErrorDetails? error;
395+
FlutterError.onError = (FlutterErrorDetails details) {
396+
error = details;
397+
};
398+
392399
await tester.pumpWidget(viewWithScroll());
393-
final AssertionError exception = tester.takeException() as AssertionError;
394-
expect(exception, isAssertionError);
400+
expect(error, isNotNull);
401+
402+
FlutterError.onError = handler;
395403
},
396404
);
397405

packages/flutter/test/widgets/scroll_notification_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ void main() {
2121
);
2222
}
2323
await tester.pumpWidget(buildFrame(1200.0));
24-
expect(events.length, 0);
24+
expect(events.length, 1);
2525

26+
events.clear();
2627
await tester.pumpWidget(buildFrame(1000.0));
2728
// Change the content dimensions will trigger a new event.
2829
expect(events.length, 1);

packages/flutter/test/widgets/scrollbar_test.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:ui' as ui;
66

7+
import 'package:flutter/foundation.dart';
78
import 'package:flutter/src/physics/utils.dart' show nearEqual;
89
import 'package:flutter/widgets.dart';
910
import 'package:flutter_test/flutter_test.dart';
@@ -1143,6 +1144,12 @@ void main() {
11431144
});
11441145

11451146
testWidgets('RawScrollbar.isAlwaysShown asserts that a ScrollPosition is attached', (WidgetTester tester) async {
1147+
final FlutterExceptionHandler? handler = FlutterError.onError;
1148+
FlutterErrorDetails? error;
1149+
FlutterError.onError = (FlutterErrorDetails details) {
1150+
error = details;
1151+
};
1152+
11461153
await tester.pumpWidget(
11471154
Directionality(
11481155
textDirection: TextDirection.ltr,
@@ -1163,12 +1170,15 @@ void main() {
11631170
),
11641171
);
11651172
await tester.pumpAndSettle();
1166-
final AssertionError exception = tester.takeException() as AssertionError;
1167-
expect(exception, isAssertionError);
1173+
1174+
expect(error, isNotNull);
1175+
final AssertionError exception = error!.exception as AssertionError;
11681176
expect(
11691177
exception.message,
11701178
contains("The Scrollbar's ScrollController has no ScrollPosition attached."),
11711179
);
1180+
1181+
FlutterError.onError = handler;
11721182
});
11731183

11741184
testWidgets('Interactive scrollbars should have a valid scroll controller', (WidgetTester tester) async {

0 commit comments

Comments
 (0)