简体   繁体   English

Flutter:如何使有色容器不透明?

[英]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):W小工具库引起的异常CA ═════════════════════════I / flutter(18323):构建BackGround(dirty)时引发了以下断言:I / flutter(18323): 'dart:ui / painting.dart':断言失败:188行:'':不正确。 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):I / flutter(18323):要么断言表明框架本身有错误,要么我们应该提供I / flutter(18323):此错误消息中的更多信息,以帮助您确定和修复根本原因。 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 I / flutter(18323):无论哪种情况,请通过在GitHub上提交错误来报告此断言:I / flutter(18323): https : //github.com/flutter/flutter/flutter/issues/new? template = BUG.md

ultimately: how do I give to colored container opacity? 最终:如何使有色容器不透明?

thanks for the help 谢谢您的帮助

If you check the source code, the line 188 from painting.dart : 如果您检查源代码,那么line 188 from painting.dartline 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. 根据assert函数,您必须输入介于0.0和1.0之间的一个值。 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)
    ),
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Flutter / Dart 打印彩色调试消息以显示彩色 Android Studio 控制台 Z78E6221F6393D14CEDZZ8F6393D14CEDZ66 - How can I print colored debug messages with Flutter / Dart to show colored Android Studio console output? 如何在位图上设置不透明度? - How do I set opacity on a Bitmap? 如何使dropdownitems的宽度大于flutter中容器(Dropdown Button)的宽度 - How to give dropdownitems a larger width than the container( Dropdown Button ) in flutter 如何从 flutter 中的网格视图计数将单元格的彩色容器从一个 position 移动(带动画)到另一个内部生成的列表? - How to move(with animation) a cell's colored container from one position to another inside generated List from grid view count in flutter? 如何制作一个按钮,在 flutter 中添加包含特定信息的容器 - How do I make a button that adds a container with specific info inside in flutter 如何为我的整个 Flutter 应用程序提供线性渐变? - How can I give Linear gradient to my entire flutter app? 我如何给一个 editText 一个固定的宽度 - How do i give an editText a fixed width 在 imageview 上添加低不透明度彩色覆盖 - Adding low opacity colored overlay on imageview 如何在 flutter 中赋予复选框渐变? - How to give checkbox gradient in flutter? 如何使用Flutter将容器移动到靠近徽标的位置 - How can I move a container closer to the overlaying logo using Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM