title: how to limit the number of selected items in a list in Flutter?
body: <pre><code>import 'package:flutter/material.dart';
  
  void main() =&gt; runApp(MaterialApp(
        home: oneCardSelectinAnimation(),
      ));
  
  class oneCardSelectinAnimation extends StatefulWidget {
    @override
    _oneCardSelectinAnimationState createState() =&gt;
        _oneCardSelectinAnimationState();
  }
  
  class ListItem&lt;T&gt; {
    bool isSelected = false; //Selection property to highlight or not
    T data; //Data of the user
    ListItem(this.data); //Constructor to assign the data
  }
  
  class _oneCardSelectinAnimationState extends State&lt;oneCardSelectinAnimation&gt; {
    List&lt;ListItem&lt;String&gt;&gt; list;
    @override
    void initState() {
      super.initState();
      populateData();
    }
  
    void populateData() {
      list = [];
      for (int i = 0; i &lt; 78; i++) list.add(ListItem&lt;String&gt;("item $i"));
    }
  
    int numberOfSelectedCards = 7;
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text("List Selection${numberOfSelectedCards}"),
        ),
        body: Container(
          height: MediaQuery.of(context).size.height * 0.3,
          child: ListView.builder(
            scrollDirection: Axis.horizontal,
            itemCount: list.length,
            itemBuilder: _getListItemTile,
          ),
        ),
      );
    }
  
    Widget _getListItemTile(BuildContext context, int index) {
      return GestureDetector(
        onTap: () {
          if (list.any((item) =&gt; item.isSelected)) {
            setState(() {
              list[index].isSelected = !list[index].isSelected;
              print(list[index].isSelected);
              numberOfSelectedCards += 1;
  
              print(list[index].isSelected);
            });
          }
        },
        onLongPress: () {
          if (numberOfSelectedCards &gt; 0) {
            setState(() {
              list[index].isSelected = true;
              numberOfSelectedCards -= 1;
            });
          }
        },
        child: Container(
          width: MediaQuery.of(context).size.width * 0.3,
          height: MediaQuery.of(context).size.height * 0.3,
          margin: EdgeInsets.symmetric(vertical: 4),
          color: list[index].isSelected ? Colors.red[100] : Colors.white,
          child: Image.asset(
            "images/scroll.png",
          ),
        ),
      );
    }
  }
  </code></pre>
  
  <p>This is my code what I wanted to do is limit the selected items to int numberOfSelectedCards = 7;</p>
  
  <p>I want to select only seven of cards to be selected. 
  I have tried to increase and decrease the numbersOfSelected cards under OnTaped and OnlongedPressed but it do not work correctly.</p>
  
  <p>And if I navigate other page. I want to reset the page to 0 selected items(reset the page).
  Thanks in advance.</p>