飞镖,颤振;从 MaterialStateProperty 获取颜色很热

时间:2021-06-30 13:47:08

标签: flutter dart themes

Theme 对象中的某些值必须作为 MaterialStateProperty<T> 提供,例如backgroundColor: MaterialStateProperty.all<Color>(Colors.white)。如何在 Flutter 小部件中访问 T 类型的属性?

例如以下行产生一个(明显的)错误:

Container(color: Theme.of(context).elevatedButtonTheme.style?.backgroundColor)
Error: *The argument type 'MaterialStateProperty<Color?>?' can't be assigned to the parameter type 'Color'*

1 个答案:

答案 0 :(得分:1)

你应该试试

Container(color: Theme.of(context).elevatedButtonTheme.style!.backgroundColor!.resolve(Set.of[MaterialState.pressed]))

但是你要确保这个主题有背景颜色,否则你会得到“意外的空值”异常。