Color and Colors class in Flutter - class

I was working with Colors class in Flutter.
Added a buildkey function with input "Color zinc" and the same takes the value like Colors.red, Colors purple etc.
However, if I add the input as "Colors Zinc" the values like Colors.red, Colors.purple throws an error. Why this is so?
The class Colors should match the input class (Colors in this case) I think?enter image description here

It would be helpful if you could attach a screenshot or maybe paste the exact error that you are getting.
But meanwhile as I understood it, It seems like that you are telling this works fine for you:
Expanded buildkey(Color zinc, int buildnumber) { ... }
But you are getting some error with this:
Expanded buildkey(Colors zinc, int buildnumber) { ... }
Which is correct behaviour as Colors.[color] returns a swatch constant which is of type color.
e.g: Color selection = Colors.green[400];
For more info: https://api.flutter.dev/flutter/material/Colors-class.html

May be I found the solution of my issue..
When i looked into the dart file of colors class. I found that,
Colors.green is actually an object of Color class. Code is something like this
Class Colors
Color green = Color(....);
Might be that is the reason thats why it requires it requires color class while initilization of variable and not Colors.

Related

Is there a way to change the default Material color definitions?

The definition of Colors.black is 0xFF000000. Is there a way to directly edit if not override this definition mapping with a different color code ?
For example if I were to then type Colors.black , it would give me something like 0xFF78909C.
Based off the Color class documentation.
Sure, you could hide the Colors class from the material package:
import 'package:flutter/material.dart' hide Colors;
And then create your Colors Class as follows:
class Colors {
Colors._();
static const Color black = Color(0x0xFF78909C);
}
A better approach might be, depending on your scenario, to create a custom Colors Class like the following:
class ThemeColors {
ThemeColors._();
static const Color black = Color(0xFF78909C);
}
Which we can use like the following:
ThemeColors.black;
Without the need to hide Colors; from the import 'package:flutter/material.dart';

error: A value of type 'MaterialColor' can't be assigned to a variable of type 'Color'. (invalid_assignment at [ipro] lib\Dashboard.dart:365)

I want to use chart so for that I put this pakage in my pubspec.ymal after that these errors appear.
error: The argument type 'Color (where Color is defined in C:\Users\anasb\AppData\Roaming\Pub\Cache\hosted\pub.dartlang.org\charts_common-0.8.1\lib\src\common\color.dart)' can't be assigned to the parameter type 'Color (where Color is defined in C:\flutter\bin\cache\pkg\sky_engine\lib\ui\painting.dart)'. (argument_type_not_assignable at [ipro] lib\Dashboard.dart:159)
Errors
The chart_flutter uses it's own Color class. So you can't use the SDK provided one.
See this issue on their repo.
Solution 1:
You can use the following as a work around for custom colors until they fix this issue.
charts.Color.fromHex(code: '#f2f2f2')
Solution 2:
Alternatively, you can define a custom function as follows, which converts the SDK provided color into chart.Color
import 'package:charts_flutter/flutter.dart' as charts;
charts.Color getChartColor(Color color) {
return charts.Color(
r: color.red,
g: color.green,
b: color.blue,
a: color.alpha);
}
Then use it like
getChartColor(Colors.red)

difference between color and map-get method in scss

I have been working on theming part of an ionic 3 application. As part of this I am refactoring the colors.
In order to map the colors based on key I've seen color and map-get functions however I couldn't get any documentation on the exact use case.
Can anyone help me with understanding the difference between map-get vs color function.
Any help will be highly appreciated.
Please see the below sample SCSS.
// Override default colors
$colors: (
primary: green,
secondary: blue,
danger: red,
);
.primary{
background-color:color($colors, primary);
}
.secondary{
background-color:map-get($colors, secondary);
}
.danger{
background-color:color($colors, danger);
}

How to use CardView getCardBackgroundColor correctly

I am trying to compare CardView background color with color from my global colors (from colors.xml).
if (cardView1.getCardBackgroundColor() == ContextCompat.getColor(getApplication(), R.color.grayLighter))
cardView1.getCardBackgroundColor return ColorStateList and not int,
and I cant use getCardBackgroundColor().getDefaultColor() because the button don't have the default color.
Thank you for your help

Dashing: Changing text color based on input to List widget

I am fairly new to HTML/CSS so this may be a newbie question.
I have a list widget that I am using to show the name of nodes (:label) from an API and the last time those nodes reported (:value). If the node has reported within the last hour I want the text color to be green, if it hasn't I want it to be red (pretty simple logic).
I have been trying to use :status-warning and :status-danger to do this, but these options do not change each value independently, rather it changes the whole widget's text color.
This is my coffeescript code, which I got from https://github.com/Shopify/dashing/issues/24 :
ready: ->
if #get('unordered')
$(#node).find('ol').remove()
else
$(#node).find('ul').remove()
onData: (data) ->
#clear existing "status-*" classes
$(#get('node')).attr 'class', (i,c) ->
c=c.replace /\bstatus-\S+/g, ''
# add new class
$(#get('node')).addClass "status-#{x.status}"
Do I need to create a custom widget for this, or is there a built in mechanism to change the text color of values?
I wouldn't bother changing the coffeescript. It's not necessary.
Not sure if you are still looking for an answer for this, but I managed to work something out for this recently. If you set up your code in the .rb file that sends data to your list so that it does some checking for whether the value was updated or not, and then determines and adds a 'color' attribute to the set of data that is sent, you can then tell your list to use that 'color' value to specify which CSS class to look at. The CSS class can then be set up with whatever colors fits the 'color' attribute.
My dashboard (.erb) code for the list is the same, so that doesn't need to change.
In the job file:
myList.rb
# get your label and value for the list before here.
if myValue >= 75.00
myColor = "green"
elsif myValue <= 74.99
myColor = "red"
else
myColor = "white"
end
results.push({:label => myLabel, :value => myValue, :color => myColor})
send_event("myList", items: results)
so from the above we can see that a color variable is sent with the label and value. Now we just need to make the list.html and the list.scss recognise it.
here's the changes in my list.html Only 4 lines should change to look like the 2 below (in the ordered list (<ol></ol>), and unordered list(<ul></ul>):
list.html
<span class="label"><div data-bind-class="item.color" data-bind="item.label"></div></span>
<span class="value"><div data-bind-class="item.color" data-bind="item.value"></div></span>
The data-bind-class is binding the class to whatever is in item.color (which i know in my example will be: red, green, or white)
Then we just need to tell the list.scss how to handle these classes by adding this to the bottom of the file:
list.scss
.white {
color: white;
}
.red {
color: #B31D2B;
}
.green {
color: green;
}
Note: CSS can handle plain English colors. You mentioned you were new to css so I guess it's worth clarifying. I've used a mixture of plain English colors: 'white', and 'green' and a hex code color: '#B31D2B'. This hex code is a darker red which I found was a bit easier on the eyes.
You could use this method to do anything with the text just thinking about it now. It doesn't need to be 'color' specifically. So you could change color to 'state' and have a 'good' state passed through as item.state and then have a 'good' class in your scss that changes the font style, font size, color, font weight. Whatever you'd like really :)