Skip to main content

Command Palette

Search for a command to run...

ExpansionPanel Widget and Attributes

Updated
2 min read
ExpansionPanel Widget and Attributes
V

"Hello World, I'm Vinit Mepani, a coding virtuoso driven by passion, fueled by curiosity, and always poised to conquer challenges. Picture me as a digital explorer, navigating through the vast realms of code, forever in pursuit of innovation.

In the enchanting kingdom of algorithms and syntax, I wield my keyboard as a magical wand, casting spells of logic and crafting solutions to digital enigmas. With each line of code, I embark on an odyssey of learning, embracing the ever-evolving landscape of technology.

Eager to decode the secrets of the programming universe, I see challenges not as obstacles but as thrilling quests, opportunities to push boundaries and uncover new dimensions in the realm of possibilities.

In this symphony of zeros and ones, I am Vinit Mepani, a coder by passion, an adventurer in the digital wilderness, and a seeker of knowledge in the enchanting world of code. Join me on this quest, and let's create digital wonders together!"

Dynamically managing content visibility is a common requirement in many Flutter applications. The ExpansionPanel widget provides an elegant solution by allowing users to expand or collapse sections of content. In this blog post, we'll explore the ExpansionPanel widget, its key attributes, and provide a comprehensive example to showcase its versatility.

The ExpansionPanel widget in Flutter facilitates the creation of collapsible and expandable panels that reveal or hide content based on user interaction. It is particularly useful when dealing with sections of content that can be toggled to conserve space or provide a focused view.

Attributes of ExpansionPanel:

  1. headerBuilder (required):

    • A callback function that builds the header widget for the panel.
  2. body:

    • The widget that represents the content of the panel.
  3. isExpanded (required):

    • A boolean value that determines whether the panel is initially expanded or collapsed.
  4. canTapOnHeader:

    • A boolean value that defines whether tapping on the header will expand or collapse the panel.
  5. backgroundColor:

    • The background color of the panel.
  6. iconColor:

    • The color of the expand/collapse icon.
  7. iconPadding:

    • The padding around the expand/collapse icon.
  8. onExpansionChanged:

    • A callback function that is triggered when the panel's expansion state changes.

Example: Let's create a Flutter app that uses the ExpansionPanel widget to display a list of items with expandable content.

import 'package:flutter/material.dart';

class myApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('ExpansionTile'),
        ),
        body: ListView.builder(
          itemBuilder: (BuildContext context, int index) => EntryItem(data[index]),
          itemCount: data.length,
        ),
      ),
    );
  }
}

// One entry in the multilevel list displayed by this app.
class Entry {
  Entry(this.title, [this.children = const <Entry>[]]);
  final String title;
  final List<Entry> children;
}

// The entire multilevel list displayed by this app.
final List<Entry> data = <Entry>[
  Entry('Chapter A',
    <Entry>[
      Entry('Section A0',
        <Entry>[
          Entry('Item A0.1'),
          Entry('Item A0.2'),
          Entry('Item A0.3'),
        ],
      ),
      Entry('Section A1'),
      Entry('Section A2'),
    ],
  ),
  Entry('Chapter B',
    <Entry>[
      Entry('Section B0'),
      Entry('Section B1'),
    ],
  ),
  Entry('Chapter C',
    <Entry>[
      Entry('Section C0'),
      Entry('Section C1'),
      Entry('Section C2',
        <Entry>[
          Entry('Item C2.0'),
          Entry('Item C2.1'),
          Entry('Item C2.2'),
          Entry('Item C2.3'),
        ],
      ),
    ],
  ),
];

// Displays one Entry. If the entry has children then it's displayed
// with an ExpansionTile.
class EntryItem extends StatelessWidget {
  const EntryItem(this.entry);

  final Entry entry;

  Widget _buildTiles(Entry root) {
    if (root.children.isEmpty)
      return ListTile(title: Text(root.title));
    return ExpansionTile(
      key: PageStorageKey<Entry>(root),
      title: Text(root.title),
      children: root.children.map<Widget>(_buildTiles).toList(),
    );
  }

  @override
  Widget build(BuildContext context) {
    return _buildTiles(entry);
  }
}

void main() {
  runApp(myApp());
}

Learn Flutter

Part 1 of 50

Explore Flutter's magic in crafting cross-platform apps effortlessly. Join the adventure!

More from this blog

Vinit Mepani (Flutter Developer)

270 posts

"Vinit Mepani, passionate coder! Dive into my Dart and Flutter journey on the blog. Let's master these tech wonders together. Happy coding! 🚀"