Skip to content

Roll Switch.adaptive changes into CupertinoSwitch - #149465

Merged
auto-submit[bot] merged 25 commits into
flutter:masterfrom
victorsanni:adaptive2-cupertino-switch
Jul 12, 2024
Merged

auto-submit[bot] merged 25 commits into
flutter:masterfrom
victorsanni:adaptive2-cupertino-switch

Conversation

@victorsanni

@victorsanni victorsanni commented Jun 1, 2024 •

Copy link
Copy Markdown
Contributor

Brings over the changes from Switch.adaptive into CupertinoSwitch.

This change adds the following Switch parameters to CupertinoSwitch:
inactiveThumbColor, activeThumbImage, onActiveThumbImageError, inactiveThumbImage, onInactiveThumbImageError, trackOutlineColor, trackOutlineWidth, thumbIcon, mouseCursor.

The following Switch parameters are ignored:

  • activeTrackColor: because activeColor has the same function.
  • inactiveTrackColor: because trackColor has the same function.
  • materialTapTargetSize: because it is only applicable in Material.
  • hoverColor, overlayColor, splashRadius: because these parameters configure the radial reaction overlay in Material, so are not applicable here.

The following CupertinoSwitch parameters which are absent from Switch.adaptive are retained:

  • onLabelColor,
  • offLabelColor,
  • applyTheme

trackColor and thumbColor are of type WidgetStateProperty in Material Switch, but are currently of type Color in CupertinoSwitch. For backwards compatibility, both parameters are kept as Colors, but can be resolved in different WidgetStates using WidgetStateColor.resolve().

This PR does not apply any fidelity updates to CupertinoSwitch.

Fixes #149275

Related PRs: #130425 #148804

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. p: cupertino_ui cupertino_ui package in flutter/packages labels Jun 1, 2024
@victorsanni victorsanni changed the title Clean room Roll Switch.adaptive changes into CupertinoSwitch Jun 1, 2024
@victorsanni
victorsanni marked this pull request as ready for review June 1, 2024 00:34
Comment thread packages/flutter/lib/src/cupertino/switch.dart Outdated
Comment thread packages/flutter/lib/src/cupertino/switch.dart Outdated
Comment thread packages/flutter/lib/src/cupertino/switch.dart Outdated
@justinmc
justinmc self-requested a review June 3, 2024 21:14
@Piinks
Piinks self-requested a review June 4, 2024 17:53
Comment thread packages/flutter/lib/src/cupertino/switch.dart Outdated
@Piinks

Piinks commented Jun 4, 2024

Copy link
Copy Markdown
Contributor

This change adds the following Switch parameters to CupertinoSwitch:
inactiveThumbColor, activeThumbImage, onActiveThumbImageError, inactiveThumbImage, onInactiveThumbImageError, trackOutlineColor, trackOutlineWidth, thumbIcon, mouseCursor.

If these properties did not exist before, is this a good opportunity to introduce them with WidgetStateProperties? The migration from MSP --> WSP was motivated by extending the concept to Cupertino. Should we be adopting that as we are revamping Cupertino widgets?

Same for trackColor and thumbColor, we could deprecate those to use matching named parameters to align better with the material properties and use WidgetStateProperties. These are just color names for a common concept in this type of component, they are not tied directly to material design.


Separate question, some folks have asked - what do we do about the reimplemented iOS Switch in the material library (behind Switch.adaptive)?

That is the only (I think?) existing implementation of an iOS widget in material, rather than .adaptive returning a Cupertino variant.
Rolling those updates here does mean we have some code duplication, but I think that is ok considering the small single case in material.

We could decide to go back to Switch.adaptive and have it return CupertinoSwitch again while still using theme adaptations to help style, or maybe we will implement more material.adaptive widgets like Switch later. We're waiting on the results of a user study (just a couple of weeks away) to help make these decisions.

I am curious though, @MitchellGoodwin @justinmc what are your thoughts here?

@MitchellGoodwin

Copy link
Copy Markdown
Contributor

On widget state properties in general, I think we should use them if there's multiple styling variations for a property. If the Cupertino version (according to native, not necessarily how we have it now) is always the same value, then in my opinion it's ok to not use widget state properties.

On the adaptive Switch in the Material library, I think eventually we need to change it to align with everything else we are doing (or change everything else to match it). For now though I think it's a low priority until after the study, and we can keep it around. It's a good example case of that approach.

@Piinks

Piinks commented Jun 5, 2024

Copy link
Copy Markdown
Contributor

On widget state properties in general, I think we should use them if there's multiple styling variations for a property. If the Cupertino version (according to native, not necessarily how we have it now) is always the same value, then in my opinion it's ok to not use widget state properties.

On the adaptive Switch in the Material library, I think eventually we need to change it to align with everything else we are doing (or change everything else to match it). For now though I think it's a low priority until after the study, and we can keep it around. It's a good example case of that approach.

@MitchellGoodwin I love this way of determining if we should use WidgetStateProperty or not.

Comment thread packages/flutter/lib/src/cupertino/switch.dart Outdated
Comment thread packages/flutter/lib/src/cupertino/switch.dart Outdated
Comment thread packages/flutter/lib/src/cupertino/switch.dart Outdated
Comment thread packages/flutter/lib/src/cupertino/switch.dart Outdated

@justinmc justinmc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As others were discussing above, I agree with keeping the Material iOS implementation around until we've seen the user study and made a clear plan about how to handle this stuff.

Do you plan to make Switch.adaptive point to CupertinoSwitch in a subsequent PR?

Is it not practical to abstract any of this to a shared RawSwitch for sharing code? Just making sure we've thought about that. I'm guessing that a lot of the tests you added are mostly duplicated in Material, but they maybe could be deduplicated if there was a RawSwitch.

Comment thread packages/flutter/lib/src/cupertino/switch.dart
Comment thread packages/flutter/lib/src/cupertino/switch.dart
Comment thread packages/flutter/lib/src/cupertino/switch.dart Outdated
Comment thread packages/flutter/lib/src/cupertino/switch.dart Outdated
Comment thread packages/flutter/lib/src/cupertino/switch.dart Outdated
/// Paints an iOS-style slider thumb.
///
/// Used by [CupertinoSwitch] and [CupertinoSlider].
/// Used by [CupertinoSlider].

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why not use CupertinoThumbPainter? Is it out of date?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

CupertinoThumbPainter doesn't paint an image/icon on the thumb. I see some duplicated code, especially in the _paintCupertinoThumbShadowAndBorder method as compared to CupertinoThumbPainter's paint method. I thought it would be a challenge to refactor CupertinoThumbPainter to use this new implementation, so I just decided to not use it for now.

Comment thread packages/flutter/test/cupertino/switch_test.dart Outdated
..rrect(color: const Color(0xffffffff)),
);
});
}, variant: TargetPlatformVariant.only(TargetPlatform.macOS));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why is this Mac only now?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I had focus and focus tests only on macOS, I am not sure if that is good or bad (or what the consensus/best practice is).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have removed the macOS requirement for focus.

Comment thread packages/flutter/test/cupertino/switch_test.dart Outdated
Comment thread packages/flutter/test/cupertino/switch_test.dart
Comment thread packages/flutter/lib/src/cupertino/switch.dart
Comment thread packages/flutter/lib/src/cupertino/switch.dart Outdated
Comment thread packages/flutter/lib/src/cupertino/switch.dart Outdated

@MitchellGoodwin MitchellGoodwin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A small nit and a question. Otherwise, this looks good to me.

Comment thread packages/flutter/lib/src/cupertino/switch.dart Outdated
});

/// Creates an object that paints an iOS-style switch thumb.
const CupertinoThumbPainter.switchThumb({

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we deprecate this? I wouldn't be surprised if nobody uses it, but it's technically a public API.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's true. It's not used anywhere else in the framework at least, but this would not be the way to deprecate it anyways.

Comment thread packages/flutter/lib/src/cupertino/switch.dart Outdated

@QuncCccccc QuncCccccc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, assuming we only focus on having matched properties in material Switch and cupertino Switch in this PR:)!

@MitchellGoodwin MitchellGoodwin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@victorsanni victorsanni added the autosubmit Merge PR when tree becomes green via auto submit App label Jul 12, 2024
@auto-submit
auto-submit Bot merged commit dddea4d into flutter:master Jul 12, 2024
@victorsanni
victorsanni deleted the adaptive2-cupertino-switch branch July 12, 2024 00:16
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App framework flutter/packages/flutter repository. See also f: labels. p: cupertino_ui cupertino_ui package in flutter/packages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bring CupertinoSwitch up to speed with Switch.adaptive

5 participants