Flutter 如何对可选参数使用非常量值
我正在尝试为我的Flutter 如何对可选参数使用非常量值,flutter,dart,Flutter,Dart,我正在尝试为我的FlutButton小部件创建一个自定义类,当我尝试使用方括号指定颜色属性时,出现以下错误: The default value of an optional parameter must be constant. 有没有办法在不将我的小部件转换为有状态小部件的情况下解决这个问题 提前谢谢。您可以使用初始值设定项列表以非常量值初始化最终实例字段: class CustomFlatButton扩展了无状态小部件{ 自定义平面按钮({ this.text='Default sign
FlutButton
小部件创建一个自定义类,当我尝试使用方括号指定颜色属性时,出现以下错误:
The default value of an optional parameter must be constant.
有没有办法在不将我的小部件转换为有状态小部件的情况下解决这个问题
提前谢谢。您可以使用初始值设定项列表以非常量值初始化最终实例字段:
class CustomFlatButton扩展了无状态小部件{
自定义平面按钮({
this.text='Default sign-in text',
this.color=Colors.white70,
颜色文本颜色,
}):textColor=textColor??Colors.grey[900];
最终字符串文本;
最终颜色;
最终颜色文本颜色;
}
在这种情况下,textColor=textColor??Colors.grey[900]
赋值对应于this.textColor
,右侧的textColor
引用构造函数参数。如果没有值传递给构造函数,则使用??
运算符使用默认值
你可以。
你可以。这能回答你的问题吗?非常有帮助,非常感谢。
class CustomFlatButton extends StatelessWidget {
final String text;
final Color color;
final Color textColor;
CustomFlatButton({
this.text='Default sign in text',
this.color = Colors.white70,
this.textColor = Colors.grey[900] // this is what is causing the error [900]
});