mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Helpful assertion for isAlwaysShown error (#58258)
This commit is contained in:
parent
6d8ec35014
commit
7d17c53992
@ -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();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user