title: How do I get the ListTile index in an ExpansionTile?
body: <p>After expanding the list and then clicking a sub-item, I would like the row that was clicked to change color. At this point, the index all time is 0, which causes all subitems to be highlighted. How to get the correct index so that the clicked row is highlighted only?</p>
  <p><strong>Current state:</strong></p>
  <p><a href="https://i.stack.imgur.com/YQjsD.png" rel="nofollow noreferrer">Image</a></p>
  <p><strong>Vehicle class:</strong></p>
  <pre><code>class Vehicle {
    final String title;
    List&lt;String&gt; contents = [];
    final IconData icon;
  
    Vehicle(this.title, this.contents, this.icon);
  }
  </code></pre>
  <p><strong>List of objects:</strong></p>
  <pre><code>List&lt;Vehicle&gt; vehicles = [
    Vehicle(
      'Bike',
      ['Vehicle no. 1', 'Vehicle no. 2', 'Vehicle no. 7', 'Vehicle no. 10'],
      Icons.motorcycle,
    ),
    Vehicle(
      'Cars',
      ['Vehicle no. 3', 'Vehicle no. 4', 'Vehicle no. 6'],
      Icons.directions_car,
    ),
  ];
  </code></pre>
  <p><strong>Widget:</strong></p>
  <pre><code>class CreateNewViewPage extends StatefulWidget {
    @override
    State&lt;StatefulWidget&gt; createState() =&gt; _CreateNewViewPage();
  }
  
  class _CreateNewViewPage extends State&lt;CreateNewViewPage&gt; {
    int _selectedIndex;
  
    @override
    void initState() {
      super.initState();
    }
  
    @override
    Widget build(BuildContext context) {
  
      return Scaffold(
        backgroundColor: const Color(0xFF000624),
        appBar: AppBar(
          elevation: 0,
          backgroundColor: Colors.transparent,
          title: Text(
            'Example',
            style: TextStyle(color: Colors.white),
          ),
          leading: IconButton(
            icon: Icon(Icons.arrow_back),
            onPressed: () {
              Navigator.pop(context);
            },
          ),
        ),
        body: Column(
          children: &lt;Widget&gt;[
            Divider(
              color: Colors.white,
            ),
            Expanded(
              child: Container(
                width: MediaQuery.of(context).size.width / 1.4,
                child: ListView.builder(
                  itemCount: 2,
                  itemBuilder: (context, index) {
                    return Card(
                      child: ExpansionTile(
                        title: Text(vehicles[index].title, style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold, fontStyle: FontStyle.italic),),
                        children: &lt;Widget&gt;[
                          Column(
                            children: _buildExpandableContent(vehicles[index], index),
                          ),
                        ],
                      ),
                    );
                  },
                ),
              ),
            )
          ],
        ),
      );
    }
  
    _buildExpandableContent(Vehicle vehicle, index) {
      List&lt;Widget&gt; columnContent = [];
  
      for (String content in vehicle.contents)
        columnContent.add(
          ListTile(
            title: Text(content, style: new TextStyle(fontSize: 18.0),),
            leading: Icon(vehicle.icon),
            tileColor: _selectedIndex == index ? Colors.blue : null,
            onTap: () {
              setState(() {
                _selectedIndex = index;
              });
            },
          ),
        );
  
      return columnContent;
    }
  }
  </code></pre>