从其他列表中获取项目的索引

时间:2020-12-19 21:39:43

标签: flutter dart provider

我开始使用提供程序,但我遇到了问题。我想获取其他屏幕中其他列表中项目的索引。我怎样才能得到它们?我有两个屏幕:一个主屏幕和一个最喜欢的屏幕,每个屏幕都有一个 listView。当它从收藏夹屏幕中删除时,我想在主屏幕中获取该项目的索引。这是我在 GitHub 上的代码链接:https://github.com/Rianou20/my_app_from_scratch/tree/master/my_app_from_scratch。以及我的代码的一些相关部分:

favModel.dart

class FavModel extends ChangeNotifier {
  List<Item> favList = [];
  List<bool> isInFav = [];
  

  addInFavorite(title, description, index){
    Item item = Item(title: title, description: description, );
    favList.add(item);
    isInFav[index] = true;
    notifyListeners();
  }

  removeOfFavorite(int index, int index2){
    favList.removeAt(index);
    isInFav[index2] = false;
    notifyListeners();
  }

  implement(){
    isInFav.add(false);
  }
}

favorite_screen.dart

class Favorite extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Favorite'),
      ),
      body: Consumer<FavModel>(
        builder: (context, favModel, child) {
          return ListView.builder(
              itemCount: favModel.favList.length,
              itemBuilder: (context, index) {
                return TextObject(favModel.favList[index].title,
                                    favModel.favList[index].description),
                                Padding(
                                  padding: const EdgeInsets.all(7.0),
                                  child: GestureDetector(
                                      child: Icon(
                                        Icons.favorite,
                                        color: Colors.red,
                                        size: 32,
                                      ),
                                      onTap: () {
                                        favModel.removeOfFavorite(index, index);
                                      }),
                                ),
           });
        },
      ),
    );
  }
}

home_screen.dart

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home'),
        actions: [
          IconButton(
            icon: Icon(Icons.favorite_border),
            onPressed: () => Navigator.push(
              context,
              MaterialPageRoute(
                fullscreenDialog: true,
                builder: (context) {
                  return Favorite();
                },
              ),
            ),
          ),
        ],
      ),
      body: Consumer<FavModel>(builder: (context, favModel, child) {
        return ListView.builder(
            shrinkWrap: false,
            itemCount: itemData.length,
            itemBuilder: (context, index) {
              favModel.implement();
              return TextObject(
                 itemData[index].title, itemData[index].description),
                        Padding(
                          padding: const EdgeInsets.all(7.0),
                          child: GestureDetector(
                              child: Icon(
                                favModel.isInFav.elementAt(index)
                                    ? Icons.favorite
                                    : Icons.favorite_border,
                                color:
                                    favModel.isInFav[index] ? Colors.red : null,
                                size: 32,
                              ),
                              onTap: () {
                                favModel.isInFav[index]
                                    ? null
                                    : Provider.of<FavModel>(context,
                                            listen: false)
                                        .addInFavorite(
                                        itemData[index].title,
                                        itemData[index].description,
                                        index,
                                      );
                              }),
              );
            });
      }),
    );
  }
}

我想要获取索引的地方是在此行的 favorite_screen.dart favModel.removeOfFavorite(index, index);

1 个答案:

答案 0 :(得分:0)

在不知道确切用例的情况下,您可能会将删除的值存储在列表中并在主屏幕上使用它们。

class FavModel extends ChangeNotifier {
  List<Item> favList = [];
  List<bool> isInFav = [];
  List<int> _removedItemIndexList = []
  
  get removedItemIndexList => _removedItemIndexList; 
  
  addInFavorite(title, description, countdown, imageURL, index){
    Item item = Item(title: title, description: description, countdown:countdown, imageURL: imageURL);
    favList.add(item);
    isInFav[index] = true;
    notifyListeners();
  }

  removeOfFavorite(int index, int index2){
    favList.removeAt(index);
    isInFav[index2] = false;
    _addToRemovedIndexList(index); 
    notifyListeners();
  }

  void _addToRemovedIndexList(int index) {
    _removedItemIndexList.add(index);
  }

  implement(){
    isInFav.add(false);
  }
}

然后在 home_sreen.dart 上使用

...
body: Consumer<FavModel>(builder: (context, favModel, child) {
            List<int> removedIndexes = favModel.removedItemIndexList;
            return ListView.builder( ... ) }; 

请注意,FavModel 提供程序类必须高于小部件树上的 home_screen.dart 才能访问其值。即你想在你的 ma​​in.dart

中做这样的事情
...
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
        providers: [
          ChangeNotifierProvider.value(
            value: FavModel(),
          ),
        ],
        child: MaterialApp(...