【问题标题】:Animated List not showing inserted item flutter动画列表不显示插入的项目颤动
【发布时间】:2021-02-20 06:43:27
【问题描述】:

我有一个像这样的 SliverAnimatedList:

 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,
                )

当我点击一个按钮时,flashcardcreatetile 会发回添加功能:

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

这是它的作用:

如您所见,该项目确实已插入,但 sliveranimatedlist 仅在我向下滚动和向上滚动时显示它,所以我认为它需要重建自身..

我想直接显示新卡,有什么想法吗?顺便说一句,删除项目工作正常

【问题讨论】:

    标签: flutter flutter-layout flutter-animation


    【解决方案1】:

    您需要在您的项目列表中添加一个密钥。我建议您阅读this article,因为您将了解为什么需要密钥、它们有什么用处以及如何解决您的问题。

    【讨论】:

    • 谢谢!我不知道你有一个 UniqueKey() 的东西,非常有用!
    • 正如文章指出的那样,请小心他们;)
    【解决方案2】:

    您应该将key: Key(index) 添加到您的FlashcardCreateTile 项目以使其独一无二。

    Flutter 引擎需要它来正确构建您的列表

    【讨论】:

    • 谢谢!我该怎么做?
    猜你喜欢
    • 2022-11-26
    • 1970-01-01
    • 2013-06-03
    • 2019-10-30
    • 2018-06-03
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多