Dart 确保类中的可选 List 参数不为空



padding必须是可选的,所以如果我不传递任何填充参数,则需要为 0,但它不接受构造函数中的任何值,这与布尔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]});

相关内容

  • 没有找到相关文章

最新更新