I want when I click on one of the Expansion Tiles, the rest of the items are closed, how can I do that?
Refer to below example code :
import 'package:flutter/material.dart';
class PickPlanPage extends StatefulWidget {
const PickPlanPage({Key? key}) : super(key: key);
@override
State<PickPlanPage> createState() => _PickPlanPageState();
}
class _PickPlanPageState extends State<PickPlanPage> {
bool initiallyExpanded = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: ListView.separated(
itemCount: 5,
separatorBuilder: (context, index) => const SizedBox(height: 20),
itemBuilder: (context, index) => ExpansionTile(
initiallyExpanded: false,
title: const Text('title'),
onExpansionChanged: (value) => setState(() => initiallyExpanded = value),
children: const [
Text('Description'),
],
),
),
);
}
}
2
Answers
You can use
ExpansionPanelList
and track the selected item tap event.You can archive in your existing code with small modification.
make sure you need to add
key
in ListView &ExpansionTile
widget. try bellow code.