Unverified Commit 7d17c539 authored by Justin McCandless's avatar Justin McCandless Committed by GitHub

Helpful assertion for isAlwaysShown error (#58258)

parent 6d8ec350
......@@ -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);
}
......
......@@ -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);
}
......
......@@ -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();
......
......@@ -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();
......
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