Skip to content

Conversation

@xvrh
Copy link
Contributor

@xvrh xvrh commented Jul 3, 2022

Allows to programmatically expand or collapse an ExpansionTile
Fixes #60387

Example usage:

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final GlobalKey<ExpansionTileState> _expansionTileKey = GlobalKey<ExpansionTileState>();

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        ExpansionTile(
          key: _expansionTileKey,
          title: const Text('Collapsible menu'),
          children: const <Widget>[
            Text('Menu'),
          ],
        ),
        ElevatedButton(
          onPressed: () {
            ExpansionTileState expansionTile = _expansionTileKey.currentState!;
            if (expansionTile.isExpanded) {
              expansionTile.collapse();
            } else {
              expansionTile.expand();
            }
          },
          child: const Text('TOGGLE MENU'),
        )
      ],
    );
  }
}

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.

@flutter-dashboard flutter-dashboard bot added d: api docs Issues with https://api.flutter.dev/ d: examples Sample code and demos documentation 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 Jul 3, 2022
@flutter-dashboard
Copy link

This pull request executed golden file tests, but it has not been updated in a while (20+ days). Test results from Gold expire after as many days, so this pull request will need to be updated with a fresh commit in order to get results from Gold.

For more guidance, visit Writing a golden file test for package:flutter.

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

@Hixie
Copy link
Contributor

Hixie commented Oct 4, 2022

@xvrh This patch looks fantastic, sorry it took us so long to look at it. Would you mind rebasing it and force pushing an update? That will cause the tests to restart, so we can see an up to date status.

xvrh added 3 commits October 5, 2022 10:00
Allows to programmatically expand or collapse an ExpansionTile
@xvrh xvrh force-pushed the xha/expose_expansion_tile_state branch from 0dfd996 to 744a9fc Compare October 5, 2022 08:02
@xvrh
Copy link
Contributor Author

xvrh commented Oct 5, 2022

@Hixie I rebased the branch.
I'm available to make any other requested changes.

@flosketch
Copy link

+1 this PR

@goderbauer goderbauer requested review from HansMuller and Piinks March 7, 2023 23:57
@HansMuller
Copy link
Contributor

I've been reluctant to review this PR because it's generally not a good idea to turn a State object into public API, since State has many methods that aren't relevant in a case like this. Usually, we've dealt with this case by creating a class like ExpansionTileController that the developer can pass to a ExpansionTile and that exposes only as much API as is needed.

@xvrh
Copy link
Contributor Author

xvrh commented Mar 8, 2023

@HansMuller understood!
I went with the public State approach as it the obvious one used by Navigator, Scaffold etc...

I will try with the ExpansionTileController way, taking inspiration from ScrollController, TabController etc...

@xvrh
Copy link
Contributor Author

xvrh commented Mar 14, 2023

I'll close this PR and reopen a new one when I have time to work on the ExpansionTileController idea.

@xvrh xvrh closed this Mar 14, 2023
@HansMuller
Copy link
Contributor

@xvrh - there are have been some additional requests for this feature so I'm going to try and land an ExpansionTileController version shortly. What I'm planning is very similar to what you've done, except that the controller's API will be limited to isExpanded, expand() and collapse():

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final ExpansionTileController controller = ExpansionTileController();

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        ExpansionTile(
          controller: controller,
          title: const Text('Collapsible menu'),
          children: const <Widget>[
            Text('Menu'),
          ],
        ),
        ElevatedButton(
          onPressed: () {
            if (controller.isExpanded) {
              controller.collapse();
            } else {
              controller.expand();
            }
          },
          child: const Text('TOGGLE MENU'),
        )
      ],
    );
  }
}

This really is the same thing under the hood, so an ExpansionTileController can only control one ExpansionTile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c: contributor-productivity Team-specific productivity, code health, technical debt. d: api docs Issues with https://api.flutter.dev/ d: examples Sample code and demos 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.

Programmatically open and close ExpansionTile Widget.

4 participants