Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify toggle mode style with DatePickerTheme #164102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

MaironLucas
Copy link

@MaironLucas MaironLucas commented Feb 25, 2025

  • Add properties to DatePickerThemeData:
    • toggleModeStyle allow customization on _DatePickerModeToggleButton TextStyle;
    • toggleModeForegroundColor allow changing the color of both button and Icon with consistency;
  • Compatible with existing code by the use of defaults;
  • Adjust test to cover new properties

How to customize toggle mode style before:

  showDatePicker(
    context: context,
    firstDate: DateTime(2025),
    lastDate: DateTime(2026),
    builder:
        (context, child) => Theme(
          data: ThemeData.light().copyWith(
            textTheme: TextTheme(titleSmall: TextStyle(fontSize: 16)),
            datePickerTheme: DatePickerThemeData(
              dayStyle: TextStyle(fontSize: 12),
            ),
          ),
          child: child!,
        ),
  );

Now:

  showDatePicker(
    context: context,
    firstDate: DateTime(2025),
    lastDate: DateTime(2026),
    builder:
        (context, child) => DatePickerTheme(
          data: DatePickerThemeData(
            toggleModeStyle: TextStyle(fontSize: 16),
            dayStyle: TextStyle(fontSize: 12),
          ),
          child: child!,
        ),
  );

Ref #163866
Ref #160591

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@github-actions github-actions bot added framework flutter/packages/flutter repository. See also f: labels. f: material design flutter/packages/flutter/material repository. labels Feb 25, 2025
@MaironLucas MaironLucas force-pushed the 163866 branch 3 times, most recently from 9d6a702 to 584b0a1 Compare February 25, 2025 19:41
@MaironLucas MaironLucas marked this pull request as ready for review February 25, 2025 19:42
@MaironLucas
Copy link
Author

MaironLucas commented Feb 25, 2025

I implemented this new feature before seeing that issue #160591 requested a similar feature. That issue already has a linked PR (#161458), but that PR doesn't seem to have received updates since January 24, so my PR could be a valid implementation for both issues.
Also, I took into account the comments and reviews of that PR.

@matanlurey matanlurey removed their request for review February 25, 2025 22:27
@dkwingsmt dkwingsmt requested a review from QuncCccccc March 12, 2025 18:43
Copy link
Contributor

@QuncCccccc QuncCccccc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution! Overall this looks good to me! Just left some comments below.

///
/// The [TextStyle.color] of [toggleModeStyle] is not used, [toggleModeForegroundColor]
/// is used instead.
final TextStyle? toggleModeStyle;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think if toggleButtonTextStyle would be more straightforward to understand? :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

/// Overrides the default color used for text labels and icons of toggle mode button.
///
/// This is used instead of the [TextStyle.color] property of [toggleModeStyle].
final Color? toggleModeForegroundColor;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To fix #160591, I think we'd better also apply this color to the month pick button. As for naming, I think subHeaderForegroundColor would be more straightforward to understand.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

- Add properties to DatePickerThemeData:
    - `toggleButtonTextStyle` allow customization on _DatePickerModeToggleButton
    TextStyle;
    - `subHeaderForegroundColor` allow changing the color of both button
    and Icons with consistency;
- Compatible with existing code by the use of defaults;
- Adjust test to cover new properties

Ref flutter#163866
Ref flutter#160591
Copy link
Contributor

@QuncCccccc QuncCccccc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update! Left some comments below. Please let me know if there's any questions:)

@@ -72,5 +71,7 @@
"md.comp.date-picker.modal.year-selection.year.unselected.focus.state-layer.color": "onSurfaceVariant",
"md.comp.date-picker.modal.year-selection.year.unselected.hover.state-layer.color": "onSurfaceVariant",
"md.comp.date-picker.modal.year-selection.year.unselected.label-text.color": "onSurfaceVariant",
"md.comp.date-picker.modal.year-selection.year.unselected.pressed.state-layer.color": "onSurfaceVariant"
"md.comp.date-picker.modal.year-selection.year.unselected.pressed.state-layer.color": "onSurfaceVariant",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This json file is generated from internal google, please don't edit it manually:)

@@ -62,6 +62,14 @@ class _${blockName}DefaultsM3 extends DatePickerThemeData {
@override
Color? get backgroundColor => ${componentColor("md.comp.date-picker.modal.container")};

@override
Color? get subHeaderForegroundColor => ${componentColor("md.comp.date-picker.modal.sub.header.foreground")}.withOpacity(0.60);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use the existing token instead of adding new tokens manually:)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, that makes sense. This part was a bit confusing to me. So I should use something like md.comp.date-picker.modal.weekdays.label-text for subHeaderForegroundColor and md.comp.date-picker.modal.range-selection.month.subhead for toggleButtonTextStyle? Thats it?

@@ -458,12 +459,12 @@ class _DatePickerModeToggleButtonState extends State<_DatePickerModeToggleButton
child: Text(
widget.title,
overflow: TextOverflow.ellipsis,
style: textTheme.titleSmall?.copyWith(color: controlColor),
style: buttonTextStyle?.apply(color: toggleModeForegroundColor),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we provide a toggleButtonTextStyle with a custom text color in date picker theme, but don't provide a subHeaderForegroundColor in the date picker theme, this change will cause that the customized text color is not respected, and the text color always defaults to the default subHeaderForegroundColor .

For the text color here, ideally, I think we want to firstly check whether theme's toggleButtonTextStyle's color is empty, if not empty, we respect the color value; but if empty, then check whether theme's subheaderForegroundColor is empty, if not empty, text color should respect it; if empty, we should go to defaults.toggleButtonTextStyle, if still empty, we should check defaults.subHeaderForegroundColor. Does this make sense?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeahh, it makes sense. I'm gonna implement it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants