From b9e3aef2390b9b755c3c3cf95f7a418f9d6ada6b Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Fri, 12 May 2023 20:12:20 +0300 Subject: [PATCH] Add `FocusNode`, `onFocusChange`, and `autofocus` to `CupertinoSwitch` (#126684) fixes https://github.com/flutter/flutter/issues/126679 ### Description Control cupertino widgets like `CupertinoCheckbox` and `CupertinoRadio` have `focusNode` and `autofocus`, while these are missing in `CupertinoSwitch`. This is blocking https://github.com/flutter/flutter/issues/126637 (`switch.dart` currently maintains its focus node for `CupertinoSwitch` when using `Switch.adaptive` which causes a bug) --- .../flutter/lib/src/cupertino/switch.dart | 15 +++ .../flutter/test/cupertino/switch_test.dart | 111 ++++++++++++++++++ 2 files changed, 126 insertions(+) diff --git a/packages/flutter/lib/src/cupertino/switch.dart b/packages/flutter/lib/src/cupertino/switch.dart index 992b885095..70c1702b7f 100644 --- a/packages/flutter/lib/src/cupertino/switch.dart +++ b/packages/flutter/lib/src/cupertino/switch.dart @@ -75,6 +75,9 @@ class CupertinoSwitch extends StatefulWidget { this.thumbColor, this.applyTheme, this.focusColor, + this.focusNode, + this.onFocusChange, + this.autofocus = false, this.dragStartBehavior = DragStartBehavior.start, }); @@ -130,6 +133,15 @@ class CupertinoSwitch extends StatefulWidget { /// Defaults to a slightly transparent [activeColor]. final Color? focusColor; + /// {@macro flutter.widgets.Focus.focusNode} + final FocusNode? focusNode; + + /// {@macro flutter.material.inkwell.onFocusChange} + final ValueChanged? onFocusChange; + + /// {@macro flutter.widgets.Focus.autofocus} + final bool autofocus; + /// {@template flutter.cupertino.CupertinoSwitch.applyTheme} /// Whether to apply the ambient [CupertinoThemeData]. /// @@ -356,6 +368,9 @@ class _CupertinoSwitchState extends State with TickerProviderSt onShowFocusHighlight: _onShowFocusHighlight, actions: _actionMap, enabled: isInteractive, + focusNode: widget.focusNode, + onFocusChange: widget.onFocusChange, + autofocus: widget.autofocus, child: _CupertinoSwitchRenderObjectWidget( value: widget.value, activeColor: activeColor, diff --git a/packages/flutter/test/cupertino/switch_test.dart b/packages/flutter/test/cupertino/switch_test.dart index c32dc69c99..f91fd0af87 100644 --- a/packages/flutter/test/cupertino/switch_test.dart +++ b/packages/flutter/test/cupertino/switch_test.dart @@ -913,4 +913,115 @@ void main() { kIsWeb ? SystemMouseCursors.click : SystemMouseCursors.basic, ); }); + + testWidgets('CupertinoSwitch is focusable and has correct focus color', (WidgetTester tester) async { + final FocusNode focusNode = FocusNode(debugLabel: 'CupertinoSwitch'); + tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional; + bool value = true; + const Color focusColor = Color(0xffff0000); + + Widget buildApp({bool enabled = true}) { + return Directionality( + textDirection: TextDirection.ltr, + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return Center( + child: CupertinoSwitch( + value: value, + onChanged: enabled ? (bool newValue) { + setState(() { + value = newValue; + }); + } : null, + focusColor: focusColor, + focusNode: focusNode, + autofocus: true, + ), + ); + }, + ), + ); + } + + await tester.pumpWidget(buildApp()); + await tester.pumpAndSettle(); + + expect(focusNode.hasPrimaryFocus, isTrue); + expect( + find.byType(CupertinoSwitch), + paints + ..rrect(color: const Color(0xff34c759)) + ..rrect(color: focusColor) + ..clipRRect() + ..rrect(color: const Color(0x26000000)) + ..rrect(color: const Color(0x0f000000)) + ..rrect(color: const Color(0x0a000000)) + ..rrect(color: const Color(0xffffffff)), + ); + + // Check the false value. + value = false; + await tester.pumpWidget(buildApp()); + await tester.pumpAndSettle(); + + expect(focusNode.hasPrimaryFocus, isTrue); + expect( + find.byType(CupertinoSwitch), + paints + ..rrect(color: const Color(0x28787880)) + ..rrect(color: focusColor) + ..clipRRect() + ..rrect(color: const Color(0x26000000)) + ..rrect(color: const Color(0x0f000000)) + ..rrect(color: const Color(0x0a000000)) + ..rrect(color: const Color(0xffffffff)), + ); + + // Check what happens when disabled. + value = false; + await tester.pumpWidget(buildApp(enabled: false)); + await tester.pumpAndSettle(); + + expect(focusNode.hasPrimaryFocus, isFalse); + expect( + find.byType(CupertinoSwitch), + paints + ..rrect(color: const Color(0x28787880)) + ..clipRRect() + ..rrect(color: const Color(0x26000000)) + ..rrect(color: const Color(0x0f000000)) + ..rrect(color: const Color(0x0a000000)) + ..rrect(color: const Color(0xffffffff)), + ); + }); + + testWidgets('CupertinoSwitch.onFocusChange callback', (WidgetTester tester) async { + final FocusNode focusNode = FocusNode(debugLabel: 'CupertinoSwitch'); + bool focused = false; + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Center( + child: CupertinoSwitch( + value: true, + focusNode: focusNode, + onFocusChange: (bool value) { + focused = value; + }, + onChanged:(bool newValue) {}, + ), + ), + ), + ); + + focusNode.requestFocus(); + await tester.pump(); + expect(focused, isTrue); + expect(focusNode.hasFocus, isTrue); + + focusNode.unfocus(); + await tester.pump(); + expect(focused, isFalse); + expect(focusNode.hasFocus, isFalse); + }); }