'How to customize background color of button using Material UI themes
I can't figure out from the documentation how to style a button so the background color of that component is purple using Material UI themes. I can get the text color to change to purple with the below. Please let me know what I am doing wrong!
import React, { Component } from 'react';
import { createTheme, ThemeProvider } from '@material-ui/core/styles';
import purple from '@material-ui/core/colors/purple';
import Button from '@material-ui/core/Button';
const theme = createTheme({
palette: {
primary: {
main:purple[500],
}
},
});
export default class Practice extends Component {
render(){
return (
<div>
<ThemeProvider theme={theme}>
<Button color="primary">Add theme</Button>
</ThemeProvider>
</div>
)}}
Solution 1:[1]
For the currently latest MUI 5 it should be (from the docs):
const theme = createTheme({
components: {
// Name of the component
MuiButtonBase: {
defaultProps: {
// The props to change the default for.
disableRipple: true, // No more ripple, on the whole application ?!
},
},
},
});
And then provide your theme as usual.
More info: https://mui.com/material-ui/customization/theme-components/#default-props
Solution 2:[2]
The null check operator is the ! you use to tell Dart that you are certain that the preceding variable is not null. If you're wrong (and it is null) you'll get this error. There are two places where you could get this error in your code.
In your code, you have final user = FirebaseAuth.instance.currentUser and it is not a given that this is not null, yet you use the null check operator in the statement .doc(user!.uid) which will throw the error if user is null.
Likewise, you set the variable doc using DocumentSnapshot? doc = snapshot.data (clearly a variable that can be null) and use the null check operator in title: Text(doc!['address']), which could also generate this error.
If you variable can be null, then you have to check for that possibility using an if... then construct or using null-aware operators like ? and ??. Only if you can statically prove that a nullable variable cannot ever be null at a certain point of your code can you forego that check and use the null-check operator !.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Erwol |
| Solution 2 | Bram |
