title: How to pass variable content to a parameter value in dart/flutter?
body: <p>I may be missing something obvious, but I'm having trouble passing a variable to a parameter or widget in flutter/dart. For example, let's say I have some variable:</p>
  
  <p><code>String col = 'red';</code></p>
  
  <p>and I want to pass this to a color parameter to get the equivalent of </p>
  
  <pre><code>color: Colors.red
  </code></pre>
  
  <p>The difficult thing is that any way I try to pass the value ends up passing a string (which isn't accepted), including trying to pass just the value of <code>col</code> directly or trying to build a function that returns <code>Colors.col</code>. </p>
  
  <p>I think what I need is something like a function like</p>
  
  <pre><code>setColor(String str) {
      return Colors.str;
    }
  </code></pre>
  
  <p>but, as you might expect, this returns "The getter 'str' isn't defined for the type 'Colors'." (And similarly for:</p>
  
  <pre><code>setColor(String str) {
      return Colors.$str;
  }
  </code></pre>
  
  <p>I know one option is to create a function using a bunch of if's, like</p>
  
  <pre><code>setColor(String str) {
   if (str==red) return Colors.red;
   if (str==blue) return Colors.blue;
   if (str==green) return Colors.green;
  etc.
  }
  </code></pre>
  
  <p>but I'd prefer a more elegant option if one is available. </p>
  
  <p>EDIT: It looks like this isn't quite as easy as I'd hoped (see answers and comments to answers below). </p>