Hasen
Hasen

Reputation: 12343

Dart make sure optional List argument in class is not null

The padding needs to be optional so needs to be 0 if I don't pass any padding argument through but it won't accept any value in constructor, unlike the bool bold argument.

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,
          ),
        ),
    );
  }
}

I can't seem to set QText(this.text, this.size, {this.align, this.bold = false, this.colour, this.font, this.padding = [0.0,0.0,0.0,0.0]]}); or anything like that to work as it throws the error Default values of an optional parameter must be constant. and I also can't set final List<double> padding ?? [0.0,0.0,0.0,0.0];

How can I give it a default value if I don't pass in an argument for it? Since it's supposed to be optional after all but the Padding widget also don't accept no padding.

Upvotes: 1

Views: 1807

Answers (1)

knezzz
knezzz

Reputation: 683

Did you try with:

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

Upvotes: 3

Related Questions