diff --git a/packages/flutter/lib/src/material/switch.dart b/packages/flutter/lib/src/material/switch.dart index 660d877c3d..6efe081d6f 100644 --- a/packages/flutter/lib/src/material/switch.dart +++ b/packages/flutter/lib/src/material/switch.dart @@ -149,7 +149,7 @@ class Switch extends StatefulWidget { /// }, /// ) /// ``` - final ValueChanged? onChanged; + final ValueChanged? 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? onChanged; + final ValueChanged? 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(); diff --git a/packages/flutter/lib/src/material/switch_list_tile.dart b/packages/flutter/lib/src/material/switch_list_tile.dart index 567b4274d6..a34dd00fb0 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 @@ class SwitchListTile extends StatelessWidget { /// title: Text('Selection'), /// ) /// ``` - final ValueChanged? onChanged; + final ValueChanged? 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 053173b4e4..be21f71204 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 '../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( 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 { diff --git a/packages/flutter/test/material/switch_test.dart b/packages/flutter/test/material/switch_test.dart index 528c30cc2f..b2c501ed40 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 @@ 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, { 'type': 'tap', - 'nodeId': object.debugSemantics.id, + 'nodeId': object.debugSemantics!.id, 'data': {}, }); - 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, { 'type': 'tap', - 'nodeId': object.debugSemantics.id, + 'nodeId': object.debugSemantics!.id, 'data': {}, }); - 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,