How to know which button is tapped in Flutter?

557 views dart
5

I came across this question : How to identify which button is clicked in flutter

But Is there any better way to detect which button is tapped? Ex. I created 100 button via for loop then how to know? In iOS we are using tag property so If there is this kind of option then it will really handy to detect.

EDITED:

Below are my code

List<Widget> pageCurrentPageIndicator(int currentIndex, int totoalCount) {
    List<Widget> tempWidget = new List<Widget>();
    for (var i = 0; i < totoalCount; i++) {
      Container container = Container(
        width: 47.0,
        height: 30.0,
        child: FlatButton(
          child: Image.asset(
            (i == currentIndex
                ? 'lib/assets/radioBtnActive.png'
                : 'lib/assets/radioBtn.png'),
            fit: BoxFit.contain), onPressed: () {
              whichButtonistaped(i);              
            },
          )
      );
      tempWidget.add(container);
    }
    return tempWidget;
  }

  void whichButtonistaped(int btnTag){
    print(btnTag);
    setState(() {
        currentBannerIndex = btnTag;               
     });
  }

answered question

1 Answer

1

Assign to each button a different callback

void onPress(int id) {
  print('pressed $id');
}

Widget build(BuildContext context) {
  return Row(
    children: [
      RaisedButton(onPressed: () => onPress(0),),
      RaisedButton(onPressed: () => onPress(1),),
    ],
  );
}

posted this

Have an answer?

JD

Please login first before posting an answer.