フラッターマルチアクション

Oct 14 2020

トグルのように機能する「いいね」ボタンを作りたいのですが、タップ操作で好きになるように色が変わり、もう一度タップすると嫌いになります。どうすればそれを達成できますか?

回答

6 Akif Oct 14 2020 at 14:21

次のような簡単なボタンを使用できます。

IconButton(
   onPressed: () {
       setState(() {
           _isLiked = !_isLiked;
        });
       }
      },
   icon: Icon(Constants.crownIcon, 
       color: _isLiked
              ? Constants.orangeColor 
              : Constants.ligthGreyColor, 
     size: 15.0,
    ),
  ),
1 Yeldar.N Oct 14 2020 at 14:18

ToggleButtonsを使用できます

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