本文介绍了色调不是Flutter中的恒定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,当您使用Colors.blue时,它将返回一个恒定的Color对象,但是如果您选择使用阴影(即Colors.blue [300]),则该对象不是恒定的.例如,当您具有采用可选Color参数的方法时,该方法的默认值必须为常数,这一点很重要.那么我们如何使颜色阴影保持不变?

When you use Colors.blue, for example this returns a constant Color object, but if you choose to use a shade instead, i.e. Colors.blue[300], then this object is NOT constant. This is important, for example, when you have a method that takes an optional Color parameter, whose default value must be constant. So how do we make a Color shade constant?

static const Color mainColor = Colors.blue \\All good!
static const Color shade = Colors.blue[400] \\ERROR: Const variables must be initialized with a constant value

推荐答案

不能.若要选择特定的阴影,应使用[]运算符,就像调用方法一样,并且由于方法返回的值在运行时会有所不同,因此从方法调用返回的值不能用作常量. /p>

You can't. To choose a specific shade, you should use the [] operator, which is just like calling a method, and since the value a method returns varies on run-time, the value returned from a method call cannot be use as constant.

如果您的情况如此简单,只需使用Colors.blue[400]的实际值,即Color(0xFF42A5F5).

If your situation is as simple as this one, just use the real value of Colors.blue[400], which is Color(0xFF42A5F5).

这篇关于色调不是Flutter中的恒定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 20:09