How to compare items in the gridview in flutter?

Viewed 44

I have been trying to but can't figure quite how to create a gridview displaying items from a online strore and to be able to compare between two items that have been selected from a checkbox. Does anyone how to do this?

2 Answers

You could overwrite the equality operator and hashcode for your GridView Item class.

Then you can just compare those items via a typical == operator. Remember, always override hashCode property when overriding the == operator.

There's also a common library that is used to simplify this called Equatable. The usage is pretty straight forward if you check the examples on pub.dev

I don't have access to the items from your online store, instead I used freely available data about Harry Potter Characters.

I used a gridview to display the data items. Once two items are selected a bottomsheet pops up showing the comparison.

Here is a gif of the working app, code can be found here.

I used flutter_bloc to implement the bloc pattern to this app. All the logic is managed in CharacterBloc which responds to three events

  1. Fetching data
  2. Selecting characters
  3. De-selecting characters
Related