Reputation: 509
after get data from API output is:L,P is attendance_flag.if click on L it toggle to P and if i click on P it will toggle to A
but i want this :it toggle attendance_flag but after some time it will look like first image
how to change L to P or P to A on click this is my code:
child: RaisedButton(
//color: pressed ? Colors.green : Colors.red,
color: (snapshot.data[index]['attendance_flag'] == 'P')
? Colors.green
: (snapshot.data[index]['attendance_flag'] == 'A')
? Colors.redAccent
: (snapshot.data[index]['attendance_flag'] == 'L')
? Colors.orange
: Colors.grey,
onPressed: () {
setState(() {
_buttonEnabled = !_buttonEnabled;
_id = index;
if (snapshot.data[index]['attendance_flag'] == 'P') {
setState(() {
_buttonEnabled = false;
snapshot.data[index]['attendance_flag'] = 'A';
});
} else if (snapshot.data[index]['attendance_flag'] == 'A') {
_buttonEnabled = true;
snapshot.data[index]['attendance_flag'] = 'P';
}
else if (snapshot.data[index]['attendance_flag'] == 'L') {
_buttonEnabled = false;
_asyncInputDialog(context);
snapshot.data[index]['attendance_flag'] = 'L';
}
});
print("You clicked item number $_id");
},
child: Text(_buttonEnabled == true
? snapshot.data[index]['attendance_flag']
: _buttonEnabled == false ? snapshot
.data[index]['attendance_flag'] : 'P')
),
Upvotes: 0
Views: 4195
Reputation: 445
You can do this by declaring the button in a stateful widget & initially you have to declare a String variable storing "L" in the state class. Then, inside the state class, implement button's method onPressed. Inside onPressed, implement setState method and inside it, change the String variable to "P", which was initially "L". You can write if else clauses inside the setState method to change the text to your suitable form like in your case you want to change "L" to "P", & "P" to "A".
It would be something like this:
class YourWidgetState extends State<YourWidget> {
String buttonText = "L";
Widget build(BuildContext context) {
return FlatButton(
child: Text(buttonText),
onPressed: () {
setState(() {
if (buttonText == "L") {
buttonText = "P";
} else if (buttonText == "P") {
buttonText = "A";
}
});
});
}
}
Else you can also use ChangeNotifier for the String buttonText & change the button text from anywhere in your app.
Upvotes: 1