I have a main dart GridView as follows:
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
GridView.count(crossAxisCount: 2, shrinkWrap: true, children: [
BuildButtonWithText(Icons.volume_down, "Vol Down", "VOL_DN")
.customButtonWithText(),
BuildButtonWithText(Icons.volume_up, "Vol Up", "VOL_UP")
.customButtonWithText(),
BuildButtonWithText(Icons.play_arrow, "Play/Pause", "PLAY")
.customButtonWithText(),
BuildButtonWithText(Icons.volume_mute, "Mute", "VOL_MUTE")
.customButtonWithText(),
]),
TextField(controller: status_text),
],
)),
The corresponding class that does the button creation is as follows:
class BuildButtonWithText {
List<Column> _gridButtons = <Column>[];
IconData _iconName;
String _buttonText;
String _buttonID;
BuildButtonWithText(IconData iconName, String buttonText, String buttonID) {
this._iconName = iconName;
this._buttonText = buttonText;
this._buttonID = buttonID;
}
String get _getButtonText {
return this._buttonText;
}
String get _getButtonID {
return this._buttonID;
}
set _setButtonText(String newButtonText) {
this._buttonText = newButtonText;
}
void buttonPressHandler(String buttonID) {
log('ButtonPressed: $buttonID');
if (buttonID.compareTo("PLAY") == 0) {
this._setButtonText = "PAUSE";
}
if (buttonID.compareTo("PAUSE") == 0) {
this._setButtonText = "PLAY";
}
}
Column customButtonWithText() {
Column _button = Column(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
icon: Icon(this._iconName),
onPressed: () {
buttonPressHandler(this._buttonID);
},
),
Text(_buttonText)
],
);
_gridButtons.add(_button);
return _button;
}
}
Given that I am very new to Dart/Flutter, what I would like to do is,
- in
buttonPressHandler
, search for a particularColumn
item from the_gridButtons
list by the_buttonID
value. I looked into the.firstWhere
function, but I am unable to access that particular variable. - once I have a reference to said
Column
, I would like to modify theText
value as required.
The above code compiles and runs, but there appears to be no change in the text value.