Migrate Switch tests to null safety. (#68133)
diff --git a/packages/flutter/lib/src/material/switch.dart b/packages/flutter/lib/src/material/switch.dart
index 660d877..6efe081 100644
--- a/packages/flutter/lib/src/material/switch.dart
+++ b/packages/flutter/lib/src/material/switch.dart
@@ -149,7 +149,7 @@
/// },
/// )
/// ```
- final ValueChanged<bool?>? onChanged;
+ final ValueChanged<bool>? onChanged;
/// The color to use when this switch is on.
///
@@ -440,7 +440,7 @@
final Color activeTrackColor;
final Color inactiveTrackColor;
final ImageConfiguration configuration;
- final ValueChanged<bool?>? onChanged;
+ final ValueChanged<bool>? onChanged;
final BoxConstraints additionalConstraints;
final DragStartBehavior dragStartBehavior;
final bool hasFocus;
@@ -463,7 +463,7 @@
activeTrackColor: activeTrackColor,
inactiveTrackColor: inactiveTrackColor,
configuration: configuration,
- onChanged: onChanged,
+ onChanged: onChanged != null ? _handleValueChanged : null,
textDirection: Directionality.of(context)!,
additionalConstraints: additionalConstraints,
hasFocus: hasFocus,
@@ -487,7 +487,7 @@
..activeTrackColor = activeTrackColor
..inactiveTrackColor = inactiveTrackColor
..configuration = configuration
- ..onChanged = onChanged
+ ..onChanged = onChanged != null ? _handleValueChanged : null
..textDirection = Directionality.of(context)!
..additionalConstraints = additionalConstraints
..dragStartBehavior = dragStartBehavior
@@ -495,6 +495,17 @@
..hovering = hovering
..vsync = state;
}
+
+ void _handleValueChanged(bool? value) {
+ // Wrap the onChanged callback because the RenderToggleable supports tri-state
+ // values (i.e. value can be null), but the Switch doesn't. We pass false
+ // for the tristate param to RenderToggleable, so value should never
+ // be null.
+ assert(value != null);
+ if (onChanged != null) {
+ onChanged!(value!);
+ }
+ }
}
class _RenderSwitch extends RenderToggleable {
@@ -650,7 +661,6 @@
}
}
-
@override
void detach() {
_cachedThumbPainter?.dispose();
diff --git a/packages/flutter/lib/src/material/switch_list_tile.dart b/packages/flutter/lib/src/material/switch_list_tile.dart
index 567b427..a34dd00 100644
--- a/packages/flutter/lib/src/material/switch_list_tile.dart
+++ b/packages/flutter/lib/src/material/switch_list_tile.dart
@@ -346,7 +346,7 @@
/// title: Text('Selection'),
/// )
/// ```
- final ValueChanged<bool?>? onChanged;
+ final ValueChanged<bool>? onChanged;
/// The color to use when this switch is on.
///
diff --git a/packages/flutter/test/material/switch_list_tile_test.dart b/packages/flutter/test/material/switch_list_tile_test.dart
index 053173b..be21f71 100644
--- a/packages/flutter/test/material/switch_list_tile_test.dart
+++ b/packages/flutter/test/material/switch_list_tile_test.dart
@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-// @dart = 2.8
-
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
@@ -12,7 +10,7 @@
import '../widgets/semantics_tester.dart';
-Widget wrap({ Widget child }) {
+Widget wrap({ required Widget child }) {
return MediaQuery(
data: const MediaQueryData(),
child: Directionality(
@@ -51,14 +49,14 @@
),
CheckboxListTile(
value: true,
- onChanged: (bool value) { },
+ onChanged: (bool? value) { },
title: const Text('BBB'),
secondary: const Text('bbb'),
),
RadioListTile<bool>(
value: true,
groupValue: false,
- onChanged: (bool value) { },
+ onChanged: (bool? value) { },
title: const Text('CCC'),
secondary: const Text('ccc'),
),
@@ -278,7 +276,7 @@
);
await tester.pump();
- expect(Focus.of(childKey.currentContext, nullOk: true).hasPrimaryFocus, isTrue);
+ expect(Focus.of(childKey.currentContext!)!.hasPrimaryFocus, isTrue);
await tester.pumpWidget(
MaterialApp(
@@ -298,7 +296,7 @@
);
await tester.pump();
- expect(Focus.of(childKey.currentContext, nullOk: true).hasPrimaryFocus, isFalse);
+ expect(Focus.of(childKey.currentContext!)!.hasPrimaryFocus, isFalse);
});
testWidgets('SwitchListTile controlAffinity test', (WidgetTester tester) async {
diff --git a/packages/flutter/test/material/switch_test.dart b/packages/flutter/test/material/switch_test.dart
index 528c30c..b2c501e 100644
--- a/packages/flutter/test/material/switch_test.dart
+++ b/packages/flutter/test/material/switch_test.dart
@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-// @dart = 2.8
-
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
@@ -318,7 +316,7 @@
Material.of(tester.element(find.byType(Switch))),
paints
..rrect(
- color: Colors.blue[600].withAlpha(0x80),
+ color: Colors.blue[600]!.withAlpha(0x80),
rrect: RRect.fromLTRBR(
383.5, 293.0, 416.5, 307.0, const Radius.circular(7.0)))
..circle(color: const Color(0x33000000))
@@ -608,10 +606,10 @@
expect(value, true);
expect(semanticEvent, <String, dynamic>{
'type': 'tap',
- 'nodeId': object.debugSemantics.id,
+ 'nodeId': object.debugSemantics!.id,
'data': <String, dynamic>{},
});
- expect(object.debugSemantics.getSemanticsData().hasAction(SemanticsAction.tap), true);
+ expect(object.debugSemantics!.getSemanticsData().hasAction(SemanticsAction.tap), true);
semanticsTester.dispose();
SystemChannels.accessibility.setMockMessageHandler(null);
@@ -658,10 +656,10 @@
expect(value, true);
expect(semanticEvent, <String, dynamic>{
'type': 'tap',
- 'nodeId': object.debugSemantics.id,
+ 'nodeId': object.debugSemantics!.id,
'data': <String, dynamic>{},
});
- expect(object.debugSemantics.getSemanticsData().hasAction(SemanticsAction.tap), true);
+ expect(object.debugSemantics!.getSemanticsData().hasAction(SemanticsAction.tap), true);
semanticsTester.dispose();
SystemChannels.accessibility.setMockMessageHandler(null);
@@ -943,7 +941,7 @@
await tester.pump();
- expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.text);
+ expect(RendererBinding.instance!.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.text);
// Test Switch() constructor
await tester.pumpWidget(
@@ -967,7 +965,7 @@
);
await gesture.moveTo(tester.getCenter(find.byType(Switch)));
- expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.text);
+ expect(RendererBinding.instance!.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.text);
// Test default cursor
await tester.pumpWidget(
@@ -989,7 +987,7 @@
),
);
- expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.click);
+ expect(RendererBinding.instance!.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.click);
// Test default cursor when disabled
await tester.pumpWidget(
@@ -1011,7 +1009,7 @@
),
);
- expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.basic);
+ expect(RendererBinding.instance!.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.basic);
await tester.pumpAndSettle();
});
@@ -1020,7 +1018,7 @@
// Regression test for https://github.com/flutter/flutter/issues/61247.
bool value = true;
bool enabled = true;
- StateSetter stateSetter;
+ late StateSetter stateSetter;
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,