【问题标题】:Dart - Flutter - How to assign a class object property to the color of a container widgetDart - Flutter - 如何将类对象属性分配给容器小部件的颜色
【发布时间】:2021-07-13 16:46:21
【问题描述】:

我是编程新手,我想为颤振容器小部件的颜色分配一个类属性。

Padding(
  padding: EdgeInsets.only(left: 15),
  child: Container(
    width: 4,
    height: 50,
    color: Colors.driver.color,
  ),
),

我尝试这样做,但结果显示错误

The getter 'driver' isn't defined for the type 'Colors'.

这是来自模型文件

color: 'blue[800]',

如果我在文本小部件中使用它,它会完美运行

Text(
  driver.points,
  style: TextStyle(
  fontSize: 16,
  fontWeight: FontWeight.bold
  ),
),

【问题讨论】:

  • driver.color 还不够吗?如果没有,您能否使用gist 分享模型文件内容。

标签: flutter dart colors containers


【解决方案1】:

Colors 是一个包含预定义颜色的类。此类没有名为 driver 的 getter 或方法。

我猜你在你的模型中存储了一个名为driver 的实例的颜色。因此应该是:

Padding(
  padding: EdgeInsets.only(left: 15),
  child: Container(
    width: 4,
    height: 50,
    color: driver.color,
  ),
),

【讨论】:

  • 它仍然给了我这个错误。参数类型“字符串”不能分配给参数类型“颜色?”。
  • 不应将字符串'blue[800]' 分配给drivercolor 属性,而应分配Color 的实例。例如Colors.blue.
  • 我试过了,但它仍然给了我同样的错误
  • 你能更新你的问题并发布模型文件的内容吗?
猜你喜欢
  • 2020-05-22
  • 2018-09-20
  • 2010-11-15
  • 2014-04-05
  • 2020-04-21
  • 2019-08-09
  • 2018-11-26
  • 1970-01-01
相关资源
最近更新 更多