Francesco Iapicca
Francesco Iapicca

Reputation: 2657

Flutter: how do I give to colored container opacity?

why can't I assign opacity to the color in this container?

this works:

class ColouredContainer extends StatelessWidget {
  @override
  Widget build(BuildContext context)=>
    Container( decoration: BoxDecoration(color: greens[1]),);
}

this return the error below:

class ColouredContainer extends StatelessWidget {
  @override
  Widget build(BuildContext context)=>
    Container( decoration: BoxDecoration(color: greens[1]
      .withOpacity(50) /// <= THIS!
    ),
  );
}

I/flutter (18323): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ I/flutter (18323): The following assertion was thrown building BackGround(dirty): I/flutter (18323): 'dart:ui/painting.dart': Failed assertion: line 188: '': is not true. I/flutter (18323): I/flutter (18323): Either the assertion indicates an error in the framework itself, or we should provide substantially I/flutter (18323): more information in this error message to help you determine and fix the underlying cause. I/flutter (18323): In either case, please report this assertion by filing a bug on GitHub: I/flutter (18323): https://github.com/flutter/flutter/issues/new?template=BUG.md

ultimately: how do I give to colored container opacity?

thanks for the help

Upvotes: 0

Views: 2151

Answers (1)

Mehmet Esen
Mehmet Esen

Reputation: 6876

If you check the source code, the line 188 from painting.dart:

  Color withOpacity(double opacity) {
    assert(opacity >= 0.0 && opacity <= 1.0);
    return withAlpha((255.0 * opacity).round());
  }

According to assert function, you must enter some value between 0.0 & 1.0 inclusive. That's the error. So the answer:

class ColouredContainer extends StatelessWidget {
  @override
  Widget build(BuildContext context)=>
    Container( decoration: BoxDecoration(color: greens[1]
      .withOpacity(0.5)
    ),
  );
}

Upvotes: 2

Related Questions