Reputation: 330
i need to change background color of dynamic raised button when it is pressed?
List<String> wordList=[i,r,o,n,m,a,n];
SizedBox(
width: double.infinity,
height: 300,
child: GridView.count(
crossAxisCount: 3,
childAspectRatio: 2.5,
padding: const EdgeInsets.all(10.0),
mainAxisSpacing: 15.0,
crossAxisSpacing: 15.0,
children: wordList.map((String data) {
return RaisedButton(
color:Colors.blueAccent,
child: Text(data),
onPressed: () {
print(data);
setState(() {
print("btnPressed ${btnPressed}");
// listBool.insert( , true);
print("data $data");
buttonPressed(data, btnPressed);
});
},
);
}).toList(),
))[![enter image description here][1]][1]
Upvotes: 0
Views: 153
Reputation: 614
The following works for me
List<String> wordList = ['i', 'r', 'o', 'n', 'm', 'a', 'n'];
List<Color> colorList;
@override
void initState() {
super.initState();
colorList = List(wordList.length);
colorList.fillRange(0, wordList.length, Colors.blueAccent);
}
SizedBox(
width: double.infinity,
height: 300,
child: GridView.count(
crossAxisCount: 3,
childAspectRatio: 2.5,
padding: const EdgeInsets.all(10.0),
mainAxisSpacing: 15.0,
crossAxisSpacing: 15.0,
children: [
for (int i = 0; i < wordList.length; i++)
RaisedButton(
color: colorList[i],
child: Text(wordList[i]),
onPressed: () {
print(wordList[i]);
setState(() {
colorList[i] = Colors.black;
// listBool.insert( , true);
});
},
)
],
),
),
Upvotes: 2
Reputation: 637
Try creating a Stateful Widget for your RaisedButton(s):
in your onPressed method, change the color of the button to another color:
setState(() { buttonColor = Colors.amber; });
That should work.
Upvotes: 0