Paul Cbt
Paul Cbt

Reputation: 143

Animated List not showing inserted item flutter

i have a SliverAnimatedList like this :

 SliverAnimatedList(
                  key: _myListkey,
                  itemBuilder: (context, index, animation) {
                    return Container(
                      child: Column(
                        children: [
                          FlashcardCreateTile(
                            autocreate: autocreate,
                            entertomovefocus: entertomovefocus,
                            flashcard: flashcards[index],
                            islast:
                                (index + 1) == flashcards.length ? true : false,
                            plusmode: true,
                            promode: true,
                            uid: widget.uid,
                            focus: null,
                            animation: animation,
                            formKey: _formkey,
                            delete: () {
                              flashcards.removeAt(index);
                              SliverAnimatedList.of(context).removeItem(
                                  index,
                                  (context, animation) => FlashcardCreateTile(
                                      autocreate: autocreate,
                                      entertomovefocus: entertomovefocus,
                                      flashcard:
                                          Flashcard(recto: "", verso: ""),
                                      islast: false,
                                      plusmode: true,
                                      promode: true,
                                      uid: widget.uid,
                                      focus: null,
                                      animation: animation,
                                      formKey: _formkey,
                                      delete: () {},
                                      add: () {}),
                                  duration: const Duration(milliseconds: 100));
                            },
                            add: () {
                              int insertitem = index + 1;
                              print(insertitem);
                              setState(() {
                                flashcards.insert(
                                    insertitem,
                                    Flashcard(
                                        recto: "",
                                        verso: "",
                                        mode: 0,
                                        isrelearning: false,
                                        easefactor: widget
                                            .folder
                                            .decklist[widget.deckindex]
                                            .startingEase,
                                        currentInterval:
                                            Duration(microseconds: 0),
                                        previousInterval:
                                            Duration(microseconds: 0)));
                                SliverAnimatedList.of(context)
                                    .insertItem(insertitem);
                                SliverAnimatedList.of(context).build(context);
                              });
                            },
                          ),
                          Container(
                            child: (index + 1) == flashcards.length
                                ? Container(
                                    child: SizedBox(
                                      height: 50,
                                    ),
                                  )
                                : Container(),
                          )
                        ],
                      ),
                    );
                  },
                  initialItemCount: flashcards.length,
                )

The flashcardcreatetile sends back the add function when i click and a button :

IconButton(
                  icon: Icon(
                    Icons.add,
                    color: Colors.red,
                  ),
                  onPressed: widget.add)

Here's what it's doing : enter image description here

As you can see, the item is indeed inserted, but the sliveranimatedlist only shows it when i scroll down and back up, so i presume it needs to rebuild itself..

I would like the new card to show directly, any ideas? Remove item is working fine by the way

Upvotes: 0

Views: 1553

Answers (2)

Michel Feinstein
Michel Feinstein

Reputation: 14266

You need to add a key to your item lists. I recommend you read this article as you will learn why do you need keys, what are they good for, and how to fix your problem.

Upvotes: 3

F Perroch
F Perroch

Reputation: 2215

You should add a key: Key(index) to your FlashcardCreateTile items to make them unique.

Flutter engine needs that to properly build your list

Upvotes: 0

Related Questions