DEV Community

Discussion on: Using Selector in Provider

Collapse
 
devkhalyd profile image
Rolando

Hi, first that all, I'm from Mexico, sorry for my bad English.

Selector<MenuSpotifyNotifier, Tuple2<int, String>>(
        selector: (_, d) => Tuple2(d.currentPos, d.titles[pos]),
        builder: (_, d, __) {
          int cPos = d.item1;
          String textIcon = d.item2;
          FaIcon icon = disabledIcon;
          bool isSelected = cPos == pos;
          if (isSelected) icon = enabledIcon;

          Log.console('Rebuild the UI in $textIcon');

          return Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              IconButton(
                icon: icon,
                onPressed: () =>
                    Provider.of<MenuSpotifyNotifier>(context, listen: false)
                        .setCurrentPos(pos),
              ),
              Text(
                textIcon,
                style: TextStyle(
                    color: isSelected ? Colors.white : Colors.grey,
                    fontSize: 11.5),
              )
            ],
          );
        },
      );
Enter fullscreen mode Exit fullscreen mode

This code is for change the state when an item is pressed so the Log.console method show me when a item is rebuild. What I expect? If the item 0 is pressed this only should be rebuild, but that is not the case. All the items are rebuild.

Here an example:

dev-to-uploads.s3.amazonaws.com/i/...

Am I doing something wrong? Or Is this the behavior expected?

Collapse
 
aseemwangoo profile image
aseem wangoo

For the selector, theoratically it should change only if d.currentPos or d.titles[pos] changes