When developing app with flutter, i want to define some common styles.

The code is as follows:

import 'package:flutter/material.dart';
class AppStyle {
  static Color colorRed = const Color(0xffe04f5f);
  static Color colorWhite = const Color(0xffffffff);
  static Color colorGreen = const Color(0xff1abc9c);
}

Now, i want to define a new style.

static TextStyle listRowTitle = const TextStyle(fontSize: 20.0, color: colorGreen);

If you write to the above, then colorGreen will have a problem here. The wrong message is

[dart] Invalid constant value.
[dart] Arguments of a constant creation must be constant expressions.
Color colorGreen

If you change colorGreen to Color (0xff1abc9c), there is no problem!

static TextStyle listRowTitle = const TextStyle(fontSize: 20.0, color: Color(0xff1abc9c));

Ask me to teach me,please!


Solution 1: Blasanka

Since, colors are defined in a class you have to do something like below:

AppStyle.colorGreen

Udate:

Ohh, I see, you are using cont TextStyle. So, you can remove const or add const for your AppStyle.

I simply removed the const from TextStyle:

TextStyle(fontSize: 20.0, color: AppStyle.colorGreen)

Understand how const works.


Solution 2: egidiocs

The problem is you are declaring a variable value (colorGreen) to a property (color) of a constant widget. Constant widgets cannot vary, change.

Just remove the modifier const from your widget

static TextStyle listRowTitle = const TextStyle(fontSize: 20.0, color: colorGreen);