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

Add support for ExpansionPanel custom splash color #147126

Merged
merged 13 commits into from
May 31, 2024

Conversation

BenjiFarquhar
Copy link
Contributor

@BenjiFarquhar BenjiFarquhar commented Apr 21, 2024

Relates to #147098 and #147097

Aside from fixing the expland/collapse icon color in the other PR, I noticed the splash color on both the icon button and the full expansion panel (if ExpansionPanel.canTapOnHeader is set to true) is just Theme.of(context).splashColor on the InkWell and Theme.of(context).highlightColor on the IconButton which may not suit the color scheme of the ExpansionPanel, so I have added a custom field splashColor, which will effect both the IconButton and the full panel Inkwell.

Pre-launch Checklist

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

@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact "@test-exemption-reviewer" in the #hackers channel in Chat (don't just cc them here, they won't see it! Use Discord!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@github-actions github-actions bot added framework flutter/packages/flutter repository. See also f: labels. f: material design flutter/packages/flutter/material repository. labels Apr 21, 2024
@BenjiFarquhar BenjiFarquhar changed the title Make ExpansionPanel support custom splash color Add support for ExpansionPanel custom splash color Apr 21, 2024
Copy link
Contributor

@dkwingsmt dkwingsmt left a comment

Choose a reason for hiding this comment

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

To reconfirm, if canTapOnHeader is true, is there no way to trigger the icon splash at all?

Also, can you add tests?

packages/flutter/lib/src/material/expansion_panel.dart Outdated Show resolved Hide resolved
packages/flutter/lib/src/material/expand_icon.dart Outdated Show resolved Hide resolved
Co-authored-by: Tong Mu <dkwingsmt@users.noreply.github.com>
@BenjiFarquhar
Copy link
Contributor Author

BenjiFarquhar commented May 8, 2024

To reconfirm, if canTapOnHeader is true, is there no way to trigger the icon splash at all?

Also, can you add tests?

@dkwingsmt Correct. The Icon shouldn't splash if canTapOnHeader is true, as the whole panel splashes, so it would look too busy. It is more of a signal to the user that the panel is expandable than a button, if canTapOnHeader is true.

I will add tests.

@BenjiFarquhar
Copy link
Contributor Author

BenjiFarquhar commented May 9, 2024

@dkwingsmt I have implemented the changes, I noticed highlightColor was available for InkWell and IconButton so I just added them both, it is a bit clearer than setting the IconButton.highlightColor to ExpansionPanel.splashColor.

I might have exposed a bug with IconButton.splashColor. We don't see the blue it in the below example, but the widget test that checks the IconButton.splashColor passes. It will be why I was setting the IconButton.highlightColor in my first commit.

You can test with this in main.dart:

import 'package:flutter/material.dart';

/// Flutter code sample for [ExpansionPanelList].

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('ExpansionPanelList Sample')),
        body: const ExpansionPanelListExample(),
      ),
    );
  }
}

// stores ExpansionPanel state information
class Item {
  Item({
    required this.expandedValue,
    required this.headerValue,
    required this.value,
    required this.canTapOnHeader,
  });

  String expandedValue;
  String headerValue;
  int value;
  bool canTapOnHeader;
}

List<Item> generateItems(int numberOfItems) {
  return List<Item>.generate(numberOfItems, (int index) {
    bool canTapOnHeader = index > 3;
    return Item(
      headerValue: 'Panel $index canTapOnHeader=${canTapOnHeader.toString()}',
      expandedValue: 'This is item number $index',
      value: index,
      canTapOnHeader: canTapOnHeader,
    );
  });
}

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

  @override
  State<ExpansionPanelListExample> createState() =>
      _ExpansionPanelListExampleState();
}

class _ExpansionPanelListExampleState extends State<ExpansionPanelListExample> {
  final List<Item> _data = generateItems(8);

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Container(
        child: _buildPanel(),
      ),
    );
  }

  Widget _buildPanel() {
    return ExpansionPanelList.radio(
      children: _data.map<ExpansionPanelRadio>((Item item) {
        return ExpansionPanelRadio(
          splashColor: Colors.blue,
          highlightColor: Colors.green,
          headerBuilder: (BuildContext context, bool isExpanded) {
            return ListTile(
              title: Text(item.headerValue),
            );
          },
          body: ListTile(
              title: Text(item.expandedValue),
          ),
          value: item.value,
          canTapOnHeader: item.canTapOnHeader,
        );
      }).toList(),
      expandIconColor: Colors.red,
    );
  }
}

@BenjiFarquhar
Copy link
Contributor Author

Hi @dkwingsmt, can we progress this? It should be visibly working by running the example code in my above comment, and the widget tests verify the colors get passed through to the underlying widgets.

Copy link
Contributor

@dkwingsmt dkwingsmt left a comment

Choose a reason for hiding this comment

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

LGTM

@dkwingsmt
Copy link
Contributor

dkwingsmt commented May 28, 2024

The change looks great!

I also ran the demo above, and I see that in the canTapOnHeader=false cases, there are only green splashes, and no blue highlights, as opposed to the canTapOnHeader=true cases. Does it mean that IconButton.highlightColor is never applied?

@BenjiFarquhar
Copy link
Contributor Author

Thanks @dkwingsmt. I have updated the test to verify that the underlying IconButton.splashColor and IconButton.highlightColor are both correctly set to the ExpansionPanel equivalent fields; ExpansionPanel.splashColor and ExpansionPanel.highlightColor, as this is the widget that will display the button splashes.

There was some confusion around the fact that IconButton.splashColor is unused in material 3. From the IconButton.splashColor code comments:

If [ThemeData.useMaterial3] is set to true, this will not be used. Use [highlightColor] instead to show the overlay color of the button when the button is in the pressed state.

However, Inkwell does use splashColor in Material 3, so both properties are useful, no matter which version of Material theme is used.

I have updated the doc comments to inform the developer when highlightColor should be used instead of splashColor.

Copy link
Contributor

@dkwingsmt dkwingsmt left a comment

Choose a reason for hiding this comment

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

LGTM! Great finding!

Copy link
Contributor

@justinmc justinmc left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@BenjiFarquhar
Copy link
Contributor Author

Cheers @dkwingsmt & @justinmc. Good to merge?

@dkwingsmt dkwingsmt added the autosubmit Merge PR when tree becomes green via auto submit App label May 31, 2024
@auto-submit auto-submit bot merged commit 5116886 into flutter:master May 31, 2024
72 checks passed
victorsanni pushed a commit to victorsanni/flutter that referenced this pull request May 31, 2024
Relates to flutter#147098 and flutter#147097

Aside from fixing the expland/collapse icon color in the other PR, I noticed the splash color on both the icon button and the full expansion panel (if ExpansionPanel.canTapOnHeader is set to true) is just `Theme.of(context).splashColor` on the `InkWell` and `Theme.of(context).highlightColor` on the `IconButton` which may not suit the color scheme of the `ExpansionPanel`, so I have added a custom field `splashColor`, which will effect both the `IconButton` and the full panel `Inkwell`.
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jun 2, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jun 2, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jun 3, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jun 3, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jun 3, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jun 3, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jun 3, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jun 3, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jun 3, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jun 3, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jun 3, 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 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.

None yet

3 participants