gpt4 book ai didi

class - Dart 确保类中的可选列表参数不为空

转载 作者:IT王子 更新时间:2023-10-29 07:10:51 24 4
gpt4 key购买 nike

padding需要是可选的,所以如果我不传递任何填充参数,则需要为 0,但它不会接受构造函数中的任何值,这与 bool bold 不同。争论。

class QText extends StatelessWidget {
QText(this.text, this.size,
{this.align, this.bold = false, this.colour, this.font, this.padding});

final String text;
final double size;
final bool bold;
final Color colour;
final String font;
final TextAlign align;
final List<double> padding;

@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.fromLTRB(padding[0], padding[1],
padding[2], padding[3]),
child: Text(
text,
textAlign: align,
style: TextStyle(
fontSize: size,
fontWeight: (bold) ? FontWeight.bold : FontWeight.normal,
fontFamily: (font != '') ? font : '',
color: colour,
),
),
);
}
}

我似乎无法设置 QText(this.text, this.size,
{this.align, this.bold = false, this.colour, this.font, this.padding = [0.0,0.0,0.0,0.0]]});
或任何类似的东西,因为它会抛出错误 Default values of an optional parameter must be constant.而且我也无法设置 final List<double> padding ?? [0.0,0.0,0.0,0.0];

如果我不为它传递参数,我怎么能给它一个默认值呢?因为它毕竟应该是可选的,但 Padding小部件也不接受无填充。

最佳答案

你试过吗:

QText(this.text, this.size, {this.align, this.bold = false, this.colour, this.font, this.padding = const <double>[0, 0, 0, 0]});

关于class - Dart 确保类中的可选列表参数不为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57201716/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com