Flutter – How to use enum with const in a class

I am fairly new to Flutter and have made my first Reusable Widget. Below is a cleaner example on what I am trying to do.

My enum:

enum StreamBoxSize {
  small(9),
  medium(15),
  large(18);

  final double borderRadius;
  const StreamBoxSize(this.borderRadius);
}

I am trying to give each enum value a default border radius value, so if I am choosing StreamBoxSize.small the border radius of my container is going to be 9.

My class looks like this:

class StreamBox extends StatelessWidget {
  const StreamBox({
    super.key,
    this.size = StreamBoxSize.large,
  });
  final StreamBoxSize size;

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(**borderRadius**),
          topRight: Radius.circular(**borderRadius**),
          bottomLeft: Radius.circular(**borderRadius**),
          bottomRight: Radius.circular(**borderRadius**),
        ),
...

I have marked in bold the value that of course doesnt work becaues my class dont know what borderRadius is. Any idea how to make it work? The value I want here is dynamic so everything will be 9 if I have choosen this:

StreamBox(size: StreamBoxSize.small),

>Solution :

Maybe you already tried and there’s something I’m missing, but why setting

Radius.circular(size.borderRadius)  

for each side won’t work?

After all, StreamBoxSize.borderRadius is public and should be visible inside the build method…

Leave a Reply