简体   繁体   English

如何更改 Material-UI 的 Toggle 颜色

[英]How to change the color of Material-UI's Toggle

So I put my Toggle button in my AppBar , which created an issue because they are the same color when the Toggle is selected.所以我将我的AppBar Toggle按钮置于一个问题,因为选择了Toggle时它们是相同的颜色。

I've tried many different things (as shown below), but have been unable to change it's color.我尝试了很多不同的东西(如下所示),但一直无法改变它的颜色。

 import React from 'react'; import Toggle from 'material-ui/Toggle' import Drawer from 'material-ui/Drawer'; import AppBar from 'material-ui/AppBar'; import MenuItem from 'material-ui/MenuItem'; import getMuiTheme from 'material-ui/styles/getMuiTheme'; import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider'; var Style = { palette: { primary1Color: '#ffffff', }, }; class AppBarComp extends React.Component { constructor() { super(); this.state = { open: false }; } getChildContext() { return {muiTheme: getMuiTheme(Style)}; } handleToggle = () => this.setState({open: !this.state.open}); handleClose = () => this.setState({open: false}); render() { return <MuiThemeProvider muiTheme={getMuiTheme()}> <div> <AppBar onLeftIconButtonTouchTap={this.handleToggle} title="Time Visualizer" iconElementRight={ <Toggle labelStyle={{color:'white'}} style={{marginTop:'.75em'}} label="Toggle Compare" /> }/> <Drawer docked={false} width={250} open={this.state.open} onRequestChange={(open) => this.setState({open})} > <MenuItem onTouchTap={this.handleClose}>Settings</MenuItem> <MenuItem onTouchTap={this.handleClose}>About</MenuItem> </Drawer> </div> </MuiThemeProvider> } } AppBarComp.childContextTypes ={ muiTheme: React.PropTypes.object, }; export default AppBarComp;

I'm not really sure how I can get to that element to change it's color.我不太确定如何访问该元素以更改其颜色。 using Chrome, I was able to inspect the element and change it's color that way, but have been unable to repeat that with code.使用 Chrome,我能够检查元素并以这种方式更改它的颜色,但无法用代码重复这一点。

I've also been unable to center the Toggle programmatically, but have been able to do it in chrome which makes be believe I'm not high enough in the object?我也无法以编程方式使Toggle居中,但已经能够在 chrome 中做到这一点,这让人相信我在对象中不够高?

If that makes sense.如果这是有道理的。

Thanks!谢谢!

If you want change toggle color in 'on mode', you need to update colors in the theme:如果要在“开启模式”中更改切换颜色,则需要更新主题中的颜色:

const muiTheme = getMuiTheme({
  toggle: {
    thumbOnColor: 'yellow',
    trackOnColor: 'red'
  }
});

and then use it :)然后使用它:)

<MuiThemeProvider muiTheme={muiTheme}>

You can check here what other theme stuff is used by toggle: https://github.com/callemall/material-ui/blob/master/src/Toggle/Toggle.js您可以在此处查看切换使用的其他主题内容: https : //github.com/callemall/material-ui/blob/master/src/Toggle/Toggle.js

I don't know if that is the only way to do this but it seems to work :) There might be problem though if some other control uses that color path :/我不知道这是否是唯一的方法,但它似乎有效:) 如果其他控件使用该颜色路径,则可能会出现问题:/

Changing color of toggle in 'off mode' is easier:在“关闭模式”下更改切换颜色更容易:

<Toggle 
  thumbStyle={{ backgroundColor: 'red' }} 
  trackStyle={{ backgroundColor: 'green' }} />

Hope it helps :)希望它有帮助:)

All you need to do所有你需要做的

thumbSwitchedStyle={{ backgroundColor: 'grey' }}

Example示例

<Toggle
     thumbSwitchedStyle={{ backgroundColor: 'grey' }}
     labelStyle={{color:'white'}}
     style={{marginTop:'.75em'}}
     label="Toggle Compare"

Thus, if selected the color becomes grey :)因此,如果选择颜色变为灰色:)

image形象

    import {Switch,makeStyles} from "material-ui/core"
     
    const useStyles = makeStyles((theme) => ({
     toggle: {
             width:50,
            '& .Mui-checked': {
               color: '#109125',
               transform:'translateX(25px) !important'
           },
           '& .MuiSwitch-track': {
               backgroundColor:'#008000e0'
           }
       },
    })   

const Index= (props) => {
    const classes = useStyles(); 
       return( 
    <Switch color="primary" size="small" className={classes.toggle} checked: {true}  />)
}

Refer to this code and you will get what you need.参考此代码,您将获得所需的内容。

Click on this link for more information Material-Ui/Switch单击此链接了解更多信息Material-Ui/Switch

    const toggleStyles = makeStyles({
      root: { /* … */ },
      label: { /* … */ },
      outlined: {
        /* … */
        '&$disabled': { /* … */ },
      },
      outlinedPrimary: {
        /* … */
        '&:hover': { /* … */ },
       },
      disabled: {},
    }, { name: 'MuiButton' });

generates the following class names that you can override:生成以下可以覆盖的类名:

    .MuiButton-root { /* … */ }
    .MuiButton-label { /* … */ }
    .MuiButton-outlined { /* … */ }
    .MuiButton-outlined.Mui-disabled { /* … */ }
    .MuiButton-outlinedPrimary: { /* … */ }
    .MuiButton-outlinedPrimary:hover { /* … */ }

To use the code:使用代码:

      function FunctionalComponent(props){
         const toggleClass = toggleStyles();
         
         return (
           <ToggleButtonGroup value={toggleValue} onChange ={handleToggleValueChange}>
              <ToggleButton value="toggleValue1" className={toggleClass.root}>VALUE 1</ToggleButton>
              <ToggleButton value="toggleValue2" className={toggleClass.outlined}>VALUE 2</ToggleButton>
           </ToggleButtonGroup>
         )
      }

      

For more details: https://material-ui.com/styles/advanced/#with-material-ui-core更多详情: https : //material-ui.com/styles/advanced/#with-material-ui-core

The color of the Material-UI toggle is set to primary1Color, which you can over-ride by making a custom theme. Material-UI 切换的颜色设置为primary1Color,您可以通过制作自定义主题来覆盖它。 http://www.material-ui.com/#/customization/themes http://www.material-ui.com/#/customization/themes

You'd want to make a styles object somewhere (a separate file is probably best) that contains an object like this:你想在某处创建一个样式对象(一个单独的文件可能是最好的),它包含一个这样的对象:

{
    palette: {
        primary1Color: '#YOURCOLOR',
    },
}

Assuming you import that into your React class as Styles, you'd want to set it as the theme like this:假设你将它作为样式导入到你的 React 类中,你想将它设置为这样的主题:

getChildContext() {
        return {
            muiTheme: getMuiTheme(Styles),
        };
    }

And并且

YOUR_CLASS.childContextTypes = {
    muiTheme: React.PropTypes.object,
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM