android - Flutter:如何赋予彩色容器不透明度?

标签 android flutter dart

为什么我不能为这个容器中的颜色指定不透明度?

这个有效:

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/

相关文章:

android - Activity 复活后存储在 Android Intent 中的状态

java - 嘿,我刚刚开始在 android 中编码并收到错误 Permission Denial : not allowed to send broadcast in android

android - AdMob 无法在真实设备上运行

android - 如何将 FlutterFire 与模拟器演示项目一起使用?

Flutter AnimatedList - 将 CurvedAnimation 添加到 SlideTransition

内存密集型 Activity 后,Android 应用程序因 WIN DEATH 而崩溃

flutter - 如何在页面加载时在 Listview 上应用自动滚动

dart - 如何在同一文件夹中导入多个 Dart 文件

sqlite - Flutter解码一列SQLite查询结果

dart - 使用Dart中的反射从ClassMirror获取 setter/getter 和/或属性?