Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: ExpansionTile force closes when Scrolling

Tags:

flutter

dart

ExpansionTile force closes once it detects a scrolling behavior i.e immediately I scroll the page... I want to keep the ExpansionTile opened until I tapped on its title.

What could be the cause?

expansionTile force loses

like image 901
Tayo.dev Avatar asked Sep 05 '25 03:09

Tayo.dev


1 Answers

Add and remove indices from a list with onExpansionChanged callback and set initiallyExpanded if it contains that index.

class _WidgetState extends State<Widget> {
    
      List expandedIndices = [];
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
           body: ListView.builder(
                    physics: const BouncingScrollPhysics(
                    parent: AlwaysScrollableScrollPhysics()),
                    itemCount: 10,
                    itemBuilder: (BuildContext context, int index) {
                        return ExpansionTile(
                            title: Text('Item'),
                             onExpansionChanged: (expanded) {
                                 if(expanded) {
                                     expandedIndices.add(index);
                                 }
                                 else{
                                     expandedIndices.remove(index);
                                 }
                             },
                             initiallyExpanded: expandedIndices.contains(index),);
                    } 
                )
            );
       }
  }
like image 114
Yash Avatar answered Sep 08 '25 02:09

Yash