apply shade to flutter color variable - flutter

I have defined a variable that holds a color value and when I setstate the screen, I always change this color. I want to use this color both as normal and shade100 in my application. However, my code is giving an error below.
late Color objeColor;
objeColor = Colors.orange;
Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
color: objeColor, // there is no error *******
),),
Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
color: objeColor.shade100, // there is error *******
),),
error
The getter 'shade100' isn't defined for the type 'Color'.
Try importing the library that defines 'shade100', correcting the name to the name of an existing getter, or defining a getter or field named 'shade100'

shade100 is a getter for MaterialColor. You can do
late MaterialColor objeColor;
...
color: objeColor.shade100

You can't change the shade of the color again in the widget tree. Though you've got plenty of other methods to manipulate the color with. For example, objeColor.withAlpha(75) looks exactly like Colors.orange.shade100 so it would perfectly fit your needs. In other usecases you can try to experiment with other methods, like withOpacity().

Related

How to get default theme data parameters in Flutter?

For example, the CardTheme. I know you can retrieve it this way:
Theme.of(context).cardTheme
However, how can I get its default parameters? I tried looking in the .dart files but it's difficult to find them.
What I would like to do is find default values for any given Widget in Flutter, so I can do something like this in the instance of a Card:
shape: Theme.of(context).cardTheme.copyWith(
clipBehavior: default,
color: default,
elevation: default + 25, // For example.
margin: default,
shadowColor: default,
shape: default,
surfaceTintColor: default,
),
I would find it useful as I often do not touch certain parameters, and when I find myself wanting to work starting from the defaults as a reference, I don't know where to get them.
You almost done it, you can get the default value this way Theme.of(context).cardTheme.elevation. So, to add 25 to elevation you just need to use like this:
var elevation = Theme.of(context).cardTheme.elevation;
shape: Theme.of(context).cardTheme.copyWith(
elevation: elevation + 25,
),

Invalid constant value in const constructor of Flutter/Dart class

I want to create a bunch of const colored boxes. The following code works:
class MyBox extends SizedBox {
const MyBox(Color color, {Key? key}) // [*1] color parameter
: super(key: key,
width: 20,
height: 40,
child: const DecoratedBox(
decoration: BoxDecoration(
backgroundBlendMode: BlendMode.multiply,
borderRadius: BorderRadius.all(Radius.circular(10)),
color: Colors.green, // [*2] all boxes are green
// color: color, // [*3] this is what I want
),
),
);
}
[...]
static const mbRed = MyBox(Colors.red);
static const mbYellow = MyBox(Colors.yellow);
static const mbBlue = MyBox(Colors.blue);
But notice that the color parameter at line [*1] is not actually used. If I replace line [*2] with line [*3], I am told this is an Invalid constant value. (Note that the Color class are immutable 32-bit values.)
Somehow there must be a way to pass a color parameter to a const constructor. What am I doing wrong?
And if this is the wrong way to go about it, what is a succinct means of creating a bunch of boxes, identical except for color? Thanks!
You can't. When you declare MyBox's constructor as const, that means that the constructor can be used to create a compile-time constant, not that it must be. Meanwhile, when that constructor itself invokes const DecoratedBox, you're stating that DecoratedBox is guaranteed to be a compile-time constant, but you cannot guarantee that. For example, MyBox could have been constructed with a Color determined at runtime.
Unfortunately Dart has no way to recursively say "try to invoke this constructor as const if possible". You will have to declare the MyBox constructor as a non-const constructor.

Is there a way to change the outline border color of the OTP fields using PinCodeTextField plugin

