2

I have 4 buttons showing a different list each time a button is clicked . How to identify which button is clicked using flutter ?

Divyang Shah
  • 3,577
  • 5
  • 14
  • 27

1 Answers1

5

You can assign a callback that calls a different method for each button

new FlatButton(
  child: new Text(confirmText),
  onPressed: () => onOkPressed(),
),

or you can pass a parameter

new FlatButton(
  child: new Text(confirmText),
  onPressed: () => onButtonPressed('okButton'),
),
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • What about providing more information about what you try to accomplish before asking for better answers? – Günter Zöchbauer Dec 01 '17 at 07:34
  • Ex. If I have added button via for loop the how can I Identify which button is tap? any tag option is that ? – Govaadiyo Nov 05 '18 at 10:40
  • You can use the `for(var x, ...` variable in `onPressed: () => ...` – Günter Zöchbauer Nov 05 '18 at 10:53
  • Please check [https://stackoverflow.com/a/55956591/4930378](https://stackoverflow.com/a/55956591/4930378). The answer explains how you can assign ids to multiple buttons using custom button class and identify them. – Pro May 02 '19 at 16:29
  • @Pro looks like overkill to me. – Günter Zöchbauer May 02 '19 at 16:31
  • @GünterZöchbauer The solution you provided didn't work me for multiple dynamic FloatingActionButtons. – Pro May 02 '19 at 16:35
  • You answer should work fine for static buttons and thank you for providing that but if you're creating a list of buttons dynamically on each button actions, the button id (dynamic value based on the action) for all the buttons always appeared to be the last button id when using your answer. – Pro May 02 '19 at 16:47
  • 1
    I think I understand your use case now. – Günter Zöchbauer May 02 '19 at 16:52