为什么我不能为这个容器中的颜色指定不透明度?
这个有效:
class ColouredContainer extends StatelessWidget {
@override
Widget build(BuildContext context)=>
Container( decoration: BoxDecoration(color: greens[1]),);
}
这将返回以下错误:
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
最后:如何给彩色容器赋予不透明度?
感谢帮助
最佳答案
如果您查看源代码,painting.dart 中的第 188 行
:
Color withOpacity(double opacity) {
assert(opacity >= 0.0 && opacity <= 1.0);
return withAlpha((255.0 * opacity).round());
}
根据 assert
函数,您必须输入介于 0.0 和 1.0 之间的某个值(含 0.0 和 1.0)。那就是错误。所以答案:
class ColouredContainer extends StatelessWidget {
@override
Widget build(BuildContext context)=>
Container( decoration: BoxDecoration(color: greens[1]
.withOpacity(0.5)
),
);
}
关于android - Flutter:如何赋予彩色容器不透明度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57165892/