颜色相关

一个提供处理颜色有用功能的库。这是原生Android库 colors-ktx 的Flutter移植版本,也是我制作的。

功能

  • 用于判断颜色是深色还是浅色的辅助函数。
  • 用于加深或变浅颜色的辅助函数。
  • 用于操作颜色 alpha 通道的辅助函数。
  • 用于生成随机深色或浅色颜色的辅助函数。
  • ColorSet – 一组背景色前景色次要前景色,您可以在应用程序的各种UI元素中使用它们。

用法

将库添加到您的pubspec.yaml文件中

dependencies:
    colors_stuff: <latest_version>

导入库。

import 'package:colors_stuff/colors_stuff.dart';

玩得开心!

var color = randomColor(); // Generates a random color
var isDark = isColorDark(color); // Returns true if the color is dark

var lightColor = lightenColor(color, 0.2); // Lightens the color by 50%
var darkColor = darkenColor(color); // Darkens the color (by 10% default)

var reducedAlphaColor = reduceAlpha(color); // Reduces the alpha (by 10% default)
var increasedAlphaColor = increaseAlpha(color, 0.5); // Increases the alpha by 50%

还有更多!
ColorSet 类会根据您传递给它的背景色,为您生成前景色和次要前景色。您还可以让ColorSet 类为您生成一个随机背景色,然后用它来生成相应的前景色和次要前景色。您可以在应用程序的各种UI元素中使用这些颜色,而不必担心前景色在背景色上是否易读。

var set = ColorSet.ofSomeRandomColor(); // Generates a set with random background color.
...
// Now some where in your UI logic, you have a card on which you have some text.
// You can use the set colors easily to style the card.
Card(
    color: set.backgroundColor,
    child: Padding(
        padding: EdgeInsets.all(24),
        child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
            Text(
                "Title",
                style: TextStyle(
                    color: set.foregroundColor),
            ),
            SizedBox(height: 16),
            Text(
                "Some descriptive text",
                style: TextStyle(
                color: set.mutedForegroundColor),
            ),
        ],
        ),
    ),
),

GitHub

查看 Github