From 36c51cb6d773790d193c474aa19e13316b4cb089 Mon Sep 17 00:00:00 2001 From: Yash Johri Date: Wed, 8 Sep 2021 22:13:22 +0530 Subject: [PATCH] [CheckboxListTile, SwitchListTile, RadioListTile] Adds visualDensity, focusNode and enableFeedback (#89353) This PR adds visualDensity, focusNode and enableFeedback to: - CheckboxListTile - SwitchListTile - RadioListTile --- .../lib/src/material/checkbox_list_tile.dart | 21 +++++ .../flutter/lib/src/material/list_tile.dart | 4 + .../lib/src/material/radio_list_tile.dart | 21 +++++ .../lib/src/material/switch_list_tile.dart | 24 +++++ .../material/checkbox_list_tile_test.dart | 87 +++++++++++++++++ .../test/material/radio_list_tile_test.dart | 93 +++++++++++++++++++ .../test/material/switch_list_tile_test.dart | 87 +++++++++++++++++ 7 files changed, 337 insertions(+) diff --git a/packages/flutter/lib/src/material/checkbox_list_tile.dart b/packages/flutter/lib/src/material/checkbox_list_tile.dart index 07fa6080c8..ed7939f867 100644 --- a/packages/flutter/lib/src/material/checkbox_list_tile.dart +++ b/packages/flutter/lib/src/material/checkbox_list_tile.dart @@ -138,6 +138,9 @@ class CheckboxListTile extends StatelessWidget { this.tristate = false, this.shape, this.selectedTileColor, + this.visualDensity, + this.focusNode, + this.enableFeedback, }) : assert(tristate != null), assert(tristate || value != null), assert(isThreeLine != null), @@ -255,6 +258,21 @@ class CheckboxListTile extends StatelessWidget { /// If non-null, defines the background color when [CheckboxListTile.selected] is true. final Color? selectedTileColor; + /// Defines how compact the list tile's layout will be. + /// + /// {@macro flutter.material.themedata.visualDensity} + final VisualDensity? visualDensity; + + /// {@macro flutter.widgets.Focus.focusNode} + final FocusNode? focusNode; + + /// {@macro flutter.material.ListTile.enableFeedback} + /// + /// See also: + /// + /// * [Feedback] for providing platform-specific feedback to certain actions. + final bool? enableFeedback; + void _handleValueChange() { assert(onChanged != null); switch (value) { @@ -311,6 +329,9 @@ class CheckboxListTile extends StatelessWidget { shape: shape, selectedTileColor: selectedTileColor, tileColor: tileColor, + visualDensity: visualDensity, + focusNode: focusNode, + enableFeedback: enableFeedback, ), ), ); diff --git a/packages/flutter/lib/src/material/list_tile.dart b/packages/flutter/lib/src/material/list_tile.dart index d72d2118c8..83740607fb 100644 --- a/packages/flutter/lib/src/material/list_tile.dart +++ b/packages/flutter/lib/src/material/list_tile.dart @@ -671,11 +671,15 @@ class ListTile extends StatelessWidget { /// if it's not null and to [Colors.transparent] if it's null. final Color? selectedTileColor; + /// {@template flutter.material.ListTile.enableFeedback} /// Whether detected gestures should provide acoustic and/or haptic feedback. /// /// For example, on Android a tap will produce a clicking sound and a /// long-press will produce a short vibration, when feedback is enabled. /// + /// When null, the default value is true. + /// {@endtemplate} + /// /// See also: /// /// * [Feedback] for providing platform-specific feedback to certain actions. diff --git a/packages/flutter/lib/src/material/radio_list_tile.dart b/packages/flutter/lib/src/material/radio_list_tile.dart index 088ddb0d1f..32d233ce55 100644 --- a/packages/flutter/lib/src/material/radio_list_tile.dart +++ b/packages/flutter/lib/src/material/radio_list_tile.dart @@ -134,6 +134,9 @@ class RadioListTile extends StatelessWidget { this.shape, this.tileColor, this.selectedTileColor, + this.visualDensity, + this.focusNode, + this.enableFeedback, }) : assert(toggleable != null), assert(isThreeLine != null), assert(!isThreeLine || subtitle != null), @@ -275,6 +278,21 @@ class RadioListTile extends StatelessWidget { /// If non-null, defines the background color when [RadioListTile.selected] is true. final Color? selectedTileColor; + /// Defines how compact the list tile's layout will be. + /// + /// {@macro flutter.material.themedata.visualDensity} + final VisualDensity? visualDensity; + + /// {@macro flutter.widgets.Focus.focusNode} + final FocusNode? focusNode; + + /// {@macro flutter.material.ListTile.enableFeedback} + /// + /// See also: + /// + /// * [Feedback] for providing platform-specific feedback to certain actions. + final bool? enableFeedback; + @override Widget build(BuildContext context) { final Widget control = Radio( @@ -324,6 +342,9 @@ class RadioListTile extends StatelessWidget { selected: selected, autofocus: autofocus, contentPadding: contentPadding, + visualDensity: visualDensity, + focusNode: focusNode, + enableFeedback: enableFeedback, ), ), ); diff --git a/packages/flutter/lib/src/material/switch_list_tile.dart b/packages/flutter/lib/src/material/switch_list_tile.dart index c77e8fa9d4..60800f60ea 100644 --- a/packages/flutter/lib/src/material/switch_list_tile.dart +++ b/packages/flutter/lib/src/material/switch_list_tile.dart @@ -143,6 +143,9 @@ class SwitchListTile extends StatelessWidget { this.controlAffinity = ListTileControlAffinity.platform, this.shape, this.selectedTileColor, + this.visualDensity, + this.focusNode, + this.enableFeedback, }) : _switchListTileType = _SwitchListTileType.material, assert(value != null), assert(isThreeLine != null), @@ -185,6 +188,9 @@ class SwitchListTile extends StatelessWidget { this.controlAffinity = ListTileControlAffinity.platform, this.shape, this.selectedTileColor, + this.visualDensity, + this.focusNode, + this.enableFeedback, }) : _switchListTileType = _SwitchListTileType.adaptive, assert(value != null), assert(isThreeLine != null), @@ -321,6 +327,21 @@ class SwitchListTile extends StatelessWidget { /// If non-null, defines the background color when [SwitchListTile.selected] is true. final Color? selectedTileColor; + /// Defines how compact the list tile's layout will be. + /// + /// {@macro flutter.material.themedata.visualDensity} + final VisualDensity? visualDensity; + + /// {@macro flutter.widgets.Focus.focusNode} + final FocusNode? focusNode; + + /// {@macro flutter.material.ListTile.enableFeedback} + /// + /// See also: + /// + /// * [Feedback] for providing platform-specific feedback to certain actions. + final bool? enableFeedback; + @override Widget build(BuildContext context) { final Widget control; @@ -386,6 +407,9 @@ class SwitchListTile extends StatelessWidget { autofocus: autofocus, shape: shape, tileColor: tileColor, + visualDensity: visualDensity, + focusNode: focusNode, + enableFeedback: enableFeedback, ), ), ); diff --git a/packages/flutter/test/material/checkbox_list_tile_test.dart b/packages/flutter/test/material/checkbox_list_tile_test.dart index b36145c4ba..6004fd8cc2 100644 --- a/packages/flutter/test/material/checkbox_list_tile_test.dart +++ b/packages/flutter/test/material/checkbox_list_tile_test.dart @@ -7,6 +7,7 @@ import 'package:flutter/rendering.dart'; import 'package:flutter_test/flutter_test.dart'; import '../rendering/mock_canvas.dart'; +import 'feedback_tester.dart'; Widget wrap({ required Widget child }) { return MediaQuery( @@ -319,4 +320,90 @@ void main() { await tester.pumpWidget(buildFrame(activeColor: activeColor)); expect(textColor('title'), activeColor); }); + + testWidgets('CheckboxListTile respects visualDensity', (WidgetTester tester) async { + const Key key = Key('test'); + Future buildTest(VisualDensity visualDensity) async { + return tester.pumpWidget( + wrap( + child: Center( + child: CheckboxListTile( + key: key, + value: false, + onChanged: (bool? value) {}, + autofocus: true, + visualDensity: visualDensity, + ), + ), + ), + ); + } + + await buildTest(VisualDensity.standard); + final RenderBox box = tester.renderObject(find.byKey(key)); + await tester.pumpAndSettle(); + expect(box.size, equals(const Size(800, 56))); + }); + + testWidgets('CheckboxListTile respects focusNode', (WidgetTester tester) async { + final GlobalKey childKey = GlobalKey(); + await tester.pumpWidget( + wrap( + child: Center( + child: CheckboxListTile( + value: false, + title: Text('A', key: childKey), + onChanged: (bool? value) {}, + ), + ), + ), + ); + + await tester.pump(); + final FocusNode tileNode = Focus.of(childKey.currentContext!); + tileNode.requestFocus(); + await tester.pump(); // Let the focus take effect. + expect(Focus.of(childKey.currentContext!).hasPrimaryFocus, isTrue); + expect(tileNode.hasPrimaryFocus, isTrue); + }); + + group('feedback', () { + late FeedbackTester feedback; + + setUp(() { + feedback = FeedbackTester(); + }); + + tearDown(() { + feedback.dispose(); + }); + + testWidgets('CheckboxListTile respects enableFeedback', (WidgetTester tester) async { + Future buildTest(bool enableFeedback) async { + return tester.pumpWidget( + wrap( + child: Center( + child: CheckboxListTile( + value: false, + onChanged: (bool? value) {}, + enableFeedback: enableFeedback, + ), + ), + ), + ); + } + + await buildTest(false); + await tester.tap(find.byType(CheckboxListTile)); + await tester.pump(const Duration(seconds: 1)); + expect(feedback.clickSoundCount, 0); + expect(feedback.hapticCount, 0); + + await buildTest(true); + await tester.tap(find.byType(CheckboxListTile)); + await tester.pump(const Duration(seconds: 1)); + expect(feedback.clickSoundCount, 1); + expect(feedback.hapticCount, 0); + }); + }); } diff --git a/packages/flutter/test/material/radio_list_tile_test.dart b/packages/flutter/test/material/radio_list_tile_test.dart index 332ee8f044..53673f4d75 100644 --- a/packages/flutter/test/material/radio_list_tile_test.dart +++ b/packages/flutter/test/material/radio_list_tile_test.dart @@ -11,6 +11,7 @@ import 'package:flutter_test/flutter_test.dart'; import '../rendering/mock_canvas.dart'; import '../widgets/semantics_tester.dart'; +import 'feedback_tester.dart'; Widget wrap({Widget? child}) { return MediaQuery( @@ -741,4 +742,96 @@ void main() { await tester.pumpWidget(buildFrame(activeColor: activeColor)); expect(textColor('title'), activeColor); }); + + testWidgets('RadioListTile respects visualDensity', (WidgetTester tester) async { + const Key key = Key('test'); + Future buildTest(VisualDensity visualDensity) async { + return tester.pumpWidget( + wrap( + child: Center( + child: RadioListTile( + key: key, + value: false, + groupValue: true, + onChanged: (bool? value) {}, + autofocus: true, + visualDensity: visualDensity, + ), + ), + ), + ); + } + + await buildTest(VisualDensity.standard); + final RenderBox box = tester.renderObject(find.byKey(key)); + await tester.pumpAndSettle(); + expect(box.size, equals(const Size(800, 56))); + }); + + testWidgets('RadioListTile respects focusNode', (WidgetTester tester) async { + final GlobalKey childKey = GlobalKey(); + await tester.pumpWidget( + wrap( + child: Center( + child: RadioListTile( + value: false, + groupValue: true, + title: Text('A', key: childKey), + onChanged: (bool? value) {}, + ), + ), + ), + ); + + await tester.pump(); + final FocusNode tileNode = Focus.of(childKey.currentContext!); + tileNode.requestFocus(); + await tester.pump(); // Let the focus take effect. + expect(Focus.of(childKey.currentContext!).hasPrimaryFocus, isTrue); + expect(tileNode.hasPrimaryFocus, isTrue); + }); + + group('feedback', () { + late FeedbackTester feedback; + + setUp(() { + feedback = FeedbackTester(); + }); + + tearDown(() { + feedback.dispose(); + }); + + testWidgets('RadioListTile respects enableFeedback', (WidgetTester tester) async { + const Key key = Key('test'); + Future buildTest(bool enableFeedback) async { + return tester.pumpWidget( + wrap( + child: Center( + child: RadioListTile( + key: key, + value: false, + groupValue: true, + selected: true, + onChanged: (bool? value) {}, + enableFeedback: enableFeedback, + ), + ), + ), + ); + } + + await buildTest(false); + await tester.tap(find.byKey(key)); + await tester.pump(const Duration(seconds: 1)); + expect(feedback.clickSoundCount, 0); + expect(feedback.hapticCount, 0); + + await buildTest(true); + await tester.tap(find.byKey(key)); + await tester.pump(const Duration(seconds: 1)); + expect(feedback.clickSoundCount, 1); + expect(feedback.hapticCount, 0); + }); + }); } diff --git a/packages/flutter/test/material/switch_list_tile_test.dart b/packages/flutter/test/material/switch_list_tile_test.dart index 597f107e5d..84bec80fcb 100644 --- a/packages/flutter/test/material/switch_list_tile_test.dart +++ b/packages/flutter/test/material/switch_list_tile_test.dart @@ -10,6 +10,7 @@ import 'package:flutter_test/flutter_test.dart'; import '../rendering/mock_canvas.dart'; import '../widgets/semantics_tester.dart'; +import 'feedback_tester.dart'; Widget wrap({ required Widget child }) { return MediaQuery( @@ -432,4 +433,90 @@ void main() { await tester.pumpWidget(buildFrame(activeColor: activeColor)); expect(textColor('title'), activeColor); }); + + testWidgets('SwitchListTile respects visualDensity', (WidgetTester tester) async { + const Key key = Key('test'); + Future buildTest(VisualDensity visualDensity) async { + return tester.pumpWidget( + wrap( + child: Center( + child: SwitchListTile( + key: key, + value: false, + onChanged: (bool? value) {}, + autofocus: true, + visualDensity: visualDensity, + ), + ), + ), + ); + } + + await buildTest(VisualDensity.standard); + final RenderBox box = tester.renderObject(find.byKey(key)); + await tester.pumpAndSettle(); + expect(box.size, equals(const Size(800, 56))); + }); + + testWidgets('SwitchListTile respects focusNode', (WidgetTester tester) async { + final GlobalKey childKey = GlobalKey(); + await tester.pumpWidget( + wrap( + child: Center( + child: SwitchListTile( + value: false, + title: Text('A', key: childKey), + onChanged: (bool? value) {}, + ), + ), + ), + ); + + await tester.pump(); + final FocusNode tileNode = Focus.of(childKey.currentContext!); + tileNode.requestFocus(); + await tester.pump(); // Let the focus take effect. + expect(Focus.of(childKey.currentContext!).hasPrimaryFocus, isTrue); + expect(tileNode.hasPrimaryFocus, isTrue); + }); + + group('feedback', () { + late FeedbackTester feedback; + + setUp(() { + feedback = FeedbackTester(); + }); + + tearDown(() { + feedback.dispose(); + }); + + testWidgets('SwitchListTile respects enableFeedback', (WidgetTester tester) async { + Future buildTest(bool enableFeedback) async { + return tester.pumpWidget( + wrap( + child: Center( + child: SwitchListTile( + value: false, + onChanged: (bool? value) {}, + enableFeedback: enableFeedback, + ), + ), + ), + ); + } + + await buildTest(false); + await tester.tap(find.byType(SwitchListTile)); + await tester.pump(const Duration(seconds: 1)); + expect(feedback.clickSoundCount, 0); + expect(feedback.hapticCount, 0); + + await buildTest(true); + await tester.tap(find.byType(SwitchListTile)); + await tester.pump(const Duration(seconds: 1)); + expect(feedback.clickSoundCount, 1); + expect(feedback.hapticCount, 0); + }); + }); }