Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
501 views
in Technique[技术] by (71.8m points)

Generate custom color shades from parent color in flutter

I needs to find a better approach to generate shade colors from a given custom color for theming purposes. So far I found a way to do this by reducing opacity of the given color as below. so I can accent Color color and faded color of given color to this function.

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));
}

But because of I'm reducing opacity of the color It shows what's going under the widgets like when using SliverAppBar.

Is there anyway to get the faded value of a Hex color?

question from:https://stackoverflow.com/questions/65856982/generate-custom-color-shades-from-parent-color-in-flutter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Finally found a way from 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);
}

And Im calling this fucntion like this.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...