Allow customizing entry mode icons in TimePicker (#157151)

This PR adds two new properties to the Material Design `TimePicker`: `switchToInputEntryModeIcon` and `switchToTimerEntryModeIcon`. These allow developers to customize the icons for toggling between dial and input modes, with default icons (`Icons.keyboard_outlined` and `Icons.access_time`) used if none are provided.

Fixes #156942
This commit is contained in:
aniq
2024-10-29 11:25:20 +01:00
committed by GitHub
parent 2de487308a
commit c83cf967cf
2 changed files with 80 additions and 1 deletions

View File

@@ -2159,6 +2159,8 @@ class TimePickerDialog extends StatefulWidget {
this.initialEntryMode = TimePickerEntryMode.dial,
this.orientation,
this.onEntryModeChanged,
this.switchToInputEntryModeIcon,
this.switchToTimerEntryModeIcon,
});
/// The time initially selected when the dialog is shown.
@@ -2219,6 +2221,12 @@ class TimePickerDialog extends StatefulWidget {
/// Callback called when the selected entry mode is changed.
final EntryModeChangeCallback? onEntryModeChanged;
/// {@macro flutter.material.time_picker.switchToInputEntryModeIcon}
final Icon? switchToInputEntryModeIcon;
/// {@macro flutter.material.time_picker.switchToTimerEntryModeIcon}
final Icon? switchToTimerEntryModeIcon;
@override
State<TimePickerDialog> createState() => _TimePickerDialogState();
}
@@ -2416,7 +2424,9 @@ class _TimePickerDialogState extends State<TimePickerDialog> with RestorationMix
color: theme.useMaterial3 ? null : entryModeIconColor,
style: theme.useMaterial3 ? IconButton.styleFrom(foregroundColor: entryModeIconColor) : null,
onPressed: _toggleEntryMode,
icon: Icon(_entryMode.value == TimePickerEntryMode.dial ? Icons.keyboard_outlined : Icons.access_time),
icon: _entryMode.value == TimePickerEntryMode.dial
? widget.switchToInputEntryModeIcon ?? const Icon(Icons.keyboard_outlined)
: widget.switchToTimerEntryModeIcon ?? const Icon(Icons.access_time),
tooltip: _entryMode.value == TimePickerEntryMode.dial
? MaterialLocalizations.of(context).inputTimeModeButtonLabel
: MaterialLocalizations.of(context).dialModeButtonLabel,
@@ -2505,6 +2515,8 @@ class _TimePickerDialogState extends State<TimePickerDialog> with RestorationMix
entryMode: _entryMode.value,
orientation: widget.orientation,
onEntryModeChanged: _handleEntryModeChanged,
switchToInputEntryModeIcon: widget.switchToInputEntryModeIcon,
switchToTimerEntryModeIcon: widget.switchToTimerEntryModeIcon,
),
),
),
@@ -2540,6 +2552,8 @@ class _TimePicker extends StatefulWidget {
this.entryMode = TimePickerEntryMode.dial,
this.orientation,
this.onEntryModeChanged,
this.switchToInputEntryModeIcon,
this.switchToTimerEntryModeIcon,
});
/// Optionally provide your own text for the help text at the top of the
@@ -2608,6 +2622,12 @@ class _TimePicker extends StatefulWidget {
/// Callback called when the selected entry mode is changed.
final EntryModeChangeCallback? onEntryModeChanged;
/// {@macro flutter.material.time_picker.switchToInputEntryModeIcon}
final Icon? switchToInputEntryModeIcon;
/// {@macro flutter.material.time_picker.switchToTimerEntryModeIcon}
final Icon? switchToTimerEntryModeIcon;
@override
State<_TimePicker> createState() => _TimePickerState();
}
@@ -2941,6 +2961,22 @@ class _TimePickerState extends State<_TimePicker> with RestorationMixin {
/// parameter to override the default and force the dialog to appear in either
/// portrait or landscape mode.
///
/// {@template flutter.material.time_picker.switchToInputEntryModeIcon}
/// The optional [switchToInputEntryModeIcon] argument can be used to customize
/// the input method icon that is shown when the [TimePickerEntryMode]
/// is [TimePickerEntryMode.dial].
///
/// Defaults to an [Icon] widget with [Icons.keyboard_outlined] as icon.
/// {@endtemplate}
///
/// {@template flutter.material.time_picker.switchToTimerEntryModeIcon}
/// The optional [switchToTimerEntryModeIcon] argument can be used to customize
/// the input method icon that is shown when the [TimePickerEntryMode]
/// is [TimePickerEntryMode.input].
///
/// Defaults to an [Icon] widget with [Icons.access_time] as icon.
/// {@endtemplate}
///
/// {@macro flutter.widgets.RawDialogRoute}
///
/// By default, the time picker gets its colors from the overall theme's
@@ -3015,6 +3051,8 @@ Future<TimeOfDay?> showTimePicker({
EntryModeChangeCallback? onEntryModeChanged,
Offset? anchorPoint,
Orientation? orientation,
Icon? switchToInputEntryModeIcon,
Icon? switchToTimerEntryModeIcon,
}) async {
assert(debugCheckHasMaterialLocalizations(context));
@@ -3029,6 +3067,8 @@ Future<TimeOfDay?> showTimePicker({
minuteLabelText: minuteLabelText,
orientation: orientation,
onEntryModeChanged: onEntryModeChanged,
switchToInputEntryModeIcon: switchToInputEntryModeIcon,
switchToTimerEntryModeIcon: switchToTimerEntryModeIcon,
);
return showDialog<TimeOfDay>(
context: context,

View File

@@ -1646,6 +1646,45 @@ void main() {
expect(find.text(errorInvalidText), findsOneWidget);
});
testWidgets('TimePicker default entry icons', (WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(
home: TimePickerDialog(initialTime: TimeOfDay.now()),
));
// Check that the default icon for the dial mode is displayed.
expect(find.byIcon(Icons.keyboard_outlined), findsOneWidget);
expect(find.byIcon(Icons.access_time), findsNothing);
// Tap the icon to switch to input mode.
await tester.tap(find.byIcon(Icons.keyboard_outlined));
await tester.pumpAndSettle();
// Check that the icon for the input mode is displayed.
expect(find.byIcon(Icons.access_time), findsOneWidget);
expect(find.byIcon(Icons.keyboard_outlined), findsNothing);
});
testWidgets('Can override TimePicker entry icons', (WidgetTester tester) async {
const Icon customInputIcon = Icon(Icons.text_fields);
const Icon customTimerIcon = Icon(Icons.watch);
await tester.pumpWidget(MaterialApp(
home: TimePickerDialog(
initialTime: TimeOfDay.now(),
switchToInputEntryModeIcon: customInputIcon,
switchToTimerEntryModeIcon: customTimerIcon,
),
));
// Check that the custom icons are displayed.
expect(find.byIcon(Icons.text_fields), findsOneWidget);
expect(find.byIcon(Icons.watch), findsNothing);
// Tap the custom icon to switch to input mode.
await tester.tap(find.byIcon(Icons.text_fields));
await tester.pumpAndSettle();
// Check that the custom icon for the input mode is displayed.
expect(find.byIcon(Icons.text_fields), findsNothing);
expect(find.byIcon(Icons.watch), findsOneWidget);
});
testWidgets('Can switch from input to dial entry mode', (WidgetTester tester) async {
await mediaQueryBoilerplate(
tester,