Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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…

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading