如何使用 class 中的变量值来设置同一 class 中另一个变量的值

How can I use the value of a variable in a class to set the value of another variable in the same class

下面的代码不起作用,它显示了我想要实现的目标。

如您所见,我想将 text 设置为使用 color 设置的颜色。

class AppThemeModal{

    AppThemeModal({required this.color});

    Color color;

    TextStyle text = TextStyle(color: color);
}

您不能在创建对象之前访问实例变量,但是您可以引用初始化列表中的参数:

class AppThemeModal{
  AppThemeModal({required this.color})
      : text = TextStyle(color: color);

  final Color color;
  final TextStyle text;
}