Simon Kwong
Simon Kwong

Reputation: 19

Passing a colour in list into a widget - Flutter

Quite possible an absurdly easy question but I've done a lot of beginner tutorials and this never seems to be covered, in below, both boardPosition.name and color are items in a data list. I can pass thru the name no problem, but the color doesn't work probably because it's dragging through the apostrophe. However, I can't seem to work out how else to do this...

Text(
    boardPosition.name,
    style: TextStyle(
        fontSize:18.0,
        fontFamily:'Amatic',
        color: boardPosition.color,
        )
    ),

Thanks in advance :)

Here's the board position:

class Board {

  String name;
  final color;
  int state;
  int myScore;
  int compScore;

  Board({this.name, this.color, this.state, this.myScore, this.compScore});
}

List <Board> boardPosition = [
  Board(name : "purple", color: "Colors.purple", state : 0, myScore: 0, compScore: 0),
  Board(name : "red", color: "Colors.red", state : 0, myScore: 0, compScore: 0),
  Board(name : "yellow", color: "Colors.yellow", state : 0, myScore: 0, compScore: 0),
  Board(name : "blue", color: "Colors.blue", state : 0, myScore: 0, compScore: 0),
  Board(name : "orange", color: "Colors.orange", state : 0, myScore: 0, compScore: 0),
  Board(name : "green", color: "Colors.green", state : 0, myScore: 0, compScore: 0),
  Board(name : "pink", color: "Colors.pink", state : 0, myScore: 0, compScore: 0),
];

Upvotes: 0

Views: 2029

Answers (1)

Dix20
Dix20

Reputation: 208

You have to set the color field to a Color Object not a String. Try:

List <Board> boardPosition = [
  Board(name : "purple", color: Colors.purple, state : 0, myScore: 0, compScore: 0),
  Board(name : "red", color: Colors.red, state : 0, myScore: 0, compScore: 0),
  Board(name : "yellow", color: Colors.yellow, state : 0, myScore: 0, compScore: 0),
  Board(name : "blue", color: Colors.blue, state : 0, myScore: 0, compScore: 0),
  Board(name : "orange", color: Colors.orange, state : 0, myScore: 0, compScore: 0),
  Board(name : "green", color: Colors.green, state : 0, myScore: 0, compScore: 0),
  Board(name : "pink", color: Colors.pink, state : 0, myScore: 0, compScore: 0),
];

Upvotes: 1

Related Questions