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;
});
}
dart flutter
add a comment |
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;
});
}
dart flutter
add a comment |
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;
});
}
dart flutter
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
dart flutter
edited Nov 5 at 10:54
asked Nov 5 at 10:49
Govaadiyo
813321
813321
add a comment |
add a comment |
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),),
],
);
}
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
add a comment |
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);
});
}
}
add a comment |
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),),
],
);
}
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
add a comment |
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),),
],
);
}
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
add a comment |
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),),
],
);
}
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),),
],
);
}
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
add a comment |
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
add a comment |
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);
});
}
}
add a comment |
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);
});
}
}
add a comment |
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);
});
}
}
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);
});
}
}
answered Nov 7 at 11:07
Muldec
1586
1586
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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