title: mulitiple indexes are being deleted. but I only want the index selected to be deleted Flutter, dart
body: <p>I am trying to delete a specific ExtraItem when the &quot;Delete&quot; Flatbutton is pressed. Everything seems in terms of the index being deleted. But, it also deletes/clears other previously added text from the UI (which I do not want). How do I fix the clearing of all other previously added text?</p>
  <p>Test case 1
  before deletion of index 0: <a href="https://i.stack.imgur.com/PEHtS.png" rel="nofollow noreferrer">before deletion</a><br />
  after deletion of index 0: <a href="https://i.stack.imgur.com/PzibB.png" rel="nofollow noreferrer">after deletion</a></p>
  <p>Test case 2
  before deletion of index 1: <a href="https://i.stack.imgur.com/P35to.png" rel="nofollow noreferrer">before deletion</a>
  after deletion of index 1: <a href="https://i.stack.imgur.com/EvNNY.png" rel="nofollow noreferrer">after deletion</a></p>
  <p>Here is my code, in other for reproduction</p>
  <pre><code>class ExtraItem extends StatefulWidget {
    bool isLast = false;
    final VoidCallback onExtraAdded;
    final VoidCallback onNameChanged;
    final VoidCallback onPriceChanged;
    final VoidCallback onDeleteClicked;
    final ExtraItem extra;
    String name;
  
    int price;
    int index;
    ExtraItem({
      Key key,
      this.isLast,
      this.onExtraAdded,
      this.extra,
      this.index,
      this.onNameChanged,
      this.onPriceChanged,
      this.onDeleteClicked,
    }) : super(key: key);
  
    @override
    _ExtraItemState createState() =&gt; _ExtraItemState();
  }
  
  class _ExtraItemState extends State&lt;ExtraItem&gt; {
    String type;
  
    TextEditingController nameController = TextEditingController();
  
    TextEditingController priceController = TextEditingController();
  
    @override
    void dispose() {
      nameController.dispose();
      priceController.dispose();
      super.dispose();
    }
  
    _showExtrasDialog() async {
      await showDialog&lt;String&gt;(
        context: this.context,
        child: new _SystemPadding(
          child: new AlertDialog(
            title: Text(
              'Modify Extras ',
              style: TextStyle(fontFamily: &quot;Montserrat Medium&quot;, fontSize: 15),
            ),
            contentPadding: const EdgeInsets.all(16.0),
            content: new Row(
              children: &lt;Widget&gt;[],
            ),
            actions: &lt;Widget&gt;[
              new FlatButton(
                  child: const Text('CANCEL'),
                  onPressed: () {
                    Navigator.pop(this.context);
                  }),
              new FlatButton(
                  child: const Text('ADD EXTRA'),
                  onPressed: () {
                    setState(() {
                      widget.onExtraAdded();
                    });
                    Navigator.pop(this.context);
                  }),
              //this is also for the modal
              new FlatButton(
                  child: const Text('REMOVE'),
                  onPressed: () {
                    setState(() {
                      widget.onDeleteClicked();
                    });
                    Navigator.pop(this.context);
                  })
            ],
          ),
        ),
      );
    }
  
    @override
    Widget build(BuildContext context) {
      return Container(
        child: Padding(
          padding: const EdgeInsets.only(bottom: 5, top: 5),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Expanded(
                child: Container(
                  width: 144,
                  height: 49,
                  decoration:
                      BoxDecoration(borderRadius: BorderRadius.circular(4)),
                  child: Center(
                    child: Padding(
                      padding: const EdgeInsets.only(left: 0),
                      child: TextField(
                        autofocus: true,
                        controller: nameController,
                        onChanged: (value) {
                          print(&quot;changing&quot;);
                          widget.name = value;
                          widget.onNameChanged();
                        },
                        scrollPadding: EdgeInsets.all(0),
                        decoration: InputDecoration(
                          focusedBorder: OutlineInputBorder(
                              borderSide: const BorderSide(
                                  width: 1, color: Color(0xff2E2E2E))),
                          isDense: true,
                          enabledBorder: OutlineInputBorder(
                              borderSide: const BorderSide(
                                  width: 1, color: Color(0xffDEDEDE))),
                        ),
                      ),
                    ),
                  ),
                ),
              ),
              SizedBox(
                width: 10,
              ),
              Expanded(
                child: Container(
                  width: 144,
                  height: 49,
                  decoration:
                      BoxDecoration(borderRadius: BorderRadius.circular(4)),
                  child: Center(
                    child: Padding(
                      padding: const EdgeInsets.only(left: 0),
                      child: TextField(
                        autofocus: true,
                        controller: priceController,
                        onChanged: (value) {
                          print(&quot;changing&quot;);
                          widget.price = int.parse(value);
                          widget.onPriceChanged();
                        },
                        scrollPadding: EdgeInsets.all(0),
                        decoration: InputDecoration(
                          hintText: &quot;\$&quot;,
                          hintStyle: TextStyle(
                              color: sankaraGreyColor,
                              fontFamily: &quot;Montserrat Medium&quot;,
                              fontSize: 15),
                          focusedBorder: OutlineInputBorder(
                              borderSide: const BorderSide(
                                  width: 1, color: Color(0xff2E2E2E))),
                          isDense: false,
                          enabledBorder: OutlineInputBorder(
                              borderSide: const BorderSide(
                                  width: 1, color: Color(0xffDEDEDE))),
                        ),
                      ),
                    ),
                  ),
                ),
              ),
              // widget.isLast
              //     ?
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 5),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    GestureDetector(
                      child: Container(
                        width: 20,
                        height: 20,
                        child: Center(
                          child: Text(&quot;+&quot;,
                              style: TextStyle(
                                  fontFamily: &quot;Montserrat Medium&quot;,
                                  fontSize: 15,
                                  color: sankaraGreyColor)),
                        ),
                        decoration: BoxDecoration(
                            shape: BoxShape.circle,
                            border: Border.all(color: Color(0xffDEDEDE))),
                      ),
                      onTap: () {
                        setState(() {
                          _showExtrasDialog();
                        });
                      },
                    ),
                    Padding(
                      padding: const EdgeInsets.only(top: 5),
                      child: Text(
                        &quot;Modify&quot;,
                        style: TextStyle(
                            fontFamily: &quot;Montserrat Medium&quot;, fontSize: 7),
                      ),
                    )
                  ],
                ),
              )
              // : Padding(
              //     padding: const EdgeInsets.symmetric(horizontal: 12),
              //     child: Column(
              //       mainAxisAlignment: MainAxisAlignment.start,
              //       crossAxisAlignment: CrossAxisAlignment.center,
              //       children: [
              //         Container(
              //           width: 20,
              //           height: 20,
              //         ),
              //       ],
              //     ),
              //   )
            ],
          ),
        ),
      );
    }
  }
  </code></pre>
  <p>This where the interactions happen</p>
  <pre><code>class ThirdPage extends StatefulWidget {
    Function(List&lt;ExtraItem&gt;) onExtrasChanged;
    Function(List&lt;ExtraItem&gt;) onDrinksChanged;
    Function(List&lt;ExtraItem&gt;) onSidesChanged;
    Function(int) onExtrasRemoved;
  
    ThirdPage(
        {this.onExtrasChanged,
        this.onDrinksChanged,
        this.onSidesChanged,
        this.onExtrasRemoved});
  
    @override
    _ThirdPageState createState() =&gt; _ThirdPageState();
  }
  
  class _ThirdPageState extends State&lt;ThirdPage&gt; {
    List&lt;ExtraItem&gt; extras = [];
    List&lt;ExtraItem&gt; drinks = [];
    List&lt;ExtraItem&gt; sides = [];
  
    void addDrinksItem() {
      print(&quot;adding drink&quot;);
      for (int i = 0; i &lt; drinks.length; i++) {
        setState(() {
          drinks[i].isLast = false;
        });
      }
      setState(() {
        drinks.add(ExtraItem(
          onExtraAdded: () {
            addDrinksItem();
          },
          onNameChanged: () {
            widget.onDrinksChanged(drinks);
  
            printDrinkValues();
          },
          onPriceChanged: () {
            widget.onDrinksChanged(drinks);
  
            printDrinkValues();
          },
          isLast: true,
          index: drinks.length,
        ));
      });
    }
  
  
  
    void _deleteItem(int index) {
      print(index);
      setState(() {
       extras.removeAt(index);
       });
      
    }
  
    @override
    void initState() {
      addExtraItem();
      addDrinksItem();
      addSidesItem();
      super.initState();
    }
  
    @override
    Widget build(BuildContext context) {
      return Column(
        children: [
          GestureDetector(
            onTap: () {
              print(extras[0].isLast);
            },
            child: Container(
              width: 380,
              height: 600,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(4),
                  border: Border.all(color: Color(0xffECECEC))),
              child: Padding(
                padding: const EdgeInsets.only(left: 20, right: 0, top: 30),
                child: ListView(
                  physics: NeverScrollableScrollPhysics(),
                  padding: EdgeInsets.symmetric(horizontal: 0),
                  shrinkWrap: true,
                  scrollDirection: Axis.vertical,
                  children: [
                    GestureDetector(
                      onTap: () {
                        printExtraValues();
                      },
                      child: Text(
                        &quot;Customize your menu&quot;,
                        style: TextStyle(
                            fontFamily: &quot;Montserrat Medium&quot;,
                            fontSize: 20,
                            color: Colors.black),
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.only(top: 5, bottom: 20),
                      child: Text(
                        &quot;Include any add-ons, if any&quot;,
                        style: TextStyle(
                            fontFamily: &quot;Montserrat Regular&quot;,
                            fontSize: 12,
                            color: sankaraGreyColor),
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.only(bottom: 10),
                      child: Text(
                        &quot;Mealbox item as extra&quot;,
                        style: TextStyle(
                            fontFamily: &quot;Montserrat Medium&quot;,
                            fontSize: 12,
                            color: Colors.black),
                      ),
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                          width: 144,
                          child: Text(
                            &quot;Name&quot;,
                            style: TextStyle(
                                fontFamily: &quot;Montserrat Medium&quot;,
                                fontSize: 10,
                                color: sankaraGreyColor),
                          ),
                        ),
                        SizedBox(
                          width: 16,
                        ),
                        Container(
                          width: 144,
                          child: Text(
                            &quot;Price Per Item&quot;,
                            style: TextStyle(
                                fontFamily: &quot;Montserrat Medium&quot;,
                                fontSize: 10,
                                color: sankaraGreyColor),
                          ),
                        ),
                      ],
                    ),
                    Container(
                      child: ListView.builder(
                        physics: NeverScrollableScrollPhysics(),
                        itemCount: extras.length,
                        scrollDirection: Axis.vertical,
                        shrinkWrap: true,
                        itemBuilder: (context, index) {
                          int ind = index;
                          final extra = extras[index];
                          return ExtraItem(
                            key: ObjectKey(extra),
                            extra: extras[index],
                            onExtraAdded: () =&gt; addExtraItem(),
                            onDeleteClicked: () =&gt; _deleteItem(ind),
                          );
                        },
                      ),
                    ),
  </code></pre>