I'm new to Flutter. I'm using PinCodeTextField https://pub.dev/packages/pin_code_fields plugin to create OTP Text field I want the text field to be in different color but I couldn't find the decoration property to change the colour.
Spent hours looking for the same problem and finaly found a way around.
Searck for a file ‘otp_field_style.dart’ through find.
Open the file and you will see following:
import 'package:flutter/material.dart';
import 'package:<myApp’s Constants>/constants/constants.dart';
class OtpFieldStyle {
/// The background color for outlined box.
final Color backgroundColor;
/// The border color text field.
final Color borderColor;
/// The border color of text field when in focus.
final Color focusBorderColor;
/// The border color of text field when disabled.
final Color disabledBorderColor;
/// The border color of text field when in focus.
final Color enabledBorderColor;
/// The border color of text field when disabled.
final Color errorBorderColor;
OtpFieldStyle(
{this.backgroundColor: kFillShade7,
this.borderColor: kOrange4,
this.focusBorderColor: kOrange1,
this.disabledBorderColor: kFillShade4,
this.enabledBorderColor: kFillShade2,
this.errorBorderColor: Colors.red});
}
Now, you can change the colors by giving the hard values,
OR
Import your constants.dart file (where one normally defines the constants, including colors etc) in this and link the colors through it e.g,
import 'package:/constants/constants.dart';
is my contants file, I have linked my kColors in above code,
just change it to appropriate link for your constants dart file.
5. Save and close ‘otp_field_style.dart’.
You can change the colors through your defined constants, I hope it helps!
you can use this properties to change the colors
pinTheme: PinTheme(
shape: PinCodeFieldShape.box,
borderRadius: BorderRadius.circular(5),
fieldHeight: 50,
fieldWidth: 40,
activeFillColor: Colors.black,
inactiveColor: Colors.deepOrange,
inactiveFillColor: Colors.green,
selectedFillColor: Colors.deepPurple,
selectedColor: Colors.greenAccent,
activeColor: Colors.blue
),

Flutter null safety - The argument type 'Color?' can't be assigned to the parameter type 'Color'

I changed my SDK version for flutter to min , so that I can fix my code for null safety.
There is one issue that I don't understand,
this line produces the following error:
The argument type 'Color?' can't be assigned to the parameter type 'Color'
border: Border.all(color: Colors.grey[300], width: 1),
but if I change Colors.grey[300] with whatever value that doesn't use [], it will work,
so Colors.grey works perfectly fine.
What should I change here to keep using grey[300]?
Problem:
Color color = Colors.grey[300]; // Error in null-safe mode
When you use Colors.grey[300], you're actually getting the color from a Map which Dart (in null-safe mode) warns you about because that value could be null. See more
Solutions:
There are two solutions for it. One is general and one is only specific to this case.
Use Bang operator (!)
Color color = Colors.grey[300]! // Added '!', error gone
Use shadeXXX on the Color
Color color = Colors.grey.shade300;
You can use 0xFFE0E0E0 for grey[300].
To pick material colors you can use this tool.
To select a specific color from one of the swatches, index into the swatch using an integer for the specific color desired, as follows:
Color selection = Colors.green[400]!; // Selects a mid-range green.
Each ColorSwatch constant is a color and can used directly. For example:
Container(
color: Colors.blue, // same as Colors.blue[500] or Colors.blue.shade500
)
use shade300 like
Color color = Colors.grey.shade300;
You can write
Colors.grey[300]! like this and this will work.

Flutter cant show string color code in color

I need to show the color from in my container. Mean i have color codes in string format and i need to use as the Container Color.
decoration: BoxDecoration(
color: widget.product.colors[i].toColor(),
borderRadius: BorderRadius.all(Radius.circular(40)),
This is the simple code i am doing like this but its showing error
Invalid argument(s): Can not interpret string 0xFFF6625E
If i remove the .toColor() then its showing
Error: The argument type 'String' can't be assigned to the parameter type 'Color'.
Can any one please tell how can i show this ?
You can use below code:
decoration: BoxDecoration(
//make sure that the widget.product.colors[i] is a hex code (i.e: "0xff0000")
color : Color(int.parse(widget.product.colors[i])),
borderRadius: BorderRadius.all(Radius.circular(40)),
border: Border.all(color: Color(int.parse(kPrimaryColor)))
You can write a method like this..
static Color hexToColor(String code) {
if(code.length == 6) {
return Color(int.parse(code.substring(0, 6), radix: 16) + 0xFF000000);
}
else {
return Color(int.parse(code.substring(0, 8), radix: 16) + 0x00000000);
}
}
You could use Supercharged package:
"#ff00ff".toColor(); // painless hex to color
"red".toColor(); // supports all web color names
You can try this:
color: const Color(widget.product.colors[i]),