[go: nahoru, domu]

Skip to content

Commit

Permalink
Add onFocusChange property for ListTile widget (flutter#111498)
Browse files Browse the repository at this point in the history
  • Loading branch information
M97Chahboun authored and shogohida committed Dec 7, 2022
1 parent 326f593 commit 0e35610
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/flutter/lib/src/material/checkbox_list_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ class CheckboxListTile extends StatelessWidget {
this.side,
this.visualDensity,
this.focusNode,
this.onFocusChange,
this.enableFeedback,
}) : assert(tristate != null),
assert(tristate || value != null),
Expand Down Expand Up @@ -320,6 +321,9 @@ class CheckboxListTile extends StatelessWidget {
/// {@macro flutter.widgets.Focus.focusNode}
final FocusNode? focusNode;

/// {@macro flutter.material.inkwell.onFocusChange}
final ValueChanged<bool>? onFocusChange;

/// {@macro flutter.material.ListTile.enableFeedback}
///
/// See also:
Expand Down Expand Up @@ -401,6 +405,7 @@ class CheckboxListTile extends StatelessWidget {
tileColor: tileColor,
visualDensity: visualDensity,
focusNode: focusNode,
onFocusChange: onFocusChange,
enableFeedback: enableFeedback,
),
);
Expand Down
2 changes: 2 additions & 0 deletions packages/flutter/lib/src/material/ink_well.dart
Original file line number Diff line number Diff line change
Expand Up @@ -559,10 +559,12 @@ class InkResponse extends StatelessWidget {
/// duplication of information.
final bool excludeFromSemantics;

/// {@template flutter.material.inkwell.onFocusChange}
/// Handler called when the focus changes.
///
/// Called with true if this widget's node gains focus, and false if it loses
/// focus.
/// {@endtemplate}
final ValueChanged<bool>? onFocusChange;

/// {@macro flutter.widgets.Focus.autofocus}
Expand Down
5 changes: 5 additions & 0 deletions packages/flutter/lib/src/material/list_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ class ListTile extends StatelessWidget {
this.enabled = true,
this.onTap,
this.onLongPress,
this.onFocusChange,
this.mouseCursor,
this.selected = false,
this.focusColor,
Expand Down Expand Up @@ -456,6 +457,9 @@ class ListTile extends StatelessWidget {
/// Inoperative if [enabled] is false.
final GestureLongPressCallback? onLongPress;

/// {@macro flutter.material.inkwell.onFocusChange}
final ValueChanged<bool>? onFocusChange;

/// {@template flutter.material.ListTile.mouseCursor}
/// The cursor for a mouse pointer when it enters or is hovering over the
/// widget.
Expand Down Expand Up @@ -738,6 +742,7 @@ class ListTile extends StatelessWidget {
customBorder: shape ?? tileTheme.shape,
onTap: enabled ? onTap : null,
onLongPress: enabled ? onLongPress : null,
onFocusChange: onFocusChange,
mouseCursor: effectiveMouseCursor,
canRequestFocus: enabled,
focusNode: focusNode,
Expand Down
5 changes: 5 additions & 0 deletions packages/flutter/lib/src/material/radio_list_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ class RadioListTile<T> extends StatelessWidget {
this.selectedTileColor,
this.visualDensity,
this.focusNode,
this.onFocusChange,
this.enableFeedback,
}) : assert(toggleable != null),
assert(isThreeLine != null),
Expand Down Expand Up @@ -320,6 +321,9 @@ class RadioListTile<T> extends StatelessWidget {
/// {@macro flutter.widgets.Focus.focusNode}
final FocusNode? focusNode;

/// {@macro flutter.material.inkwell.onFocusChange}
final ValueChanged<bool>? onFocusChange;

/// {@macro flutter.material.ListTile.enableFeedback}
///
/// See also:
Expand Down Expand Up @@ -385,6 +389,7 @@ class RadioListTile<T> extends StatelessWidget {
contentPadding: contentPadding,
visualDensity: visualDensity,
focusNode: focusNode,
onFocusChange: onFocusChange,
enableFeedback: enableFeedback,
),
);
Expand Down
10 changes: 10 additions & 0 deletions packages/flutter/lib/src/material/switch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class Switch extends StatelessWidget {
this.overlayColor,
this.splashRadius,
this.focusNode,
this.onFocusChange,
this.autofocus = false,
}) : _switchType = _SwitchType.material,
assert(dragStartBehavior != null),
Expand Down Expand Up @@ -158,6 +159,7 @@ class Switch extends StatelessWidget {
this.overlayColor,
this.splashRadius,
this.focusNode,
this.onFocusChange,
this.autofocus = false,
}) : assert(autofocus != null),
assert(activeThumbImage != null || onActiveThumbImageError == null),
Expand Down Expand Up @@ -455,6 +457,9 @@ class Switch extends StatelessWidget {
/// {@macro flutter.widgets.Focus.focusNode}
final FocusNode? focusNode;

/// {@macro flutter.material.inkwell.onFocusChange}
final ValueChanged<bool>? onFocusChange;

/// {@macro flutter.widgets.Focus.autofocus}
final bool autofocus;

Expand All @@ -478,6 +483,7 @@ class Switch extends StatelessWidget {
final Size size = _getSwitchSize(context);
return Focus(
focusNode: focusNode,
onFocusChange: onFocusChange,
autofocus: autofocus,
child: Container(
width: size.width, // Same size as the Material switch.
Expand Down Expand Up @@ -518,6 +524,7 @@ class Switch extends StatelessWidget {
overlayColor: overlayColor,
splashRadius: splashRadius,
focusNode: focusNode,
onFocusChange: onFocusChange,
autofocus: autofocus,
);
}
Expand Down Expand Up @@ -577,6 +584,7 @@ class _MaterialSwitch extends StatefulWidget {
this.overlayColor,
this.splashRadius,
this.focusNode,
this.onFocusChange,
this.autofocus = false,
}) : assert(dragStartBehavior != null),
assert(activeThumbImage != null || onActiveThumbImageError == null),
Expand All @@ -603,6 +611,7 @@ class _MaterialSwitch extends StatefulWidget {
final MaterialStateProperty<Color?>? overlayColor;
final double? splashRadius;
final FocusNode? focusNode;
final Function(bool)? onFocusChange;
final bool autofocus;
final Size size;

Expand Down Expand Up @@ -822,6 +831,7 @@ class _MaterialSwitchState extends State<_MaterialSwitch> with TickerProviderSta
child: buildToggleable(
mouseCursor: effectiveMouseCursor,
focusNode: widget.focusNode,
onFocusChange: widget.onFocusChange,
autofocus: widget.autofocus,
size: widget.size,
painter: _painter
Expand Down
8 changes: 8 additions & 0 deletions packages/flutter/lib/src/material/switch_list_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ class SwitchListTile extends StatelessWidget {
this.selectedTileColor,
this.visualDensity,
this.focusNode,
this.onFocusChange,
this.enableFeedback,
this.hoverColor,
}) : _switchListTileType = _SwitchListTileType.material,
Expand Down Expand Up @@ -221,6 +222,7 @@ class SwitchListTile extends StatelessWidget {
this.selectedTileColor,
this.visualDensity,
this.focusNode,
this.onFocusChange,
this.enableFeedback,
this.hoverColor,
}) : _switchListTileType = _SwitchListTileType.adaptive,
Expand Down Expand Up @@ -368,6 +370,9 @@ class SwitchListTile extends StatelessWidget {
/// {@macro flutter.widgets.Focus.focusNode}
final FocusNode? focusNode;

/// {@macro flutter.material.inkwell.onFocusChange}
final ValueChanged<bool>? onFocusChange;

/// {@macro flutter.material.ListTile.enableFeedback}
///
/// See also:
Expand All @@ -394,6 +399,7 @@ class SwitchListTile extends StatelessWidget {
inactiveTrackColor: inactiveTrackColor,
inactiveThumbColor: inactiveThumbColor,
autofocus: autofocus,
onFocusChange: onFocusChange,
);
break;

Expand All @@ -409,6 +415,7 @@ class SwitchListTile extends StatelessWidget {
inactiveTrackColor: inactiveTrackColor,
inactiveThumbColor: inactiveThumbColor,
autofocus: autofocus,
onFocusChange: onFocusChange,
);
}

Expand Down Expand Up @@ -452,6 +459,7 @@ class SwitchListTile extends StatelessWidget {
tileColor: tileColor,
visualDensity: visualDensity,
focusNode: focusNode,
onFocusChange: onFocusChange,
enableFeedback: enableFeedback,
hoverColor: hoverColor,
),
Expand Down
2 changes: 2 additions & 0 deletions packages/flutter/lib/src/material/toggleable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ mixin ToggleableStateMixin<S extends StatefulWidget> on TickerProviderStateMixin
/// build method - potentially after wrapping it in other widgets.
Widget buildToggleable({
FocusNode? focusNode,
Function(bool)? onFocusChange,
bool autofocus = false,
required MaterialStateProperty<MouseCursor> mouseCursor,
required Size size,
Expand All @@ -314,6 +315,7 @@ mixin ToggleableStateMixin<S extends StatefulWidget> on TickerProviderStateMixin
actions: _actionMap,
focusNode: focusNode,
autofocus: autofocus,
onFocusChange: onFocusChange,
enabled: isInteractive,
onShowFocusHighlight: _handleFocusHighlightChanged,
onShowHoverHighlight: _handleHoverChanged,
Expand Down
29 changes: 29 additions & 0 deletions packages/flutter/test/material/checkbox_list_tile_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,35 @@ void main() {
expect(tileNode.hasPrimaryFocus, isTrue);
});

testWidgets('CheckboxListTile onFocusChange callback', (WidgetTester tester) async {
final FocusNode node = FocusNode(debugLabel: 'CheckboxListTile onFocusChange');
bool gotFocus = false;
await tester.pumpWidget(
MaterialApp(
home: Material(
child: CheckboxListTile(
value: true,
focusNode: node,
onFocusChange: (bool focused) {
gotFocus = focused;
},
onChanged: (bool? value) {},
),
),
),
);

node.requestFocus();
await tester.pump();
expect(gotFocus, isTrue);
expect(node.hasFocus, isTrue);

node.unfocus();
await tester.pump();
expect(gotFocus, isFalse);
expect(node.hasFocus, isFalse);
});

testWidgets('CheckboxListTile can be disabled', (WidgetTester tester) async {
bool? value = false;
bool enabled = true;
Expand Down
28 changes: 28 additions & 0 deletions packages/flutter/test/material/list_tile_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1476,6 +1476,34 @@ void main() {
expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.basic);
});

testWidgets('ListTile onFocusChange callback', (WidgetTester tester) async {
final FocusNode node = FocusNode(debugLabel: 'ListTile Focus');
bool gotFocus = false;
await tester.pumpWidget(
MaterialApp(
home: Material(
child: ListTile(
focusNode: node,
onFocusChange: (bool focused) {
gotFocus = focused;
},
onTap: () {},
),
),
),
);

node.requestFocus();
await tester.pump();
expect(gotFocus, isTrue);
expect(node.hasFocus, isTrue);

node.unfocus();
await tester.pump();
expect(gotFocus, isFalse);
expect(node.hasFocus, isFalse);
});

testWidgets('ListTile respects tileColor & selectedTileColor', (WidgetTester tester) async {
bool isSelected = false;
final Color tileColor = Colors.green.shade500;
Expand Down
30 changes: 30 additions & 0 deletions packages/flutter/test/material/radio_list_tile_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,36 @@ void main() {
expect(tileNode.hasPrimaryFocus, isTrue);
});

testWidgets('RadioListTile onFocusChange callback', (WidgetTester tester) async {
final FocusNode node = FocusNode(debugLabel: 'RadioListTile onFocusChange');
bool gotFocus = false;
await tester.pumpWidget(
MaterialApp(
home: Material(
child: RadioListTile<bool>(
value: true,
focusNode: node,
onFocusChange: (bool focused) {
gotFocus = focused;
},
onChanged: (bool? value) {},
groupValue: true,
),
),
),
);

node.requestFocus();
await tester.pump();
expect(gotFocus, isTrue);
expect(node.hasFocus, isTrue);

node.unfocus();
await tester.pump();
expect(gotFocus, isFalse);
expect(node.hasFocus, isFalse);
});

group('feedback', () {
late FeedbackTester feedback;

Expand Down
58 changes: 58 additions & 0 deletions packages/flutter/test/material/switch_list_tile_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,64 @@ void main() {
expect(tileNode.hasPrimaryFocus, isTrue);
});

testWidgets('SwitchListTile onFocusChange callback', (WidgetTester tester) async {
final FocusNode node = FocusNode(debugLabel: 'SwitchListTile onFocusChange');
bool gotFocus = false;
await tester.pumpWidget(
MaterialApp(
home: Material(
child: SwitchListTile(
value: true,
focusNode: node,
onFocusChange: (bool focused) {
gotFocus = focused;
},
onChanged: (bool value) {},
),
),
),
);

node.requestFocus();
await tester.pump();
expect(gotFocus, isTrue);
expect(node.hasFocus, isTrue);

node.unfocus();
await tester.pump();
expect(gotFocus, isFalse);
expect(node.hasFocus, isFalse);
});

testWidgets('SwitchListTile.adaptive onFocusChange Callback', (WidgetTester tester) async {
final FocusNode node = FocusNode(debugLabel: 'SwitchListTile.adaptive onFocusChange');
bool gotFocus = false;
await tester.pumpWidget(
MaterialApp(
home: Material(
child: SwitchListTile.adaptive(
value: true,
focusNode: node,
onFocusChange: (bool focused) {
gotFocus = focused;
},
onChanged: (bool value) {},
),
),
),
);

node.requestFocus();
await tester.pump();
expect(gotFocus, isTrue);
expect(node.hasFocus, isTrue);

node.unfocus();
await tester.pump();
expect(gotFocus, isFalse);
expect(node.hasFocus, isFalse);
});

group('feedback', () {
late FeedbackTester feedback;

Expand Down

0 comments on commit 0e35610

Please sign in to comment.