0

我正在尝试制作一个收藏夹页面,它将标记收藏夹列表作为 ListTile() 并让用户禁用收藏夹。我使用 StreamController 来管理收藏夹列表,因为它在整个应用程序中共享。

所以我最喜欢的页面是这样的:

class FavoritesListPageBody extends StatelessWidget
{
    FavoritesListPageBody({Key? key}) : super(key: key);
    final _animatedListKey = new GlobalKey<AnimatedListState>();

    @override
    Widget build(BuildContext context)
    {
        final ScoresBloc bloc = BlocProvider.of<ScoresBloc>(context);
        
        return StreamBuilder<List<Score>>(
            stream: bloc.favorites,
            initialData: bloc.favoritesManager.favorites,
            builder: (BuildContext context, AsyncSnapshot<List<Score>> snapshot)
            {       
                if(bloc.favoritesManager.favorites.isEmpty)
                {
                    return Container(
                        constraints: const BoxConstraints.expand(),
                        color: Theme.of(context).colorScheme.favListEmptyBackground,
                        child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                                Icon(Icons.search_rounded, size: 120, color: Theme.of(context).colorScheme.noFavoriteFound),
                                const SizedBox(height: 50,),
                                Text("Aucun favori", style: TextStyle(color: Theme.of(context).colorScheme.noFavoriteFound)),
                            ],
                        )
                    );
                }

                
                for(Score fav in bloc.favoritesManager.favorites)
                {
                    this._animatedListKey.currentState?.insertItem(bloc.favoritesManager.favorites.indexOf(fav));
                }
                

                return AnimatedList(
                    key: this._animatedListKey,
                    initialItemCount: bloc.favoritesManager.favorites.length,
                    itemBuilder: (BuildContext context, int index, Animation<double> animation)
                    {
                        var curved = new CurvedAnimation(parent: animation, curve: const Interval(.5, 1));
                        
                        Score score = bloc.favoritesManager.favorites[index];

                        return SizeTransition(
                            sizeFactor: CurvedAnimation(parent: animation, curve: const Interval(0, .5)),
                            child: SlideTransition(
                                position: Tween(
                                    begin: const Offset(1, 0),
                                    end: const Offset(0, 0)
                                ).animate(curved),
                                child: Column(
                                    children: [
                                        ScoreTile(
                                            score: score,
                                            onPressed: ()
                                            {
                                                bloc.toggleFavorite(score);
                                                _animatedListKey.currentState!.removeItem(
                                                    index,
                                                    (context, animation) => Column(children: [
                                                        ScoreTile(score: score),
                                                        Divider(height: 1, color: Theme.of(context).colorScheme.separator)
                                                    ]),
                                                    duration: const Duration(seconds: 1)
                                                );
                                            },
                                        ),
                                        Divider(height: 1, color: Theme.of(context).colorScheme.separator)
                                    ]
                                )
                            )
                        );
                    },
                );
            }
        );
    }
}

无论我在哪里添加收藏夹都很好。如果我在除此页面之外的所有页面中都执行此操作,则删除收藏夹效果很好。上面的代码有效,但动画无效,因为我之前没有使用“insertItem”……这就像基本的 ListView 一样是“静态的”。

当我在构建 AnimatedList 时使用 insertItem(),然后尝试从此页面中删除收藏夹时,我收到此错误: RangeError (RangeError (index): Invalid value: Not in inclusive range 0..2: 3)

我想我不能同时使用 StreamBuilder() 和 insertItem()/removeItem() 来实现这一点,因为它会导致“索引”问题......

我怎么能这样做?

4

0 回答 0