Dart,Flutter; 如何从MaterialStateProperty中获取颜色

3

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个回答

3

你应该尝试

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

但是你需要确保这个主题有backgroundColor,否则将会出现“意外的空值”异常。


3
elevatedButtonTheme.style!.backgroundColor!.resolve({MaterialState.pressed})) - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接