Migrate Switch tests to null safety. (#68133)

This commit is contained in:
Darren Austin 2020-10-14 15:32:04 -07:00 committed by GitHub
parent f2d5f1f61f
commit 2c59a3a760
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 25 deletions

View file

@ -149,7 +149,7 @@ class Switch extends StatefulWidget {
/// },
/// )
/// ```
final ValueChanged<bool?>? onChanged;
final ValueChanged<bool>? onChanged;
/// The color to use when this switch is on.
///
@ -440,7 +440,7 @@ class _SwitchRenderObjectWidget extends LeafRenderObjectWidget {
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 @@ class _SwitchRenderObjectWidget extends LeafRenderObjectWidget {
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 @@ class _SwitchRenderObjectWidget extends LeafRenderObjectWidget {
..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 @@ class _SwitchRenderObjectWidget extends LeafRenderObjectWidget {
..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 @@ class _RenderSwitch extends RenderToggleable {
}
}
@override
void detach() {
_cachedThumbPainter?.dispose();

View file

@ -346,7 +346,7 @@ class SwitchListTile extends StatelessWidget {
/// title: Text('Selection'),
/// )
/// ```
final ValueChanged<bool?>? onChanged;
final ValueChanged<bool>? onChanged;
/// The color to use when this switch is on.
///

View file

@ -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 '../rendering/mock_canvas.dart';
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 @@ void main() {
),
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 @@ void main() {
);
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 @@ void main() {
);
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 {

View file

@ -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 @@ void main() {
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 @@ void main() {
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 @@ void main() {
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 @@ void main() {
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 @@ void main() {
);
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 @@ void main() {
),
);
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 @@ void main() {
),
);
expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.basic);
expect(RendererBinding.instance!.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.basic);
await tester.pumpAndSettle();
});
@ -1020,7 +1018,7 @@ void main() {
// 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,