【问题标题】:Generate custom color shades from parent color in flutter在颤动中从父颜色生成自定义颜色阴影
【发布时间】:2021-04-27 14:50:16
【问题描述】:

我需要找到一种更好的方法来从给定的自定义颜色生成阴影颜色以用于主题化。到目前为止,我找到了一种通过降低给定颜色的不透明度来做到这一点的方法,如下所示。所以我可以为这个函数强调颜色颜色和给定颜色的褪色颜色。

import 'package:flutter/material.dart';

class AppColors {
  Color accentColor;
  Color fadedColor;

  AppColors(this.accentColor, this.fadedColor);
}

AppColors getAppColors(String color) {
  int budgetAccentcolor = int.parse('0xff' + color);
  int budgetFadedColor = int.parse('0x26' + color);

  return AppColors(Color(budgetAccentcolor), Color(budgetFadedColor));
}

但由于我正在降低颜色的不透明度,它显示了使用 SliverAppBar 时小部件下的情况。

有没有办法获得Hex 颜色的褪色值?

【问题讨论】:

    标签: flutter colors


    【解决方案1】:

    终于从here找到了一条路。

    Color lighten(Color color, [double amount = 0.49]) {
      assert(amount >= 0 && amount <= 1);
    
      final hsl = HSLColor.fromColor(color);
      final hslLight = hsl.withLightness((hsl.lightness + amount).clamp(0.0, 1.0));
    
      return hslLight.toColor();
    }
    
    Color hexToColor(String code) {
        return Color(int.parse(code.substring(0, 6), radix: 16) + 0xFF000000);
    }
    

    我这样称呼这个函数。

    backgroundColor: lighten(hexToColor("f98b5")),
    

    【讨论】:

      猜你喜欢
      • 2021-03-27
      • 2020-12-24
      • 1970-01-01
      • 2012-09-18
      • 2015-07-14
      • 2021-07-01
      • 2018-11-30
      • 2017-03-29
      • 2011-12-19
      相关资源
      最近更新 更多