This demo shows how you can open an expansion tile while closing an already open expansion tile.

Overview

Expansion Tile Open/Close Demo

This demo shows you how to open an expansion tile while simultaneously closing an already open tile while maintaining animation using the Expansion Tile widget.

Alt Text

Getting Started

Flutter 3.0.6-0.0.pre.1 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 7ce44f121f (13 days ago) • 2022-08-19 14:37:55 -0500
Engine • revision e85ea0e79c
Tools • Dart 2.17.6 • DevTools 2.12.2

Clone the repository to your dev pc. Inside the lib directory you will see two files:

  1. main.dart
  2. expansion_tile_copy.dart

expansion_tile_copy.dart File

  • This file contains a copy of the Flutter source code for the expansion tile. The only difference is that we changed the class name to "ExpansionTileCopy" and added two methods to the state class:
// Added to class
void closeExpansion() {
if (_isExpanded) _handleTap();
}

// Added to class
void openExpansion() {
if (!_isExpanded) _handleTap();
}
  • These methods will allow us to open/close the specified expansion tile. We are only going to be using the closeExpansion method for this project.

main.dart File

final _tileKeys = [];
var _selectedIndex = 0;
  • The code above is added top level to allow for global access outside of the class. Can be contained within the class if for isolated use.
    • The _tileKeys list stores the GlobalKeys assigned to each expansion tile.
    • The _selectedIndex variable stores the index of the currently opened expansion tile.
ListView.separated(
    itemBuilder: (context, index) {
        final tileKey = GlobalKey();
        _tileKeys.add(tileKey); // <- Add tile key to [_tileKeys] list.
        return ExpansionTileCopy(
            key: tileKey, // <- Add a key to each tile.
            title: Text("Tile $index"),
            children: [
                Container(
                    color: Colors.grey[350],
                    height: 200,
                    child: Center(
                    child: Text("Tile $index Body"),
                    ))
            ],
            onExpansionChanged: (value) {
                // If tile is expanding, then collapse the already expanded tile.
                if (value) {
                    if (index != _selectedIndex) {
                        _tileKeys[_selectedIndex].currentState!.closeExpansion();
                    }
                    _selectedIndex = index;
                }
            },
        );
    },
    separatorBuilder: (context, index) => const Divider(height: 0),
    itemCount: 100));
  • Each generated GlobalKey is stored in the _tileKeys list.
  • The generated GlobalKey is assigned to the ExpansionTileCopy key field.
  • In the onExpansionChanged callback function, we check if the value is true, which indicates the tile is opening. We only want to execute the block if we are expanding another tile. If the index of the tile being expanded does not equal the _selectedIndex value, then we want to fetch the GlobalKey for the currently expanded tile by it's index, which is represented by the _selectedIndex value. Once the GlobalKey is obtained, we then execute the closeExpansion method that we added to the ExpansionTileCopy state class.
  • This will collapse the currently expanded tile, while expanding the tile that was just clicked on.
  • We then assign the new index to the _selectedIndex variable.

And that's it. I hope this help's you in your future projects. Please leave me any questions, comments, or concerns and I will be happy to respond. Thank you


You might also like...

This is a Flutter app which shows how to add a Fitted Box in you App

fittedbox A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started if thi

Oct 25, 2021

A simple button that gives you the possibility to transform into a circular one and shows a progress indicator

A simple button that gives you the possibility to transform into a circular one and shows a progress indicator

Progress Button A simple button that gives you the possibility to transform into

Dec 22, 2021

This is a Flutter Food Recipe App this shows food recipes of any food which you want.

food_recipe_app A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started

Oct 31, 2022

This is my way to build a Tagged Search Field that display a list with suggestions while the user type.

This is my way to build a Tagged Search Field that display a list with suggestions while the user type.

tagged_search_field This is my way to build a Tagged Search Field that display a list with suggestions while the user type. A regular search field at

Nov 5, 2021

Learn to Code While Building Apps - The Complete Flutter Development Bootcamp

Learn to Code While Building Apps - The Complete Flutter Development Bootcamp

BMI Calculator 💪 Our Goal The objective of this tutorial is to look at how we can customise Flutter Widgets to achieve our own beautiful user interfa

Jan 1, 2023

The codes written while teaching Dart lessons in Flutter Fest

The codes written while teaching Dart lessons in Flutter Fest

dart_lessons Flutter Fest etkinliğinde Dart dersleri anlatılırken yazılmış kodla

Aug 28, 2022

Rock-Paper-Scissor-Game-Using-Dart - This is a repository of Rock Paper Scissor Game which I developed while learning Dart.

Rock-Paper-Scissor-Game-Using-Dart This is a repository of Rock Paper Scissor Game which I developed while learning Dart. The main.dart file consist o

Jan 4, 2022

Learn to Code While Building Apps - The Complete Flutter Development Bootcamp

Learn to Code While Building Apps - The Complete Flutter Development Bootcamp

Quizzler ❓ Our Goal In this tutorial we will be reviewing Stateful and Stateless Widgets as well as learning about the fundamental building blocks of

Dec 31, 2022

A flutter library for loading images from network, resizing as per container size and caching while being memory sensitive.

Optimized Cached Image A flutter library for loading images from network, resizing and caching them for memory sensitivity. This resizes and stores th

Dec 20, 2022
Owner
Trey Thomas
Flutter Developer-Programmer Languages: Dart Python Javascript Frameworks: Flutter Flask VueJS ExpressJS Databases: MongoDB MySQL
Trey Thomas
Responsive Scaffold - On mobile it shows a list and pushes to details and on tablet it shows the List and the selected item. Maintainer: @rodydavis

responsive_scaffold View the online demo here! On mobile it shows a list and pushes to details and on tablet it shows the List and the selected item.

Flutter Community 346 Dec 2, 2022
"Login Demo" app which shows how to use google sign in Android and iOS using Flutter.

⚠️ ARCHIVED: This repository is using Flutter 1.7 for the sample app. You can find the latest version of the similar implementation on this new repo.

Souvik Biswas 195 Dec 2, 2022
An application built using Flutter that can be used while playing board games if actual or physical dice is missing . This is a dual dice application.

diceee_utter A new Flutter application. Getting Started This project is a starting point for a Flutter application. A few resources to get you started

dev_allauddin 3 Feb 3, 2022
This application was created using the Dart language and it is an application that contains a set of different questions and at the end shows you the number of correct answers you have answered , made by flutter

exams_app A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started if thi

null 0 Dec 28, 2021
Web3-demo-flutter - A demo for the flutter web3 library.

web3_demo_flutter A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you starte

Tommaso Azzalin 0 Oct 7, 2022
A Flutter add-to-app demo you can try with your own apps

Put Flutter to Work ?? Hello! This project is a demo intended to help people test drive Flutter by integrating it into their existing applications. In

Flutter 280 Jan 8, 2023
dos downloader app is developed for downloading video. You can download video from YouTube and Facebook. You can also play video on background

dosdownloader Dos downloader app is developed for downloading video. You can download video from YouTube and Facebook. You can also play video on back

Md Abir Ahsan Tahmim 1 Dec 8, 2021
A widget that shows an image which can be scaled and dragged using gestures.

[DISCONTINUED] - 24.05.2021 While this widget was useful in the early days of Flutter, the Flutter team introduced an own way to zoom and pan, see Int

EPNW 15 May 3, 2022