List Favorites not works very well

278 Views Asked by At

I created two ListViews with items in the first one and in the other one with items added in Favorite. It's working, but there are some things that don't work out the way I want them to. Items are not deleted when I click the UnFavorite button. When I go back to the first screen with all the items, the buttons are not refreshed and the button does not change to aFavorite button ('favorite_border') when I delete some favorite items. And when I don't have a favorite item, it doesn't display my text on my screen.

home.dart

class Home extends StatelessWidget {
  List<Item> savedItems = new List<Item>();
  @override
  Widget build(BuildContext context) {  
    
    return Scaffold(
        appBar: AppBar(
          title: Text('Home'),
          actions: [
            IconButton(
                icon: Icon(Icons.favorite_border),
                onPressed: () => pushToFavorite(context))
          ],
        ),
        body: ListView.builder(
            shrinkWrap: false,
            itemCount: itemData.length,
            itemBuilder: (context, index) {
              
              return Row(
                children: [
                  Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        TextWidget(
                            itemData[index].title, itemData[index].description),
                        FavWidget(savedItems, itemData[index]),
                      ],
                    ),
                ],
              );
            }));
  }

  Future pushToFavorite(BuildContext context) {
    return Navigator.of(context).push(MaterialPageRoute(
        builder: (context) => Favorite(favoriteItem: savedItems)));
  }
}

class Item {
  final String title;
  final String description;
  final String imageURL;
  final int countdown;
  final int id;
  final bool boole;

  Item({
    this.title,
    this.description,
    this.imageURL,
    this.countdown,
    this.id,
    this.boole,
  });
}

widgets.dart

...
class FavWidget extends StatefulWidget {
  final List<Object> savedItems;
  final Object item;

  FavWidget(this.savedItems, this.item);
  @override
  _FavWidgetState createState() => _FavWidgetState();
}
class _FavWidgetState extends State<FavWidget> { 
  @override
  Widget build(BuildContext context) {
    bool isSaved = widget.savedItems.contains(widget.item);
    return Padding(
      padding: const EdgeInsets.all(7.0),
      child: GestureDetector(
        child: Icon(
          isSaved ? Icons.favorite : Icons.favorite_border,
          color: isSaved ? Colors.red : null,
          size: 32,
        ),
        onTap: () {
          setState(() {
            if (isSaved) {
              widget.savedItems.remove(widget.item);
              isSaved = false;
            } else {
              widget.savedItems.add(widget.item);
              isSaved = true;
            }
          });
        },
      ),
    );
  }
}

favorite.dart

class Favorite extends StatelessWidget {
  
  final List<Item> favoriteItem;
  const Favorite({Key key, @required this.favoriteItem}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Favourite'),
      ),
      body: ListView.builder(
        itemCount: favoriteItem.length,
        itemBuilder: (context, index) {
          return favoriteItem.length > 0 ? Row(
                children: [
                  Flexible(
                    flex: 1,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        TextObject(
                            favoriteItem[index].title, favoriteItem[index].description),
                        FavWidget(favoriteItem, favoriteItem[index]),
                      ],
                    ),
                  )
                ],
              )
              : Center(child: Text('Ther\'s nothings', style: TextStyle(color: Colors.black)),);
        }),
    );
  }
}
1

There are 1 best solutions below

2
On

This why you need to use state management library like Provider , changing the data in one screen will not effect data in other screens. Using a package for state management will make life easier. It might be hard at the beginning, but you will never regret working with them and it is totally worth learning it.