sungchan
sungchan

Reputation: 79

Flutter multiple action

I want to make a like button, which works like toggle, the color will change to make it liked by tap action, and tap it again to dislike it. how can I achieve that?

Upvotes: 1

Views: 1062

Answers (2)

Akif
Akif

Reputation: 7640

You can use a simple button like this:

IconButton(
   onPressed: () {
       setState(() {
           _isLiked = !_isLiked;
        });
       }
      },
   icon: Icon(Constants.crownIcon, 
       color: _isLiked
              ? Constants.orangeColor 
              : Constants.ligthGreyColor, 
     size: 15.0,
    ),
  ),

Upvotes: 6

Yeldar Nurpeissov
Yeldar Nurpeissov

Reputation: 2296

You can use ToggleButtons

ToggleButtons(
  children: <Widget>[
    Icon(Icons.ac_unit),
    Icon(Icons.call),
    Icon(Icons.cake),
  ],
  onPressed: (int index) {
    setState(() {
      isSelected[index] = !isSelected[index];
    });
  },
  isSelected: isSelected,
),

Upvotes: 1

Related Questions