Helpful assertion for isAlwaysShown error (#58258)

This commit is contained in:
Justin McCandless 2020-05-29 15:48:01 -07:00 committed by GitHub
parent 6d8ec35014
commit 7d17c53992
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 106 additions and 4 deletions

View File

@ -62,7 +62,8 @@ class CupertinoScrollbar extends StatefulWidget {
this.controller,
this.isAlwaysShown = false,
@required this.child,
}) : super(key: key);
}) : assert(!isAlwaysShown || controller != null, 'When isAlwaysShown is true, must pass a controller that is attached to a scroll view'),
super(key: key);
/// The subtree to place inside the [CupertinoScrollbar].
///
@ -276,7 +277,6 @@ class _CupertinoScrollbarState extends State<CupertinoScrollbar> with TickerProv
void _triggerScrollbar() {
WidgetsBinding.instance.addPostFrameCallback((Duration duration) {
if (widget.isAlwaysShown) {
assert(widget.controller != null);
_fadeoutTimer?.cancel();
widget.controller.position.didUpdateScrollPositionBy(0);
}

View File

@ -38,7 +38,8 @@ class Scrollbar extends StatefulWidget {
@required this.child,
this.controller,
this.isAlwaysShown = false,
}) : super(key: key);
}) : assert(!isAlwaysShown || controller != null, 'When isAlwaysShown is true, must pass a controller that is attached to a scroll view'),
super(key: key);
/// The widget below this widget in the tree.
///
@ -131,7 +132,6 @@ class _ScrollbarState extends State<Scrollbar> with TickerProviderStateMixin {
void _triggerScrollbar() {
WidgetsBinding.instance.addPostFrameCallback((Duration duration) {
if (widget.isAlwaysShown) {
assert(widget.controller != null);
_fadeoutTimer?.cancel();
widget.controller.position.didUpdateScrollPositionBy(0);
}

View File

@ -164,6 +164,58 @@ void main() {
await tester.pump(_kScrollbarFadeDuration);
});
testWidgets('When isAlwaysShown is true, must pass a controller',
(WidgetTester tester) async {
Widget viewWithScroll() {
return Directionality(
textDirection: TextDirection.ltr,
child: MediaQuery(
data: const MediaQueryData(),
child: CupertinoScrollbar(
isAlwaysShown: true,
child: const SingleChildScrollView(
child: SizedBox(
width: 4000.0,
height: 4000.0,
),
),
),
),
);
}
expect(() async {
await tester.pumpWidget(viewWithScroll());
}, throwsAssertionError);
});
testWidgets('When isAlwaysShown is true, must pass a controller that is attached to a scroll view',
(WidgetTester tester) async {
final ScrollController controller = ScrollController();
Widget viewWithScroll() {
return Directionality(
textDirection: TextDirection.ltr,
child: MediaQuery(
data: const MediaQueryData(),
child: CupertinoScrollbar(
controller: controller,
isAlwaysShown: true,
child: const SingleChildScrollView(
child: SizedBox(
width: 4000.0,
height: 4000.0,
),
),
),
),
);
}
await tester.pumpWidget(viewWithScroll());
final dynamic exception = tester.takeException();
expect(exception, isAssertionError);
});
testWidgets('On first render with isAlwaysShown: true, the thumb shows',
(WidgetTester tester) async {
final ScrollController controller = ScrollController();

View File

@ -201,6 +201,56 @@ void main() {
expect(scrollbar.controller, isNotNull);
}, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS, TargetPlatform.macOS }));
testWidgets('When isAlwaysShown is true, must pass a controller',
(WidgetTester tester) async {
Widget viewWithScroll() {
return _buildBoilerplate(
child: Theme(
data: ThemeData(),
child: Scrollbar(
isAlwaysShown: true,
child: const SingleChildScrollView(
child: SizedBox(
width: 4000.0,
height: 4000.0,
),
),
),
),
);
}
expect(() async {
await tester.pumpWidget(viewWithScroll());
}, throwsAssertionError);
});
testWidgets('When isAlwaysShown is true, must pass a controller that is attached to a scroll view',
(WidgetTester tester) async {
final ScrollController controller = ScrollController();
Widget viewWithScroll() {
return _buildBoilerplate(
child: Theme(
data: ThemeData(),
child: Scrollbar(
isAlwaysShown: true,
controller: controller,
child: const SingleChildScrollView(
child: SizedBox(
width: 4000.0,
height: 4000.0,
),
),
),
),
);
}
await tester.pumpWidget(viewWithScroll());
final dynamic exception = tester.takeException();
expect(exception, isAssertionError);
});
testWidgets('On first render with isAlwaysShown: true, the thumb shows',
(WidgetTester tester) async {
final ScrollController controller = ScrollController();