How to know which button is tapped in Flutter?











up vote
0
down vote

favorite












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;
});
}









share|improve this question




























    up vote
    0
    down vote

    favorite












    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;
    });
    }









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      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;
      });
      }









      share|improve this question















      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;
      });
      }






      dart flutter






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 5 at 10:54

























      asked Nov 5 at 10:49









      Govaadiyo

      813321




      813321
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          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),),
          ],
          );
          }





          share|improve this answer





















          • Pls check my updated. I know this way but any other way is available ?
            – Govaadiyo
            Nov 5 at 10:54










          • No, that's the only way.
            – Rémi Rousselet
            Nov 5 at 11:01


















          up vote
          1
          down vote













          I would suggest using the key property of the widget.



          Here is a solution:



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

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


          With the following custom Widget:



          typedef TagButtonPressedCallBack = void Function(Key key);

          class TagButton extends StatelessWidget {
          final TagButtonPressedCallBack onPressed;

          agButton({
          this.onPressed,
          }) : super(key: UniqueKey());

          @override
          Widget build(BuildContext context) {
          return RaisedButton(onPressed: () {
          if (onPressed != null) onPressed(key);
          });
          }
          }


          output:



          I/flutter ( 6371): pressed [#2ca50]
          I/flutter ( 6371): pressed [#2180d]
          I/flutter ( 6371): pressed [#2ca50]


          That solution gives you the unique identifier of the TagButton widget.





          If you want the id of the RaisedButton inside the TagButton, you can generate a Key in the build method and pass it to the RaisedButton like so :



          typedef TagButtonPressedCallBack = void Function(Key key);

          class TagButton extends StatelessWidget {
          final TagButtonPressedCallBack onPressed;

          TagButton({
          Key key,
          this.onPressed,
          }) : super(key: key);

          @override
          Widget build(BuildContext context) {
          var k = UniqueKey();
          return RaisedButton(
          key: k,
          onPressed: () {
          if (onPressed != null) onPressed(k);
          });
          }
          }





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53152881%2fhow-to-know-which-button-is-tapped-in-flutter%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            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),),
            ],
            );
            }





            share|improve this answer





















            • Pls check my updated. I know this way but any other way is available ?
              – Govaadiyo
              Nov 5 at 10:54










            • No, that's the only way.
              – Rémi Rousselet
              Nov 5 at 11:01















            up vote
            1
            down vote













            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),),
            ],
            );
            }





            share|improve this answer





















            • Pls check my updated. I know this way but any other way is available ?
              – Govaadiyo
              Nov 5 at 10:54










            • No, that's the only way.
              – Rémi Rousselet
              Nov 5 at 11:01













            up vote
            1
            down vote










            up vote
            1
            down vote









            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),),
            ],
            );
            }





            share|improve this answer












            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),),
            ],
            );
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 5 at 10:52









            Rémi Rousselet

            20.7k23068




            20.7k23068












            • Pls check my updated. I know this way but any other way is available ?
              – Govaadiyo
              Nov 5 at 10:54










            • No, that's the only way.
              – Rémi Rousselet
              Nov 5 at 11:01


















            • Pls check my updated. I know this way but any other way is available ?
              – Govaadiyo
              Nov 5 at 10:54










            • No, that's the only way.
              – Rémi Rousselet
              Nov 5 at 11:01
















            Pls check my updated. I know this way but any other way is available ?
            – Govaadiyo
            Nov 5 at 10:54




            Pls check my updated. I know this way but any other way is available ?
            – Govaadiyo
            Nov 5 at 10:54












            No, that's the only way.
            – Rémi Rousselet
            Nov 5 at 11:01




            No, that's the only way.
            – Rémi Rousselet
            Nov 5 at 11:01












            up vote
            1
            down vote













            I would suggest using the key property of the widget.



            Here is a solution:



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

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


            With the following custom Widget:



            typedef TagButtonPressedCallBack = void Function(Key key);

            class TagButton extends StatelessWidget {
            final TagButtonPressedCallBack onPressed;

            agButton({
            this.onPressed,
            }) : super(key: UniqueKey());

            @override
            Widget build(BuildContext context) {
            return RaisedButton(onPressed: () {
            if (onPressed != null) onPressed(key);
            });
            }
            }


            output:



            I/flutter ( 6371): pressed [#2ca50]
            I/flutter ( 6371): pressed [#2180d]
            I/flutter ( 6371): pressed [#2ca50]


            That solution gives you the unique identifier of the TagButton widget.





            If you want the id of the RaisedButton inside the TagButton, you can generate a Key in the build method and pass it to the RaisedButton like so :



            typedef TagButtonPressedCallBack = void Function(Key key);

            class TagButton extends StatelessWidget {
            final TagButtonPressedCallBack onPressed;

            TagButton({
            Key key,
            this.onPressed,
            }) : super(key: key);

            @override
            Widget build(BuildContext context) {
            var k = UniqueKey();
            return RaisedButton(
            key: k,
            onPressed: () {
            if (onPressed != null) onPressed(k);
            });
            }
            }





            share|improve this answer

























              up vote
              1
              down vote













              I would suggest using the key property of the widget.



              Here is a solution:



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

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


              With the following custom Widget:



              typedef TagButtonPressedCallBack = void Function(Key key);

              class TagButton extends StatelessWidget {
              final TagButtonPressedCallBack onPressed;

              agButton({
              this.onPressed,
              }) : super(key: UniqueKey());

              @override
              Widget build(BuildContext context) {
              return RaisedButton(onPressed: () {
              if (onPressed != null) onPressed(key);
              });
              }
              }


              output:



              I/flutter ( 6371): pressed [#2ca50]
              I/flutter ( 6371): pressed [#2180d]
              I/flutter ( 6371): pressed [#2ca50]


              That solution gives you the unique identifier of the TagButton widget.





              If you want the id of the RaisedButton inside the TagButton, you can generate a Key in the build method and pass it to the RaisedButton like so :



              typedef TagButtonPressedCallBack = void Function(Key key);

              class TagButton extends StatelessWidget {
              final TagButtonPressedCallBack onPressed;

              TagButton({
              Key key,
              this.onPressed,
              }) : super(key: key);

              @override
              Widget build(BuildContext context) {
              var k = UniqueKey();
              return RaisedButton(
              key: k,
              onPressed: () {
              if (onPressed != null) onPressed(k);
              });
              }
              }





              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                I would suggest using the key property of the widget.



                Here is a solution:



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

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


                With the following custom Widget:



                typedef TagButtonPressedCallBack = void Function(Key key);

                class TagButton extends StatelessWidget {
                final TagButtonPressedCallBack onPressed;

                agButton({
                this.onPressed,
                }) : super(key: UniqueKey());

                @override
                Widget build(BuildContext context) {
                return RaisedButton(onPressed: () {
                if (onPressed != null) onPressed(key);
                });
                }
                }


                output:



                I/flutter ( 6371): pressed [#2ca50]
                I/flutter ( 6371): pressed [#2180d]
                I/flutter ( 6371): pressed [#2ca50]


                That solution gives you the unique identifier of the TagButton widget.





                If you want the id of the RaisedButton inside the TagButton, you can generate a Key in the build method and pass it to the RaisedButton like so :



                typedef TagButtonPressedCallBack = void Function(Key key);

                class TagButton extends StatelessWidget {
                final TagButtonPressedCallBack onPressed;

                TagButton({
                Key key,
                this.onPressed,
                }) : super(key: key);

                @override
                Widget build(BuildContext context) {
                var k = UniqueKey();
                return RaisedButton(
                key: k,
                onPressed: () {
                if (onPressed != null) onPressed(k);
                });
                }
                }





                share|improve this answer












                I would suggest using the key property of the widget.



                Here is a solution:



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

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


                With the following custom Widget:



                typedef TagButtonPressedCallBack = void Function(Key key);

                class TagButton extends StatelessWidget {
                final TagButtonPressedCallBack onPressed;

                agButton({
                this.onPressed,
                }) : super(key: UniqueKey());

                @override
                Widget build(BuildContext context) {
                return RaisedButton(onPressed: () {
                if (onPressed != null) onPressed(key);
                });
                }
                }


                output:



                I/flutter ( 6371): pressed [#2ca50]
                I/flutter ( 6371): pressed [#2180d]
                I/flutter ( 6371): pressed [#2ca50]


                That solution gives you the unique identifier of the TagButton widget.





                If you want the id of the RaisedButton inside the TagButton, you can generate a Key in the build method and pass it to the RaisedButton like so :



                typedef TagButtonPressedCallBack = void Function(Key key);

                class TagButton extends StatelessWidget {
                final TagButtonPressedCallBack onPressed;

                TagButton({
                Key key,
                this.onPressed,
                }) : super(key: key);

                @override
                Widget build(BuildContext context) {
                var k = UniqueKey();
                return RaisedButton(
                key: k,
                onPressed: () {
                if (onPressed != null) onPressed(k);
                });
                }
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 7 at 11:07









                Muldec

                1586




                1586






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53152881%2fhow-to-know-which-button-is-tapped-in-flutter%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini