So I am currently trying to figure out a way to give a color to a certain card depending on which subject the user has selected. I am usifier a notifier. Previously in my code, I was able to refer to look for a precise image that would be related to the topic that the user choose and it worked using this code here:

@override
Widget build(BuildContext context) {
   return Consumer<FlashcardsNotifier>(
      builder: (_, notifier,__)=> Scaffold(
        appBar: AppBar(
          actions: <Widget>[
            Padding(padding: const EdgeInsets.all(8.0),
            child: Hero(
              tag: notifier.topic,
              child: Image.asset("images/${notifier.topic}.png"))),
          ],
          title: Text(notifier.topic, style: TextStyle(color: Color.fromARGB(248, 70, 65, 65)),)
          ,
        ),
        body: IgnorePointer(
          ignoring: notifier.ingnoreTouches,
          child: 
          Stack(
            children: [
              Card2(),
              Card1(),
            ],
          ),
        ),
        ),
      //),
    );
  }
}

However if I try to do the same thing with the Color Widget it is not working because I think it is not able to use a String.

child: Container(
         width: size.width *0.80,
         height: size.height*0.60,
         decoration: BoxDecoration(
           color: Colors.white,
           border: Border.all(
             color: {notifier.topic};       
           ),
           boxShadow: [
             BoxShadow(
               color: Market.withOpacity(0.3),
               spreadRadius: 5,
               blurRadius: 7,
               offset: Offset(0, 3), // changes position of shadow
             ),
           ], 
         ),
       ),

I created constants of colors with the same name as the topics but my notifier.topic.

const Color Market = Color.fromARGB(255, 215, 165, 30);
const Color School = Color.fromARGB(255, 215, 117, 11);
const Color Farm = Color.fromARGB(255, 65, 106, 208);`your text`
const Color Garden = Color.fromARGB(255, 29, 136, 38);
const Color House = Color.fromARGB(255, 197, 53, 53);
const Color Temple = Color.fromARGB(255, 103, 32, 91);

Can anyone help it would be really appreciated.

I am hoping that someone can help me solve this issue.

2

There are 2 best solutions below

0
On

This is how you define colors

final Color Market = const Color.fromARGB(255, 215, 165, 30);
0
On

Your topic variable is a String, and the color property of the Border.all is the type Color. Either you have to change topic to be of type Color or create a new variable that is the correct type. If you must convert a String to a Color, then you can create a simple function to do so:

Color getColorFromString(final String colorName) {
  switch (colorName) {
    case 'Market':
      return Market;
    case 'School':
      return School;
    case 'Farm':
      return Farm;
    case 'Garden':
      return Garden;
    case 'House':
      return House;
    default:
      return Temple;
  }
}

And then do border: Border.all(color: getColorFromString(notifier.topic))