From dc6a15995e3c4f17316533045e2233b18941d82c Mon Sep 17 00:00:00 2001 From: hangyu Date: Tue, 30 Jul 2024 13:38:06 -0700 Subject: [PATCH] Reland #151599 (Add button semantics in list tile ) with a flag to control behavior. (#152526) https://github.com/flutter/flutter/pull/151599 was reverted because it was a breaking change to g3 Will reland 151599 in 5 steps 1. Make changes with an additional parameter ( bool internalAddSemanticForOnTap = false) 2. Send regular CLs in google3 to pass internalAddSemanticForOnTap: true, and update the tests / text goldens accordingly. 3. Send a PR to flip the default value to true. 4. Send CLs internally to remove internalAddSemanticForOnTap: true. 5. Send another PR to remove the now-redundant internalAddSemanticForOnTap flag. (<----This PR is step 1) --- .../flutter/lib/src/material/checkbox_list_tile.dart | 10 ++++++++++ packages/flutter/lib/src/material/expansion_tile.dart | 9 +++++++++ packages/flutter/lib/src/material/list_tile.dart | 9 +++++++++ packages/flutter/lib/src/material/radio_list_tile.dart | 10 ++++++++++ .../flutter/lib/src/material/switch_list_tile.dart | 9 +++++++++ .../flutter/test/material/checkbox_list_tile_test.dart | 2 ++ .../flutter/test/material/expansion_tile_test.dart | 4 ++++ packages/flutter/test/material/list_tile_test.dart | 5 +++++ .../flutter/test/material/radio_list_tile_test.dart | 6 ++++++ .../flutter/test/material/reorderable_list_test.dart | 2 ++ .../flutter/test/material/switch_list_tile_test.dart | 6 ++++++ 11 files changed, 72 insertions(+) diff --git a/packages/flutter/lib/src/material/checkbox_list_tile.dart b/packages/flutter/lib/src/material/checkbox_list_tile.dart index 701297df48..0c3718fb7f 100644 --- a/packages/flutter/lib/src/material/checkbox_list_tile.dart +++ b/packages/flutter/lib/src/material/checkbox_list_tile.dart @@ -205,6 +205,7 @@ class CheckboxListTile extends StatelessWidget { this.onFocusChange, this.enableFeedback, this.checkboxSemanticLabel, + this.internalAddSemanticForOnTap = false, }) : _checkboxType = _CheckboxType.material, assert(tristate || value != null), assert(!isThreeLine || subtitle != null); @@ -249,6 +250,7 @@ class CheckboxListTile extends StatelessWidget { this.onFocusChange, this.enableFeedback, this.checkboxSemanticLabel, + this.internalAddSemanticForOnTap = false, }) : _checkboxType = _CheckboxType.adaptive, assert(tristate || value != null), assert(!isThreeLine || subtitle != null); @@ -464,6 +466,13 @@ class CheckboxListTile extends StatelessWidget { /// inoperative. final bool? enabled; + /// Whether to add button:true to the semantics if onTap is provided. + /// This is a temporary flag to help changing the behavior of ListTile onTap semantics. + /// + // TODO(hangyujin): Remove this flag after fixing related g3 tests and flipping + // the default value to true. + final bool internalAddSemanticForOnTap; + /// {@macro flutter.material.checkbox.semanticLabel} final String? checkboxSemanticLabel; @@ -567,6 +576,7 @@ class CheckboxListTile extends StatelessWidget { focusNode: focusNode, onFocusChange: onFocusChange, enableFeedback: enableFeedback, + internalAddSemanticForOnTap: internalAddSemanticForOnTap, ), ); } diff --git a/packages/flutter/lib/src/material/expansion_tile.dart b/packages/flutter/lib/src/material/expansion_tile.dart index cb394d49cb..c1c809f1eb 100644 --- a/packages/flutter/lib/src/material/expansion_tile.dart +++ b/packages/flutter/lib/src/material/expansion_tile.dart @@ -263,6 +263,7 @@ class ExpansionTile extends StatefulWidget { this.enableFeedback = true, this.enabled = true, this.expansionAnimationStyle, + this.internalAddSemanticForOnTap = false, }) : assert( expandedCrossAxisAlignment != CrossAxisAlignment.baseline, 'CrossAxisAlignment.baseline is not supported since the expanded children ' @@ -562,6 +563,13 @@ class ExpansionTile extends StatefulWidget { /// {@end-tool} final AnimationStyle? expansionAnimationStyle; + /// Whether to add button:true to the semantics if onTap is provided. + /// This is a temporary flag to help changing the behavior of ListTile onTap semantics. + /// + // TODO(hangyujin): Remove this flag after fixing related g3 tests and flipping + // the default value to true. + final bool internalAddSemanticForOnTap; + @override State createState() => _ExpansionTileState(); } @@ -753,6 +761,7 @@ class _ExpansionTileState extends State with SingleTickerProvider subtitle: widget.subtitle, trailing: widget.showTrailingIcon ? widget.trailing ?? _buildTrailingIcon(context) : null, minTileHeight: widget.minTileHeight, + internalAddSemanticForOnTap: widget.internalAddSemanticForOnTap, ), ), ), diff --git a/packages/flutter/lib/src/material/list_tile.dart b/packages/flutter/lib/src/material/list_tile.dart index 19af6616fd..db6eefe038 100644 --- a/packages/flutter/lib/src/material/list_tile.dart +++ b/packages/flutter/lib/src/material/list_tile.dart @@ -410,6 +410,7 @@ class ListTile extends StatelessWidget { this.minLeadingWidth, this.minTileHeight, this.titleAlignment, + this.internalAddSemanticForOnTap = false, }) : assert(!isThreeLine || subtitle != null); /// A widget to display before the title. @@ -738,6 +739,13 @@ class ListTile extends StatelessWidget { /// [ListTileThemeData]. final ListTileTitleAlignment? titleAlignment; + /// Whether to add button:true to the semantics if onTap is provided. + /// This is a temporary flag to help changing the behavior of ListTile onTap semantics. + /// + // TODO(hangyujin): Remove this flag after fixing related g3 tests and flipping + // the default value to true. + final bool internalAddSemanticForOnTap; + /// Add a one pixel border in between each tile. If color isn't specified the /// [ThemeData.dividerColor] of the context's [Theme] is used. /// @@ -910,6 +918,7 @@ class ListTile extends StatelessWidget { autofocus: autofocus, enableFeedback: enableFeedback ?? tileTheme.enableFeedback ?? true, child: Semantics( + button: internalAddSemanticForOnTap && (onTap != null || onLongPress != null), selected: selected, enabled: enabled, child: Ink( diff --git a/packages/flutter/lib/src/material/radio_list_tile.dart b/packages/flutter/lib/src/material/radio_list_tile.dart index b0d914f1e5..2b8a554164 100644 --- a/packages/flutter/lib/src/material/radio_list_tile.dart +++ b/packages/flutter/lib/src/material/radio_list_tile.dart @@ -200,6 +200,7 @@ class RadioListTile extends StatelessWidget { this.focusNode, this.onFocusChange, this.enableFeedback, + this.internalAddSemanticForOnTap = false, }) : _radioType = _RadioType.material, useCupertinoCheckmarkStyle = false, assert(!isThreeLine || subtitle != null); @@ -240,6 +241,7 @@ class RadioListTile extends StatelessWidget { this.onFocusChange, this.enableFeedback, this.useCupertinoCheckmarkStyle = false, + this.internalAddSemanticForOnTap = false, }) : _radioType = _RadioType.adaptive, assert(!isThreeLine || subtitle != null); @@ -449,6 +451,13 @@ class RadioListTile extends StatelessWidget { final _RadioType _radioType; + /// Whether to add button:true to the semantics if onTap is provided. + /// This is a temporary flag to help changing the behavior of ListTile onTap semantics. + /// + // TODO(hangyujin): Remove this flag after fixing related g3 tests and flipping + // the default value to true. + final bool internalAddSemanticForOnTap; + /// Whether to use the checkbox style for the [CupertinoRadio] control. /// /// Only usable under the [RadioListTile.adaptive] constructor. If set to @@ -546,6 +555,7 @@ class RadioListTile extends StatelessWidget { focusNode: focusNode, onFocusChange: onFocusChange, enableFeedback: enableFeedback, + internalAddSemanticForOnTap: internalAddSemanticForOnTap, ), ); } diff --git a/packages/flutter/lib/src/material/switch_list_tile.dart b/packages/flutter/lib/src/material/switch_list_tile.dart index 79a8797916..c8fe88da33 100644 --- a/packages/flutter/lib/src/material/switch_list_tile.dart +++ b/packages/flutter/lib/src/material/switch_list_tile.dart @@ -209,6 +209,7 @@ class SwitchListTile extends StatelessWidget { this.visualDensity, this.enableFeedback, this.hoverColor, + this.internalAddSemanticForOnTap = false, }) : _switchListTileType = _SwitchListTileType.material, applyCupertinoTheme = false, assert(activeThumbImage != null || onActiveThumbImageError == null), @@ -266,6 +267,7 @@ class SwitchListTile extends StatelessWidget { this.visualDensity, this.enableFeedback, this.hoverColor, + this.internalAddSemanticForOnTap = false, }) : _switchListTileType = _SwitchListTileType.adaptive, assert(!isThreeLine || subtitle != null), assert(activeThumbImage != null || onActiveThumbImageError == null), @@ -517,6 +519,12 @@ class SwitchListTile extends StatelessWidget { /// {@macro flutter.cupertino.CupertinoSwitch.applyTheme} final bool? applyCupertinoTheme; + /// Whether to add button:true to the semantics if onTap is provided. + /// This is a temporary flag to help changing the behavior of ListTile onTap semantics. + /// + // TODO(hangyujin): Remove this flag after fixing related g3 tests and flipping + // the default value to true. + final bool internalAddSemanticForOnTap; @override Widget build(BuildContext context) { final Widget control; @@ -616,6 +624,7 @@ class SwitchListTile extends StatelessWidget { onFocusChange: onFocusChange, enableFeedback: enableFeedback, hoverColor: hoverColor, + internalAddSemanticForOnTap: internalAddSemanticForOnTap, ), ); } diff --git a/packages/flutter/test/material/checkbox_list_tile_test.dart b/packages/flutter/test/material/checkbox_list_tile_test.dart index 3f897b934b..0b96de6162 100644 --- a/packages/flutter/test/material/checkbox_list_tile_test.dart +++ b/packages/flutter/test/material/checkbox_list_tile_test.dart @@ -1180,10 +1180,12 @@ void main() { onChanged: (bool? value) { log.add(value); }, title: const Text('Hello'), checkboxSemanticLabel: 'there', + internalAddSemanticForOnTap: true, ), )); expect(tester.getSemantics(find.byType(CheckboxListTile)), matchesSemantics( + isButton: true, hasCheckedState: true, isChecked: true, hasEnabledState: true, diff --git a/packages/flutter/test/material/expansion_tile_test.dart b/packages/flutter/test/material/expansion_tile_test.dart index 78c4d96833..fa3e9ab98e 100644 --- a/packages/flutter/test/material/expansion_tile_test.dart +++ b/packages/flutter/test/material/expansion_tile_test.dart @@ -707,10 +707,12 @@ void main() { children: [ ExpansionTile( title: Text('First Expansion Tile'), + internalAddSemanticForOnTap: true, ), ExpansionTile( initiallyExpanded: true, title: Text('Second Expansion Tile'), + internalAddSemanticForOnTap: true, ), ], ), @@ -727,6 +729,7 @@ void main() { expect( tester.getSemantics(find.byType(ListTile).first), matchesSemantics( + isButton: true, hasTapAction: true, hasFocusAction: true, hasEnabledState: true, @@ -742,6 +745,7 @@ void main() { expect( tester.getSemantics(find.byType(ListTile).last), matchesSemantics( + isButton: true, hasTapAction: true, hasFocusAction: true, hasEnabledState: true, diff --git a/packages/flutter/test/material/list_tile_test.dart b/packages/flutter/test/material/list_tile_test.dart index 97d8ea6abe..cf35621e0e 100644 --- a/packages/flutter/test/material/list_tile_test.dart +++ b/packages/flutter/test/material/list_tile_test.dart @@ -270,18 +270,22 @@ void main() { children: [ const ListTile( title: Text('one'), + internalAddSemanticForOnTap: true, ), ListTile( title: const Text('two'), onTap: () {}, + internalAddSemanticForOnTap: true, ), const ListTile( title: Text('three'), selected: true, + internalAddSemanticForOnTap: true, ), const ListTile( title: Text('four'), enabled: false, + internalAddSemanticForOnTap: true, ), ], ), @@ -304,6 +308,7 @@ void main() { ), TestSemantics.rootChild( flags: [ + SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled, SemanticsFlag.isFocusable, diff --git a/packages/flutter/test/material/radio_list_tile_test.dart b/packages/flutter/test/material/radio_list_tile_test.dart index b2a4a40ab5..26179199fd 100644 --- a/packages/flutter/test/material/radio_list_tile_test.dart +++ b/packages/flutter/test/material/radio_list_tile_test.dart @@ -393,6 +393,7 @@ void main() { groupValue: 2, onChanged: (int? i) {}, title: const Text('Title'), + internalAddSemanticForOnTap: true, ), ), ); @@ -405,6 +406,7 @@ void main() { TestSemantics( id: 1, flags: [ + SemanticsFlag.isButton, SemanticsFlag.hasCheckedState, SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled, @@ -429,6 +431,7 @@ void main() { groupValue: 2, onChanged: (int? i) {}, title: const Text('Title'), + internalAddSemanticForOnTap: true, ), ), ); @@ -441,6 +444,7 @@ void main() { TestSemantics( id: 1, flags: [ + SemanticsFlag.isButton, SemanticsFlag.hasCheckedState, SemanticsFlag.isChecked, SemanticsFlag.hasEnabledState, @@ -466,6 +470,7 @@ void main() { groupValue: 2, onChanged: null, title: Text('Title'), + internalAddSemanticForOnTap: true, ), ), ); @@ -502,6 +507,7 @@ void main() { groupValue: 2, onChanged: null, title: Text('Title'), + internalAddSemanticForOnTap: true, ), ), ); diff --git a/packages/flutter/test/material/reorderable_list_test.dart b/packages/flutter/test/material/reorderable_list_test.dart index fcd36d7d5c..a15264e9bf 100644 --- a/packages/flutter/test/material/reorderable_list_test.dart +++ b/packages/flutter/test/material/reorderable_list_test.dart @@ -707,6 +707,7 @@ void main() { title: const Text('Switch tile'), value: true, onChanged: (bool? newValue) { }, + internalAddSemanticForOnTap: true, ), ), ), @@ -744,6 +745,7 @@ void main() { return false; }); expect(child, matchesSemantics( + isButton: true, hasToggledState: true, isToggled: true, isEnabled: true, diff --git a/packages/flutter/test/material/switch_list_tile_test.dart b/packages/flutter/test/material/switch_list_tile_test.dart index 5d5c8b43e9..985270cb6d 100644 --- a/packages/flutter/test/material/switch_list_tile_test.dart +++ b/packages/flutter/test/material/switch_list_tile_test.dart @@ -47,12 +47,14 @@ void main() { onChanged: (bool value) { }, title: const Text('AAA'), secondary: const Text('aaa'), + internalAddSemanticForOnTap: true, ), CheckboxListTile( value: true, onChanged: (bool? value) { }, title: const Text('BBB'), secondary: const Text('bbb'), + internalAddSemanticForOnTap: true, ), RadioListTile( value: true, @@ -60,6 +62,7 @@ void main() { onChanged: (bool? value) { }, title: const Text('CCC'), secondary: const Text('ccc'), + internalAddSemanticForOnTap: true, ), ], ), @@ -72,6 +75,7 @@ void main() { id: 1, rect: const Rect.fromLTWH(0.0, 0.0, 800.0, 56.0), flags: [ + SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, SemanticsFlag.hasToggledState, SemanticsFlag.isEnabled, @@ -86,6 +90,7 @@ void main() { rect: const Rect.fromLTWH(0.0, 0.0, 800.0, 56.0), transform: Matrix4.translationValues(0.0, 56.0, 0.0), flags: [ + SemanticsFlag.isButton, SemanticsFlag.hasCheckedState, SemanticsFlag.hasEnabledState, SemanticsFlag.isChecked, @@ -100,6 +105,7 @@ void main() { rect: const Rect.fromLTWH(0.0, 0.0, 800.0, 56.0), transform: Matrix4.translationValues(0.0, 112.0, 0.0), flags: [ + SemanticsFlag.isButton, SemanticsFlag.hasCheckedState, SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled,