Skip to content

Conversation

@TahaTesser
Copy link
Member

@TahaTesser TahaTesser commented Jun 9, 2023

fixes #115827
fixes #101325

Description

  1. This PR adds a new MaterialState color property to all the chips (this makes it possible to customize chips in all states from the M3 specs).
  2. Updated defaults to use the new MaterialState color property.
  3. Updated and added new tests to all the chip test classes.
code sample
import 'package:flutter/material.dart';

const Color disabledColor = Colors.black26;
const Color backgroundColor = Colors.cyan;
final Color disabledSelectedColor = Colors.red.shade100;
const Color selectedColor = Colors.amber;
final MaterialStateProperty<Color> color =
    MaterialStateProperty.resolveWith((Set<MaterialState> states) {
  if (states.contains(MaterialState.disabled) &&
      states.contains(MaterialState.selected)) {
    return disabledSelectedColor;
  }
  if (states.contains(MaterialState.disabled)) {
    return disabledColor;
  }
  if (states.contains(MaterialState.selected)) {
    return selectedColor;
  }
  return backgroundColor;
});

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        useMaterial3: true,
        // chipTheme: ChipThemeData(color: color),
      ),
      home: const Example(),
    );
  }
}

class Example extends StatefulWidget {
  const Example({super.key});

  @override
  State<Example> createState() => _ExampleState();
}

class _ExampleState extends State<Example> {
  bool enabled = false;
  bool selected = true;

  @override
  Widget build(BuildContext context) {
    const Widget verticalSpace = SizedBox(height: 20);

    return Scaffold(
      body: Center(
        child: Column(
          children: <Widget>[
            const SizedBox(height: 25),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: <Widget>[
                const Card(
                  elevation: 0.0,
                  color: disabledColor,
                  child: Padding(
                    padding: EdgeInsets.all(8.0),
                    child: Text('disabledColor'),
                  ),
                ),
                const Card(
                  elevation: 0.0,
                  color: backgroundColor,
                  child: Padding(
                    padding: EdgeInsets.all(8.0),
                    child: Text('backgroundColor'),
                  ),
                ),
                Card(
                  elevation: 0.0,
                  color: disabledSelectedColor,
                  child: const Padding(
                    padding: EdgeInsets.all(8.0),
                    child: Text('disabledSelectedColor'),
                  ),
                ),
                const Card(
                  elevation: 0.0,
                  color: selectedColor,
                  child: Padding(
                    padding: EdgeInsets.all(8.0),
                    child: Text('selectedColor'),
                  ),
                ),
              ],
            ),
            const Spacer(),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: <Widget>[
                Column(
                  mainAxisSize: MainAxisSize.min,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    RawChip(
                      selected: selected,
                      selectedColor: selectedColor,
                      color: color,
                      label: const Text('RawChip'),
                      isEnabled: enabled,
                      onSelected: enabled ? (bool value) {} : null,
                    ),
                    verticalSpace,
                    InputChip(
                      isEnabled: enabled,
                      selected: selected,
                      selectedColor: selectedColor,
                      color: color,
                      label: const Text('InputChip'),
                      onSelected: enabled ? (bool value) {} : null,
                    ),
                  ],
                ),
                Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    FilterChip(
                      selected: selected,
                      selectedColor: selectedColor,
                      color: color,
                      label: const Text('FilterChip'),
                      onSelected: enabled ? (bool value) {} : null,
                    ),
                    verticalSpace,
                    FilterChip.elevated(
                      selected: selected,
                      selectedColor: selectedColor,
                      color: color,
                      label: const Text('FilterChip.elevated'),
                      onSelected: enabled ? (bool value) {} : null,
                    ),
                  ],
                ),
                Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    ChoiceChip(
                      selected: selected,
                      selectedColor: selectedColor,
                      color: color,
                      label: const Text('ChoiceChip'),
                      onSelected: enabled ? (bool value) {} : null,
                    ),
                    verticalSpace,
                    ChoiceChip.elevated(
                      selected: selected,
                      selectedColor: selectedColor,
                      color: color,
                      label: const Text('ChoiceChip.elevated'),
                      onSelected: enabled ? (bool value) {} : null,
                    ),
                  ],
                ),
              ],
            ),
            const Spacer(),
            Row(
              children: <Widget>[
                Flexible(
                  child: SwitchListTile(
                    title: const Text('Enabled'),
                    value: enabled,
                    onChanged: (bool value) {
                      setState(() => enabled = value);
                    },
                  ),
                ),
                Flexible(
                  child: SwitchListTile(
                    title: const Text('Selected'),
                    value: selected,
                    onChanged: (bool value) {
                      setState(() => selected = value);
                    },
                  ),
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}

Before (not possible to customize disabled and selected chips)

Screenshot 2023-06-13 at 16 27 13

After (using disabled and selected chips using the new MaterialState color property)

Screenshot 2023-06-13 at 16 26 53

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I signed the CLA.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

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

@github-actions github-actions bot added f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels. c: contributor-productivity Team-specific productivity, code health, technical debt. labels Jun 9, 2023
@TahaTesser TahaTesser marked this pull request as ready for review June 9, 2023 15:46
@TahaTesser TahaTesser requested a review from HansMuller June 9, 2023 15:46
@TahaTesser TahaTesser force-pushed the chips_disabledSelectedColor branch from 4c6fc1a to e10bf8a Compare June 13, 2023 14:53
@TahaTesser TahaTesser changed the title Add disabledSelectedColor for selectable chips Add MaterialState color property Jun 13, 2023
@TahaTesser TahaTesser force-pushed the chips_disabledSelectedColor branch from e10bf8a to ad4e45f Compare June 13, 2023 15:06
@TahaTesser TahaTesser changed the title Add MaterialState color property Introduce MaterialState color property for chips Jun 13, 2023
@TahaTesser TahaTesser force-pushed the chips_disabledSelectedColor branch from ad4e45f to f88778c Compare June 13, 2023 15:07
@TahaTesser TahaTesser requested a review from HansMuller June 13, 2023 16:14
@TahaTesser
Copy link
Member Author

Revamped the PR to introduce color property with a MaterialStateProperty<Color?>? value :)

Copy link
Contributor

@HansMuller HansMuller left a comment

Choose a reason for hiding this comment

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

This is a really impressive bit of work! It all looks good, just wanted to get your reaction to the feedback.

Copy link
Contributor

Choose a reason for hiding this comment

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

In a separate follow-on PR or two, we need to be a little bolder here:

  • Explain which color properties this one replaces
  • In the properties that this one replaces, explain that color is preferable.
  • Provide an example that demos this property and make sure that it's linked here and in the class API doc.
  • Add support for the focused and hovered states

Copy link
Member Author

@TahaTesser TahaTesser Jun 14, 2023

Choose a reason for hiding this comment

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

This sounds good.

Once this is merged, I'll create two issue trackers for these.

  1. Documentation and examples.
  2. support for the focused and hovered states

Copy link
Contributor

Choose a reason for hiding this comment

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

In a separate PR, maybe we could tweak our codegen tech to deal with things like this.

Copy link
Member Author

@TahaTesser TahaTesser Jun 14, 2023

Choose a reason for hiding this comment

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

Sounds good. Once this is merged, I'll create an issue tracker and reference this.

@TahaTesser TahaTesser force-pushed the chips_disabledSelectedColor branch from f88778c to b421ff3 Compare June 14, 2023 10:59
@TahaTesser TahaTesser requested a review from HansMuller June 14, 2023 13:35
Copy link
Contributor

@HansMuller HansMuller left a comment

Choose a reason for hiding this comment

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

LGTM

@TahaTesser TahaTesser force-pushed the chips_disabledSelectedColor branch from 36cdf0c to c36f7a5 Compare June 14, 2023 19:59
@TahaTesser TahaTesser force-pushed the chips_disabledSelectedColor branch 2 times, most recently from 7297432 to 6a3ff21 Compare June 16, 2023 14:24
@TahaTesser
Copy link
Member Author

@HansMuller
For some reason, Google testing seems to be broken on this.

@HansMuller
Copy link
Contributor

Looking into the Google Testing failure.

@HansMuller
Copy link
Contributor

Turns out to have been a problem with the "Google Testing" presubmit bot. It's being fixed now.

@TahaTesser
Copy link
Member Author

Turns out to have been a problem with the "Google Testing" presubmit bot. It's being fixed now.

Thanks for the update.

@TahaTesser TahaTesser force-pushed the chips_disabledSelectedColor branch from 6b66bd7 to 2dd51a0 Compare June 19, 2023 21:08
@TahaTesser TahaTesser added the autosubmit Merge PR when tree becomes green via auto submit App label Jun 19, 2023
@auto-submit auto-submit bot merged commit 467c970 into flutter:master Jun 19, 2023
@TahaTesser TahaTesser deleted the chips_disabledSelectedColor branch June 20, 2023 07:31
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jun 20, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jun 20, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jun 20, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jun 21, 2023
auto-submit bot pushed a commit to flutter/packages that referenced this pull request Jun 22, 2023
Manual version of #4269

----

Roll Flutter from fc8856e to c40baf4 (57 revisions)

flutter/flutter@fc8856e...c40baf4

2023-06-21 [email protected] Roll Packages from 6e1918f to 0fdf05f (8 revisions) (flutter/flutter#129286)
2023-06-21 [email protected] move test ownership from zanderso -> tools team (flutter/flutter#129199)
2023-06-21 [email protected] Refactor `Analytics` global getter to point to context only (flutter/flutter#129196)
2023-06-21 [email protected] Roll Flutter Engine from cfbd3652532d to f973fb4636d3 (1 revision) (flutter/flutter#129253)
2023-06-21 [email protected] Roll Flutter Engine from 059643dcc8e3 to cfbd3652532d (1 revision) (flutter/flutter#129243)
2023-06-21 [email protected] Roll Flutter Engine from 5313ca367549 to 059643dcc8e3 (1 revision) (flutter/flutter#129240)
2023-06-21 [email protected] Roll Flutter Engine from 946f523859fe to 5313ca367549 (2 revisions) (flutter/flutter#129234)
2023-06-21 [email protected] Roll Flutter Engine from e5a860c5479c to 946f523859fe (2 revisions) (flutter/flutter#129232)
2023-06-21 [email protected] Relax `OverlayPortal` asserts (flutter/flutter#129053)
2023-06-21 [email protected] Roll Flutter Engine from adfc3af300a9 to e5a860c5479c (3 revisions) (flutter/flutter#129228)
2023-06-21 [email protected] Move all the firebase lab device configs to .ci.yaml. (flutter/flutter#129219)
2023-06-21 [email protected] Roll Flutter Engine from 7d4abb81ccd1 to adfc3af300a9 (2 revisions) (flutter/flutter#129225)
2023-06-20 [email protected] update resolution-aware asset docs links (flutter/flutter#128769)
2023-06-20 [email protected] Fix: Magnifier appears and won't dismiss (flutter/flutter#128545)
2023-06-20 [email protected] DecoratedSliver (flutter/flutter#127823)
2023-06-20 [email protected] Roll Flutter Engine from 666244148e89 to 7d4abb81ccd1 (1 revision) (flutter/flutter#129217)
2023-06-20 [email protected] Roll Flutter Engine from 06d0c08460e5 to 666244148e89 (2 revisions) (flutter/flutter#129208)
2023-06-20 [email protected] fixed PreferredSize constuctor invocations (flutter/flutter#128181)
2023-06-20 [email protected] Roll Flutter Engine from 1c16af76ca26 to 06d0c08460e5 (3 revisions) (flutter/flutter#129200)
2023-06-20 [email protected] Roll Flutter Engine from ec64672afd91 to 1c16af76ca26 (1 revision) (flutter/flutter#129197)
2023-06-20 [email protected] Roll Flutter Engine from 4444ede34a9c to ec64672afd91 (3 revisions) (flutter/flutter#129194)
2023-06-20 [email protected] Fix duplicate devices from xcdevice with iOS 17 (flutter/flutter#128802)
2023-06-20 [email protected] iOS info.plist template: make UIViewControllerBasedStatusBar to be true (flutter/flutter#128970)
2023-06-20 [email protected] Fix detection that tests are running on monorepo bots (flutter/flutter#129173)
2023-06-20 [email protected] Use the new `getIsolatePauseEvent` method from VM service to check for pause event. (flutter/flutter#128834)
2023-06-20 [email protected] Adding ScrollController support for Stepper widget (flutter/flutter#128814)
2023-06-20 [email protected] Roll Packages from 59d93d6 to 6e1918f (6 revisions) (flutter/flutter#129176)
2023-06-20 [email protected] Refactor generate_localizations_test.dart (flutter/flutter#128974)
2023-06-20 [email protected] [process] Add a design doc issue template. (flutter/flutter#128361)
2023-06-20 [email protected] Roll Flutter Engine from bd6d3fc90462 to 4444ede34a9c (1 revision) (flutter/flutter#129169)
2023-06-20 [email protected] Roll Flutter Engine from 73c4ba4240cc to bd6d3fc90462 (1 revision) (flutter/flutter#129168)
2023-06-20 [email protected] Roll Flutter Engine from 7ee874792067 to 73c4ba4240cc (1 revision) (flutter/flutter#129162)
2023-06-20 [email protected] Roll Flutter Engine from 6a6c8fb591f5 to 7ee874792067 (1 revision) (flutter/flutter#129160)
2023-06-20 [email protected] Add to API docs to explain what Assist and Suggestion chips are (flutter/flutter#129034)
2023-06-20 [email protected] Roll Flutter Engine from a91bb3f566b9 to 6a6c8fb591f5 (1 revision) (flutter/flutter#129158)
2023-06-20 [email protected] Roll Flutter Engine from e0d456d9251b to a91bb3f566b9 (1 revision) (flutter/flutter#129148)
2023-06-20 [email protected] Roll Flutter Engine from 55418e648958 to e0d456d9251b (1 revision) (flutter/flutter#129146)
2023-06-19 [email protected] Roll Flutter Engine from 84ecaa053ec6 to 55418e648958 (1 revision) (flutter/flutter#129145)
2023-06-19 [email protected] Roll Flutter Engine from 23a2c246600f to 84ecaa053ec6 (2 revisions) (flutter/flutter#129142)
2023-06-19 [email protected] Introduce MaterialState `color` property for chips (flutter/flutter#128584)
2023-06-19 [email protected] Roll Flutter Engine from 280491d4cc21 to 23a2c246600f (8 revisions) (flutter/flutter#129140)
2023-06-19 [email protected] Fix an ordering dependency in the flutter_tools upgrade test (flutter/flutter#129131)
2023-06-19 [email protected] Roll Flutter Engine from 164c6b49dfb5 to 280491d4cc21 (1 revision) (flutter/flutter#129102)
2023-06-19 [email protected] Fix `InputDecoration.applyDefaults` ignoring some properties (flutter/flutter#129010)
2023-06-19 [email protected] Roll Flutter Engine from d298f0bf720c to 164c6b49dfb5 (1 revision) (flutter/flutter#129100)
2023-06-19 [email protected] Roll Flutter Engine from 7ffa1355f718 to d298f0bf720c (24 revisions) (flutter/flutter#129092)
...

----
auto-submit bot pushed a commit that referenced this pull request Jun 26, 2023
Updated the chip tests per the changes from #128584 so that Chip tests that depend on Material2 will continue to pass when Material3 becomes the default. 

Part of: #127064
@rydmike
Copy link
Contributor

rydmike commented Jul 6, 2023

Wow and thanks @TahaTesser, this is cool. Need to test it in practice. Not sure if it solves my grievance of being able to theme each variant of the Chips differently. I have to try to really see what is going.

engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 16, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 17, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 17, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 17, 2023
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 c: contributor-productivity Team-specific productivity, code health, technical debt. 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.

Cannot make M3 themed Selected and disabled ChoiceChip, FilterChip and InputChip (M3 spec) Allow Chip background to be state adaptive

3 participants