I’m struggling finding a solution to manage the color of my Container’s Borders without hard coding them every time.
I want to define my custom Colors for the Borders in ThemeData for the day mode and the night mode, but I can’t find the right property. At the moment the color chosen by Dart for the borders is dark.
Just to be clear … i wish to write something like this:
border: Border.all(
width: width * 0.002,
),
without specifying the color.
2
Answers
If you take a look at the
Border.all
constructor in the source code, it looks like this:As you can see, the
color
parameter defaults toColor(0xFF000000)
, which is solid black. This means it doesn’t inherit or reference any color from the app’sThemeData
. Unfortunately, this parameter isn’t linked to theme properties that we can easily customize.Moreover, extending the
Border
class to create a custom border that uses something likeTheme.of(context).colorScheme.surface
for thecolor
isn’t an option, because theBorder
class doesn’t have access to thecontext
.However, if you’re looking for a more flexible way to apply borders consistently throughout your app, you can create a global function. This function can pull color values from the theme and apply them to your borders, like so:
You can then use this function in your widgets like this:
You must first create a dart file:
you can define each color that you want in dark or light mode
and you can use this
when you change the theme (light to dark or dark to light)
colors can be changed automatically.