How to get default theme data parameters in Flutter? - 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,
),

Related

apply shade to flutter color variable

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().

How to override an ExpansionPanel in flutter

How the title already suggests, I want to have my own ExpansionPanel, where, contrary to the default one, the elevation is set to zero.
I already found this in flutter.dev:
https://api.flutter.dev/flutter/material/ExpansionPanelList/ExpansionPanelList.html
I have been trying to implement this but haven't succeeded yet, how do I do it?
So Im talking about these Elevation marks of each ElevationPanel:
Maybe I misunderstood you, but it is as simple as just setting the elevation parameter when you build the widget, as such:
As such:
ExpansionPanelList(
elevation: 0,
children: [ ... ],
)
Edit:
After comments, problem has to do with divider color. Do something like this:
ExpansionPanelList(
dividerColor: Colors.transparent,
children: [...]
)

Deciding which level(s) in object hierarchy to add const in Dart/Flutter

In a widget hierarchy, how should one go about deciding at which level const should be added? For example, is the following:
const Padding(
padding: EdgeInsets.symmetric(
horizontal: LayoutStyles.horizontalPagePadding
),
child: Icon(Icons.search, color: Colors.black),
)
better than, say:
Padding(
padding: const EdgeInsets.symmetric(
horizontal: LayoutStyles.horizontalPagePadding
),
child: const Icon(Icons.search, color: Colors.black),
)
If so, why? Does it depend on which element(s) is/are most likely to recur in the program, and hence be canonicalized?
Depends on that specific part of the Widget tree being constant throughout the Application life cycle. Like the entire Padding widget is not going to change overtime hence make it const. In cases where the child or the subtree is going to change the second approach suits
From the Docs - Performance considerations
If a subtree does not change, cache the widget that represents that
subtree and re-use it each time it can be used. It is massively more
efficient for a widget to be re-used than for a new (but
identically-configured) widget to be created. Factoring out the
stateful part into a widget that takes a child argument is a common
way of doing this.
Use const widgets where possible. (This is equivalent to caching a
widget and re-using it.)
An excerpt from medium article on Inherited Widgets
Use const to build your widgets
Without const, selective rebuilding of
the sub-tree does not happen. Flutter creates a new instance of each
widget in the sub-tree and calls build() wasting precious cycles
especially if your build methods are heavy.
If you can add const, add it because it will be compile-time constant and it will optimize the usage.
check this answer for details on const constructors:
How does the const constructor actually work?
here is an explanation of the keyword const and its use:
First, to be able to use this keyword the constructor of your widget must be const
for example (using flutter widgets ) :
you can add the keyword const before EdgeInsets.all but you cant do this with BorderRaduis.circular and the reason is that BorderRaduis.circular is not marked as const.
and the second condition if the constructor is marked as const is to pass a constant value as parametre.
Why we use const keyword ?
it's used mostely to increase the performance of your application (avoid instanciation for every call of the build method)
hope that my answer helped you.
From the Effective Dart: usage, you can see the following example:
Basically, any place where it would be an error to write new instead of const, Dart 2 allows you to omit the const.
Good:
const primaryColors = [
Color("red", [255, 0, 0]),
Color("green", [0, 255, 0]),
Color("blue", [0, 0, 255]),
];
Bad:
const primaryColors = const [
const Color("red", const [255, 0, 0]),
const Color("green", const [0, 255, 0]),
const Color("blue", const [0, 0, 255]),
];
Using your example, and reading the Edge Insets documentation you can see that the symmetric constructor is a const constructor:
const EdgeInsets.symmetric(
{double vertical: 0.0,
double horizontal: 0.0}
)
So the const in this case is optional and recommended to be ommited from the Dart Guidelines.
So, answering your question:
If so, why?
It isn't. It's optional and makes it redundant to read, so it's better to be ommited.
Does it depend on which element(s) is/are most likely to recur in the program, and hence be canonicalized?
You should use for things that are compile time constants, i.e you can figure it's whole value without having to run the program.
If you can assure that, you can add const to the constructor of a function you declared, or a value you created. In this way, the compiler can assign a "nickname" to that object and will not need to recreate it everytime it's needed.

Flutter - Color name string to material color

In Flutter is there a way to generate a material color from it's name, without creating a full map Map<String,MaterialColor>.
In theory, something like this:
String colorName = "deepOrange";
MaterialColor color = Colors(colorName);
According to the comment, the intention is to save and read back from shared_preferences. In that case, it is better to save and retrieve the color by int value, not by string name, to ensure we always get the color.
Save: prefs.setInt("prefered_color", Color.value)
Retrieve: Color c = const Color(prefs.getInt('prefered_color') ?? 0xFF42A5F5);
According to the official doc, there's currently no API to perform the function you described. Although it's easy to implement your methodology, I doubt its usefulness in general cases. Also we have to deal with typos or noSuchColor errors. But using const / enum will offer the advantage of compile time error checking.
I manage to do something like that using the
Colors.primaries list ( found in the colors.dart file):
//colors.dart
/// The material design primary color swatches, excluding grey.
static const List<MaterialColor> primaries = <MaterialColor>[
red,
pink,
purple,
deepPurple,
indigo,
blue,
lightBlue,
cyan,
teal,
green,
lightGreen,
lime,
yellow,
amber,
orange,
deepOrange,
brown,
// The grey swatch is intentionally omitted because when picking a color
// randomly from this list to colorize an application, picking grey suddenly
// makes the app look disabled.
blueGrey,
];
So in order to save the colors:
Color colorToSave = Colors.indigo;
prefs.setInt("colorIndex", Colors.primaries.indexOf(colorToSave));
To retrieve:
MaterialColor colorSaved = Colors.primaries(getInt('colorIndex') ?? 0);
Hope that helps you.
Here is an easy way to do it.
Let's suppose you want to get the material color for Colors.blue
You can use this to get it:-
MaterialStateProperty.all(Colors.blue)
For example, to set the background color of an elevated button, a material color should be provided. You can provide a named color like:-
ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.blue),
),
onPressed: (){},
child: Text('Press me'),
),
Hope helps.

How do I set the animation color of a LinearProgressIndicator?

The LinearProgressIndicator documentation helpfully displays the existence of a valueColor property and even mentions "To specify a constant color use: new AlwaysStoppedAnimation(color).", but if I try to set the color I get an error that LinearProgressIndicator has no instance setter for valueColor and the constructor for the class only accepts a key and a numerical value for the progress amount.
If I want a LinearProgressIndicator with a custom color do I need to create my own class? Is there really no way to specify this?
If you want to set a constant color you can use :
CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
)
Looks like it's controlled from the Theme's accent color:
https://github.com/flutter/flutter/blob/b670ce4bcc49bbab745221eae24fcebcbc9dba7c/packages/flutter/lib/src/material/progress_indicator.dart#L61
Wrap the relevant subtree in a modified Theme setting the accentColor to whatever you might like.
LinearProgressIndicator(
backgroundColor: Color(0xFFB4B4B4),
valueColor: AlwaysStoppedAnimation<Color>(Colors.green),
)
I think you can provide in the below way,
LinearProgressIndicator( valueColor: AlwaysStoppedAnimation<Color> (Color(0xFFA86E52)),),
LinearProgressIndicator(
backgroundColor: Colors.black,
valueColor: AlwaysStoppedAnimation<Color>(Colors.yellow) ,
color: Colors.red,
minHeight: 10,
),