Home > Software engineering >  Title name of my new card changes all the other cards' name to the new title name
Title name of my new card changes all the other cards' name to the new title name

Time:01-24

I have a listview which displays cards on but whenever I add a new card, the title of all the cards is updated to the new title but I need just the current added one to change

final taskName = TextEditingController();

 TextField(
                  controller: taskName,
                  decoration: const InputDecoration(hintText: 'Task Name'),
                ),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  ElevatedButton(
                    onPressed: () {
                      counter  ;
                      Navigator.pop(context);
                    },
                    child: const Text(
                      'Add',
                      style: TextStyle(fontSize: 15),
                    ),
                  ),

Expanded(
            child: ListView.builder(
                itemCount: counter,
                itemBuilder: (BuildContext context, index) {
                  return TodoContainer(
                    cardTitle: taskName.text.toString(),
                  );
                }),
          )

ScreenShot to show how my cards look like after adding a new card with name of TODO

ScreenShot to show how my cards look like after adding a new card with name of TODO

CodePudding user response:

You are using a single TextEditingController value to populate the ListView, therefore listview children is getting update value instead of creating new item.

final taskName = TextEditingController();

What you can do, create a list of String and feed it on children.

  List<String> items = []; // on state class

 .... 
 // add data 
  onPressed: () {
    items.add(taskName.text.toString()); 
     ....
      },

......

/// populate listView
 return TodoContainer(
         cardTitle: items[index],
             );

  •  Tags:  
  • Related