如何为onPressed的每个按钮分配特定的值?

时间:2020-02-29 01:55:22

标签: flutter dart

我连续有5个按钮,我想为每个按钮分配一个值。另外,如果获胜,我想显示按钮的相同值,如果输掉,我想显示按钮的时间times(*)2。

如何为onPressed的每个按钮分配特定的值?

谢谢

int bab = 5;
  int o = 0; // output 

  void Scorebab() {
    setState(() {
      o = o + bab;
    });
  }

5个不同按钮的示例

Container(
  height: 80,
  width: 80,
  padding: EdgeInsets.all(3.0),
  child: MaterialButton(
    shape: CircleBorder(
        side: BorderSide(
            color: Colors.black,
            width: 1.0,
            style: BorderStyle.solid)),
    color: selectedTalba == Talba.bab
        ? kActiveCardColor
        : kInactiveCardColor,
    onPressed: () {
      Scorebab();
      setState(() {
        selectedTalba = Talba.bab;
      });
    },
    child: Text(
      'bab',
      style: kLabelTextStyle,
    ),
  ),
),


得分输出

Expanded(
  child: ReusableCard(
    onPress: () {
      setState(() {
        selectedShary = Shary.Lihum;
      });
    },
    color: kInactiveCardColor,
    cardChild: CardChild(
      label: '$o ',
    ),
  ),
),

输赢按钮

Expanded(
  child: ReusableCard(
    onPress: () {


      setState(() {
        selectedScore = Score.Lose;
      });
    },
    color: selectedScore == Score.Lose
        ? kActiveCardColor
        : kInactiveCardColor,
    cardChild: CardChild(
      label: 'lose',
    ),
  ),
),
Expanded(
  child: ReusableCard(
    onPress: () {
      setState(() {

        selectedScore = Score.Win;
      });
    },
    color: selectedScore == Score.Win
        ? kActiveCardColor
        : kInactiveCardColor,
    cardChild: CardChild(label: 'win!'),
  ),
),

1 个答案:

答案 0 :(得分:0)

据我了解!

str

Screenshot