在 MUI 中更改背景与线性条件。

huangapple go评论62阅读模式
英文:

change background in MUI with linery condition

问题

我想有条件地更改背景,但这并没有起作用。

export const defaultTheme = createTheme({
  palette: {
    mode: "dark",
    primary: {
      light: "#757ce8",
      main: "#3f50b5",
      dark: "#464646",
      contrastText: "#fff",
    },
    secondary: {
      light: "#ff7961",
      main: "#f44336",
      dark: "#b200e2",
      background: "#fff",
      contrastText: "#000",
    },
    background: { default: defaultTheme.palette.mode === "dark" ? "#000" : "#fff" },
  },
});

const MainTheme = ({ children }) => {
  return (
    <ThemeProvider theme={defaultTheme}>
      <CssBaseline />
      {children}
    </ThemeProvider>
  );
};

export default MainTheme;

我尝试在应用程序的其他部分像这样更改背景:

if (defaultTheme.palette.mode === "dark") {
  defaultTheme.palette.background = { default: "#939393" };
} else {
  defaultTheme.palette.background = { default: "#000" };
}

但这也没有起作用。

英文:

I want to change background conditionaly but that isn't work.

export const defaultTheme = createTheme({

  },
  palette: {
    mode: &quot;dark&quot;,
    primary: {
      light: &quot;#757ce8&quot;,
      main: &quot;#3f50b5&quot;,
      dark: &quot;#464646&quot;,
      contrastText: &quot;#fff&quot;,
    },
    secondary: {
      light: &quot;#ff7961&quot;,
      main: &quot;#f44336&quot;,
      dark: &quot;#b200e2&quot;,
      background: &quot;#fff&quot;,
      contrastText: &quot;#000&quot;,
    },
    background: { default: defaultTheme.palette.mode=== &quot;dark&quot; ? &quot;#000&quot; : &quot;#fff&quot; },
  },
});

const MainTheme = ({ children }) =&gt; {
  return (
    &lt;ThemeProvider theme={defaultTheme}&gt;
      &lt;CssBaseline /&gt;
      {children}
    &lt;/ThemeProvider&gt;
  );
};

export default MainTheme;

I try to change background in the other part of application like this:

      if (defaultTheme.palette.mode === &quot;dark&quot;) {
        defaultTheme.palette.background =  {default:&quot;#939393&quot;} ;

      } else {
        defaultTheme.palette.background = { default: &quot;#000&quot; };

      }

and that didn't work too.

答案1

得分: 1

你必须提供两个主题 - 亮色和暗色,它们各自必须具有背景属性。在我们的应用程序中,您必须有一个切换按钮,其状态如下:

const [isLight, setLight] = useState(true);

// 其他一些内容

return (
    <ThemeProvider theme={isLight ? defaultTheme : darkTheme}>

    // 其他内容

    </ThemeProvider>
);

这是一种非常常见的方法,但我希望您明白了要点。

英文:

You must provide two themes - light and dark, each of them must have props for background. Somewhere in our app your must have a toggle button with state like

const [isLight, setLight] = useState(true);

// some other stuff

return (
    &lt;ThemeProvider theme={isLight ? defaultTheme : darkTheme}&gt;

    // whatever

    &lt;/ThemeProvider&gt;
);

It's very common approach, but I hope you got the point.

huangapple
  • 本文由 发表于 2023年7月18日 00:12:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定