【问题标题】:how do you invert a color in flutter?你如何在颤动中反转颜色?
【发布时间】:2021-04-06 08:27:15
【问题描述】:

我有一个用于创建渐变效果的小部件:

class GradientContainer extends StatelessWidget {
  final child;

  GradientContainer({@required this.child});
  @override
  Widget build(BuildContext context) {
    return Stack(children: [
      Positioned(
          left: 5,
          top: 17,
          bottom: 5,
          right: 9,
          child: ClipRRect(
            borderRadius: BorderRadius.circular(4),
            child: Container(
              decoration: BoxDecoration(
                  gradient: LinearGradient(
                      begin: Alignment.topLeft,
                      end: Alignment.bottomRight,
                      stops: [
                    0.0,
                    1.0
                  ],
                      colors: [
                    Colors.black.withOpacity(0.0),
                    Theme.of(context).scaffoldBackgroundColor.withOpacity(0.8),
                  ])),
            ),
          )),
      child,
    ]);
  }
}

这很有效,直到用户切换到深色主题并且无法非常清晰地隐藏渐变。所以,我想做类似Theme.of(context).scaffoldBackgroundColor.invertColor().withOpacity(0.8) 这样的事情,这样我就可以获得身体的反转颜色,这让我可以在渐变上创建一个很好的对比,无论主题如何。有人知道怎么做吗?我曾尝试在网上查找,但我只能找到如何在图像上应用反转滤镜。

【问题讨论】:

    标签: flutter colors


    【解决方案1】:

    根据http://www.vb-helper.com/howto_invert_color.html

    您可以通过减去红色、绿色和蓝色中的每一个来反转颜色 来自 255 的组件。换句话说:

    new_red   = 255 - old_red
    new_green = 255 - old_green
    new_blue  = 255 - old_blue
    
    Color invert(Color color) {
      final r = 255 - color.red;
      final g = 255 - color.green;
      final b = 255 - color.blue;
    
      return Color.fromARGB((color.opacity * 255).round(), r, g, b);
    }
    
    void main() {
      final inverted = invert(Colors.deepOrange);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-11
      • 2019-09-08
      • 2021-03-10
      • 2021-09-27
      • 2019-09-23
      • 1970-01-01
      • 2016-05-14
      • 2020-09-01
      相关资源
      最近更新 更多