如何在材料中自定义复选框的颜色-ui

kel*_*pei 20 material-ui

我在我的项目中使用material-ui,并且我在一个黑色背景的div中有一个Checkbox.但它看起来不太好,因为Checkbox也是黑色的.如何将Checkbox的颜色从黑色更改为另一种颜色?

Hit*_*ahu 37

这是你如何做到的:

 <FormControlLabel  
                control={
                  <Checkbox
                    checked={cryon}
                    onChange={this.handleChange('cryon')}
                    style ={{
                      color: "#00e676",
                    }}
                    value="cryon"
                  />
                }
                label={<Typography variant="h6" style={{ color: '#2979ff' }}>Work</Typography>}
              />
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

  • 我喜欢这个解决方案,因为它应用起来很简单,并且可以影响选中和未选中的颜色。适用于 Material-ui 4.8.0 (2认同)

Jef*_*oud 32

您需要使用iconStyle,但由于复选框图标是SVG图像,您需要使用fill而不是设置颜色color:

https://jsfiddle.net/27Lmaz48/1/

<Checkbox label='My checkbox' 
  labelStyle={{color: 'white'}}
  iconStyle={{fill: 'white'}}
/>
Run Code Online (Sandbox Code Playgroud)

  • 我认为这个答案对于 Material UI 4.11.3 来说已经过时了 (4认同)
  • 哪个版本的material-ui使用? (3认同)
  • 如果有一件事我可以责怪material-ui,那就是他们的网站上缺乏示例..它充满了工作示例,但他们从未显示原始代码.. (2认同)

Nea*_*arl 11

MUI v5中,有 2 种更好的方法来更改Checkbox颜色:

sx支柱

这对于一次性样式很有用,可以快速设置,但仅适用于特定的Checkbox

import Checkbox, { checkboxClasses } from "@mui/material/Checkbox";
Run Code Online (Sandbox Code Playgroud)
<Checkbox
  {...props}
  sx={{
    [`&, &.${checkboxClasses.checked}`]: {
      color: 'magenta',
    },
  }}
/>
Run Code Online (Sandbox Code Playgroud)

color支柱

您可以查看答案以了解更多详细信息。基本上,color某些组件(例如,,,...)的 propButton必须Checkbox是对象Radio的颜色之一Palette,可以根据您的喜好进行扩展:

import { pink, yellow } from "@mui/material/colors";
import Checkbox, { checkboxClasses } from "@mui/material/Checkbox";
import { createTheme, ThemeProvider } from "@mui/material/styles";

const { palette } = createTheme();
const theme = createTheme({
  palette: {
    pinky: palette.augmentColor({ color: pink }),
    summer: palette.augmentColor({ color: yellow })
  }
});
Run Code Online (Sandbox Code Playgroud)
<ThemeProvider theme={theme}>
  {/* pre-defined color */}
  <Checkbox />
  <Checkbox color="secondary" />
  <Checkbox color="success" />
  <Checkbox color="default" />
  {/* custom color */}
  <Checkbox color="pinky" />
  <Checkbox color="summer" />
  <Checkbox
</ThemeProvider>
Run Code Online (Sandbox Code Playgroud)

现场演示

Codesandbox 演示

相关答案


Den*_*lot 10

You could use material ui theme.

const theme = createMuiTheme({
  overrides: {
    MuiCheckbox: {
      colorSecondary: {
        color: '#custom color',
        '&$checked': {
          color: '#custom color',
        },
      },
    },
  },
});
Run Code Online (Sandbox Code Playgroud)

got it from here


小智 9

这是一个老问题,但是对于那些正在使用材料1.2的人来说。

iconStyle对我不起作用。

但是,我通过覆盖现有主题并将“复选框”组件包装到一个新主题中来实现。

import { withStyles } from '@material-ui/core/styles';
import Checkbox from '@material-ui/core/Checkbox';



const checkBoxStyles = theme => ({
    root: {
      '&$checked': {
        color: '#3D70B2',
      },
    },
    checked: {},
   })

const CustomCheckbox = withStyles(checkBoxStyles)(Checkbox);
Run Code Online (Sandbox Code Playgroud)

现在,您可以在渲染功能中使用“ CustomCheckbox”组件。

选中后,颜色应为您分配的颜色。

例


小智 6

对我来说,这是通过添加 root 和检查分类来解决的

const styles = () => ({
  root: {
    "&$checked": {
      color: "rgba(0, 0, 0, 0.54)"
    }
  },
  checked: {}
})
Run Code Online (Sandbox Code Playgroud)

并将其传递到复选框的类中

<Checkbox
          checked={checked}
          classes={{
            root: classes.root,
            checked: classes.checked
          }}
          onChange={handleCheckBox}
        />
Run Code Online (Sandbox Code Playgroud)

希望这会帮助其他人