Delete textView in listView

When press the Add button, it will add a TextField. When I click the minus button, I want the TextField get removed. But it keeps deleting the wrong TextField.

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  var commentList = List();
  TextEditingController _commentsController = TextEditingController();


  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Sample"),
        ),
        body: SingleChildScrollView(
            child: Padding(
                padding: EdgeInsets.all(10),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(children: [
                      Text("Comments", style: TextStyle(color: Colors.grey)),
                      SizedBox(width: 20),
                      Container(
                          height: 35,
                          padding: const EdgeInsets.all(10.0),
                          child: InkWell(
                              onTap: () {
                                setState(() {
                                  commentList.insert(
                                      0, _commentsController.text);
                                });
                              },
                              child: Row(children: [
                                Icon(
                                  Icons.add,
                                  color: Colors.white,
                                  size: 15,
                                ),
                                SizedBox(width: 5),
                                Text(
                                  "Add",
                                  style: TextStyle(color: Colors.white),
                                )
                              ])),
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(10.0),
                            color: const Color(0xff0000ff),
                          )),
                    ]),
                    SizedBox(height: 15),
                    Padding(
                        padding: EdgeInsets.all(10),
                        child: _showListViewComments()),
                  ],
                ))));
  }

  Widget _showListViewComments() {
    return ListView.builder(
        shrinkWrap: true,
        physics: NeverScrollableScrollPhysics(),
        itemCount: commentList.length + 1,
        itemBuilder: (BuildContext ctxt, int index) {
          if (index < commentList.length) {
            return Padding(
                padding: EdgeInsets.only(bottom: 10),
                child: Row(children: [
                  Expanded(
                      child: TextField(
                          maxLines: 3,
                          decoration: InputDecoration(
                            border: OutlineInputBorder(
                                borderRadius: const BorderRadius.all(
                                    const Radius.circular(5.0))),
                            contentPadding: EdgeInsets.all(10),
                          ))),
                  SizedBox(width: 10),
                  Container(
                      width: 30,
                      height: 30,
                      child: FloatingActionButton(
                        backgroundColor: Colors.red,
                        onPressed: () {
                          setState(() {
                            print(index);
                            commentList.removeAt(index);
                          });
                        },
                        child: Icon(
                          Icons.remove,
                          color: Colors.white,
                        ),
                      ))
                ]));
          }
        });
  }
}

Eg: I have text 1 and text 2. I want delete text 1 instead of text 2.

160221786641741846


Check stack link for better understanding

3 Likes

@Tony_Seng make use of key here.
below link will help you under stand it better.

3 Likes