title: Flutter - select only single item in list view
body: <p>In my app I am generating a ListView and items can be highlighted by tapping on them.  That works fine and I also have a callback function that gives me the key for the just selected item. I can currently manually deselect the item by tapping on it again, but will ultimately take that functionality out.</p>
  <p>My problem is that I want one and only one item to be selected at a time.  In order to create the list I currently take some initial content in the form of a list, generate the tiles and add them to another list.  I then use that list to create the ListView. My plan was on the callback from a new selection, run through the list of tiles and deselect them before highlighting the new chosen tile and carrying out the other functions.  I have tried various methods to tell each tile to deselect itself but have not found any way to address each of the tiles. Currently I get the error:</p>
  <p>Class 'OutlineTile' has no instance method 'deselect'.
  Receiver: Instance of 'OutlineTile'
  Tried calling: deselect()</p>
  <p>I have tried to access a method within the tile class and to use a setter but neither worked so far.  I am quite new to flutter so it could be something simple I am missing. My previous experience was with Actionscript where this system would have worked fine and I could access a method of an object (in this case the tile) easily as long s it is a public method.</p>
  <p>I'd be happy to have another way to unselect the old item or to find a way to access a method within the tile.  <strong>The challenge is to make the tiles show not highlighted without them being tapped themselves but when a different tile is tapped.</strong></p>
  <p>The code in my parent class is as follows:</p>
  <pre><code>class WorkingDraft extends StatefulWidget {
    final String startType;
    final String name;
    final String currentContent;
    final String currentID;
    final List startContent;
  
    WorkingDraft(
        {this.startType,
        this.name,
        this.currentContent,
        this.currentID,
        this.startContent});
  
    @override
    _WorkingDraftState createState() =&gt; _WorkingDraftState();
  }
  
  class _WorkingDraftState extends State&lt;WorkingDraft&gt; {
    final _formKey = GlobalKey&lt;FormState&gt;();
    final myController = TextEditingController();
    //String _startType;
    String _currentContent = &quot;&quot;;
    String _name = &quot;Draft&quot;;
    List _startContent = [];
    List _outLineTiles = [];
    int _counter = 0;
  
    @override
    void dispose() {
      // Clean up the controller when the widget is disposed.
      myController.dispose();
      super.dispose();
    }
  
    void initState() {
      super.initState();
  
      _currentContent = widget.currentContent;
      _name = widget.name;
      _startContent = widget.startContent;
      _counter = 0;
  
      _startContent.forEach((element) {
        _outLineTiles.add(OutlineTile(
          key: Key(&quot;myKey$_counter&quot;),
          outlineName: element[0],
          myContent: element[1],
          onTileSelected: clearHilights,
        ));
        _counter++;
      });
    }
  
    dynamic clearHilights(Key myKey) {
      _outLineTiles.forEach((element) {
        element.deselect();  // this throws an error Class 'OutlineTile' has no instance method 'deselect'.
        Key _foundKey = element.key;
        print(&quot;Element Key $_foundKey&quot;);
      });
    }
  .......
  </code></pre>
  <p>and further down within the widget build scaffold:</p>
  <pre><code>      child: ListView.builder(
          itemCount: _startContent.length,
          itemBuilder: (context, index) {
            return _outLineTiles[index];
          },
        ),
  </code></pre>
  <p>Then the tile class is as follows:</p>
  <pre><code>class OutlineTile extends StatefulWidget {
    final Key key;
    final String outlineName;
    final Icon myIcon;
    final String myContent;
    final Function(Key) onTileSelected;
  
    OutlineTile(
        {this.key,
        this.outlineName,
        this.myIcon,
        this.myContent,
        this.onTileSelected});
  
    @override
    _OutlineTileState createState() =&gt; _OutlineTileState();
  }
  
  class _OutlineTileState extends State&lt;OutlineTile&gt; {
    Color color;
    Key _myKey;
  
    @override
    void initState() {
      super.initState();
  
      color = Colors.transparent;
    }
  
    bool _isSelected = false;
    set isSelected(bool value) {
      _isSelected = value;
      print(&quot;set is selected to $_isSelected&quot;);
    }
  
    void changeSelection() {
      setState(() {
        _myKey = widget.key;
        _isSelected = !_isSelected;
        if (_isSelected) {
          color = Colors.lightBlueAccent;
        } else {
          color = Colors.transparent;
        }
      });
    }
  
    void deselect() {
      setState(() {
        isSelected = false;
        color = Colors.transparent;
      });
    }
  
    @override
    Widget build(BuildContext context) {
      return Padding(
        padding: const EdgeInsets.only(top: 4.0),
        child: Row(
          children: [
            Card(
              elevation: 10,
              margin: EdgeInsets.fromLTRB(10.0, 6.0, 5.0, 0.0),
              child: SizedBox(
                width: 180,
                child: Container(
                  color: color,
                  child: ListTile(
                    title: Text(widget.outlineName),
                    onTap: () {
                      if (widget.outlineName == &quot;Heading&quot;) {
                        Text(&quot;Called Heading&quot;);
                      } else (widget.outlineName == &quot;Paragraph&quot;) {
                        Text(&quot;Called Paragraph&quot;);
                      widget.onTileSelected(_myKey);
                      changeSelection();
                    },
                  ),
  
           ........ 
  </code></pre>
  <p>Thanks for any help.</p>
  <p><strong>Amended Code sample and explanation, that builds to a complete project, from here:</strong></p>
  <p>Following the advice from phimath I have created a full buildable sample of the relevant part of my project.</p>
  <p>The problem is that the tiles in my listview are more complex with several elements, many of which are buttons in their own right so whilst phimath's solution works for simple text tiles I have not been able to get it working inside my own project.  My approach is trying to fundamentally do the same thing as phimath's but when I include these more complex tiles it fails to work.</p>
  <p>This sample project is made up of three files.  main.dart which simply calls the project and passes in some dummy data in the way my main project does. working_draft.dart which is the core of this issue.  And outline_tile.dart which is the object that forms the tiles.</p>
  <p>Within working draft I have a function that returns an updated list of the tiles which should show which tile is selected (and later any other changes from the other buttons).  This gets called when first going to the screen.  When the tile is tapped it uses a callback function to redraw the working_draft class but this seems to not redraw the list as I would expect it to.  Any further guidance would be much appreciated.</p>
  <p>The classes are:</p>
  <p>first class is main.dart:</p>
  <pre><code>import 'package:flutter/material.dart';
  import 'package:listexp/working_draft.dart';
  
  void main() =&gt; runApp(MyApp());
  
  class MyApp extends StatelessWidget {
    // This widget is the root of your application.
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
          home: WorkingDraft(
        startType: &quot;Basic&quot;,
        name: &quot;Draft&quot;,
        currentID: &quot;anID&quot;,
        startContent: [
          [&quot;Heading&quot;, &quot;New Heading&quot;],
          [&quot;Paragraph&quot;, &quot;New Text&quot;],
          [&quot;Image&quot;, &quot;placeholder&quot;],
          [&quot;Signature&quot;, &quot;placeholder&quot;]
        ],
      ));
    }
  }
  </code></pre>
  <p>Next file is working_draft.dart:</p>
  <pre><code>import 'package:flutter/material.dart';
  import 'package:listexp/outline_tile.dart';
  
  class WorkingDraft extends StatefulWidget {
    final String startType;
    final String name;
    final String currentContent;
    final String currentID;
    final List startContent;
    final int selectedIndex;
  
    WorkingDraft(
        {this.startType,
        this.name,
        this.currentContent,
        this.currentID,
        this.startContent,
        this.selectedIndex});
  
    @override
    _WorkingDraftState createState() =&gt; _WorkingDraftState();
  }
  
  class _WorkingDraftState extends State&lt;WorkingDraft&gt; {
    int selectedIndex;
    String _currentContent = &quot;&quot;;
    String _name = &quot;Draft&quot;;
    List _startContent = [];
    var _outLineTiles = [];
    int _counter = 0;
    int _selectedIndex;
    bool _isSelected;
  
    dynamic clearHilights(int currentIndex) {
      setState(() {
        _selectedIndex = currentIndex;
      });
    }
  
    updatedTiles() {
      if (_selectedIndex == null) {
        _selectedIndex = 0;
      }
  
      _currentContent = widget.currentContent;
      _name = widget.name;
      _startContent = widget.startContent;
      _counter = 0;
      _outLineTiles = [];
      _startContent.forEach((element) {
        _isSelected = _selectedIndex == _counter ? true : false;
        _outLineTiles.add(OutlineTile(
          key: Key(&quot;myKey$_counter&quot;),
          outlineName: element[0],
          myContent: element[1],
          myIndex: _counter,
          onTileSelected: clearHilights,
          isSelected: _isSelected,
        ));
        _counter++;
      });
    }
  
    @override
    Widget build(BuildContext context) {
      updatedTiles();
      return Scaffold(
          body: Center(
        child: Column(children: [
          SizedBox(height: 100),
          Text(&quot;Outline&quot;, style: new TextStyle(fontSize: 15)),
          Container(
            height: 215,
            width: 300,
            decoration: BoxDecoration(
              border: Border.all(
                color: Colors.lightGreenAccent,
                width: 2,
              ),
              borderRadius: BorderRadius.circular(2),
            ),
            child: ListView.builder(
              itemCount: _startContent.length,
              itemBuilder: (context, index) {
                return _outLineTiles[index];
              },
            ),
          ),
        ]),
      ));
    }
  }
  </code></pre>
  <p>and finally is outline_tile.dart</p>
  <pre><code>import 'package:flutter/material.dart';
  
  class OutlineTile extends StatefulWidget {
    final Key key;
    final String outlineName;
    final Icon myIcon;
    final String myContent;
    final int myIndex;
    final Function(int) onTileSelected;
    final bool isSelected;
  
    OutlineTile(
        {this.key,
        this.outlineName,
        this.myIcon,
        this.myContent,
        this.myIndex,
        this.onTileSelected,
        this.isSelected});
  
    @override
    _OutlineTileState createState() =&gt; _OutlineTileState();
  }
  
  class _OutlineTileState extends State&lt;OutlineTile&gt; {
    Color color;
    // Key _myKey;
    bool _isSelected;
  
    @override
    void initState() {
      super.initState();
  
      _isSelected = widget.isSelected;
  
      if (_isSelected == true) {
        color = Colors.lightBlueAccent;
      } else {
        color = Colors.transparent;
      }
    }
  
    void deselect() {
      setState(() {
        _isSelected = widget.isSelected;
  
        if (_isSelected == true) {
          color = Colors.lightBlueAccent;
        } else {
          color = Colors.transparent;
        }
      });
    }
  
    @override
    Widget build(BuildContext context) {
      return Padding(
        padding: const EdgeInsets.only(top: 4.0),
        child: Row(
          children: [
            Card(
              elevation: 10,
              margin: EdgeInsets.fromLTRB(10.0, 6.0, 5.0, 0.0),
              child: SizedBox(
                width: 180,
                child: Container(
                  color: color,
                  child: ListTile(
                    title: Text(widget.outlineName),
                    onTap: () {
                      if (widget.outlineName == &quot;Heading&quot;) {
                        Text(&quot;Called Heading&quot;);
                      } else if (widget.outlineName == &quot;Paragraph&quot;) {
                        Text(&quot;Called Paragraph&quot;);
                      } else if (widget.outlineName == &quot;Signature&quot;) {
                        Text(&quot;Called Signature&quot;);
                      } else {
                        Text(&quot;Called Image&quot;);
                      }
                      var _myIndex = widget.myIndex;
  
                      widget.onTileSelected(_myIndex);
                      deselect();
                    },
                  ),
                ),
              ),
            ),
            SizedBox(
              height: 60,
              child: Column(
                children: [
                  SizedBox(
                    height: 20,
                    child: IconButton(
                        iconSize: 30,
                        icon: Icon(Icons.arrow_drop_up),
                        onPressed: () {
                          print(&quot;Move Up&quot;);
                        }),
                  ),
                  SizedBox(height: 5),
                  SizedBox(
                    height: 20,
                    child: IconButton(
                        iconSize: 30,
                        icon: Icon(Icons.arrow_drop_down),
                        onPressed: () {
                          print(&quot;Move Down&quot;);
                        }),
                  ),
                ],
              ),
            ),
            SizedBox(
              height: 60,
              child: Column(
                children: [
                  SizedBox(
                    height: 20,
                    child: IconButton(
                        iconSize: 20,
                        icon: Icon(Icons.add_box),
                        onPressed: () {
                          print(&quot;Add another&quot;);
                        }),
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  SizedBox(
                    height: 20,
                    child: IconButton(
                        iconSize: 20,
                        icon: Icon(Icons.delete),
                        onPressed: () {
                          print(&quot;Delete&quot;);
                        }),
                  ),
                ],
              ),
            ),
          ],
        ),
      );
    }
  }
  </code></pre>
  <p>Thanks again</p>