How to change the default palette in MudBlazor?
Asked Answered
M

1

6

I'm using MudBlazor v6.07 and I know how to change the individual colors of the (UI) components using a new theme and code like this:

private MudTheme _myTheme= new MudTheme();
_myTheme.Palette.Primary = new MudBlazor.Utilities.MudColor("#090");

But I would like to switch to one of the palettes at once, like Green or Pink. Instead of modifying each theme color one by one.

I assume this can be done, but I can't find how to do this.

Metheglin answered 16/3, 2022 at 8:46 Comment(2)
Hi, did you find a way to do it? I am interested too!Autocracy
I never found a way. It's almost a year ago that I used MudBlazor. Perhaps it is now natively possible.Metheglin
V
12

You can pass in the theme you want as a variable and then you can change it via code..

<MudThemeProvider Theme="@MyCustomTheme" />

@code {
  MudTheme MyCustomTheme = new MudTheme()
    {
        Palette = new Palette()
        {
            Primary = Colors.Blue.Darken4,
            Secondary = Colors.Green.Accent4,
            AppbarBackground = Colors.Red.Default,
            //Define other properties here.  
        },
        
    };
}

There is a section about this in the documentation.

Voronezh answered 23/3, 2022 at 10:55 Comment(3)
Thanks for your reply. I'm aware of that page. I already use that to change individual components. But I want to change all components at once by setting a different color palette.Metheglin
I made an example for you, perhaps this will be more clear: try.mudblazor.com/snippet/wEQGuxmSgOWqxkyq Note that you'll have to change the main theme provider, i.e. the one in your layout.Voronezh
As a note, there is no built in way to retrieve a Palette from a config file. But you can always roll your own. In other words, create a section in AppSetting.json and then get that at runtime and use it to define the Palette for the Theme.Assessment

© 2022 - 2024 — McMap. All rights reserved.