Home > Software engineering >  Flutter AppBar IconSize Invalid Constant Value?
Flutter AppBar IconSize Invalid Constant Value?

Time:07-25

CustomAppBar(PreferredSize Widget Size.FromHeight(60))

I'm trying to make a custom appbar and using in leading IconButton, when I try to IconButton in icon size I got an invalid constant value error

This is my circle icon back button code:

class CircleBackButton extends StatelessWidget {
  CircleBackButton({
    Key? key,
    this.width,
    this.height,
    this.iconSize
  }) : super(key: key);

  double? width = 35;
  double? height = 35;
  final double? iconSize;

  @override
  Widget build(BuildContext context) {
    return Container(
      width: width,
      height: width,
      decoration: BoxDecoration(
          shape: BoxShape.circle,
          border: Border.all(color: secondary.withOpacity(0.2))),
      child: const Center(
        child: Icon(
          Icons.arrow_back_ios_new,
          size: **iconSize**,
          color: secondary,
        ),
      ),
    );
  }
}

What am I doing wrong, I don't understand.

CodePudding user response:

You need to remove the constkeyword before your Center widget, like so:

class CircleBackButton extends StatelessWidget {
  CircleBackButton({
    Key? key,
    this.width,
    this.height,
    this.iconSize
  }) : super(key: key);

  double? width = 35;
  double? height = 35;
  final double? iconSize;

  @override
  Widget build(BuildContext context) {
    return Container(
      width: width,
      height: width,
      decoration: BoxDecoration(
          shape: BoxShape.circle,
          border: Border.all(color: secondary.withOpacity(0.2))),
      child: Center( // <-- no const here
        child: Icon(
          Icons.arrow_back_ios_new,
          size: iconSize,
          color: secondary,
        ),
      ),
    );
  }
}

Your Center widget is not constant (as the const keyword would imply) since the size of its child depends on iconSize which is not a compile time constant.

CodePudding user response:

Remove const from Center because it is reading it data on runtime.

child: Center(
  child: Icon(
    Icons.arrow_back_ios_new,
    size: iconSize,
  • Related