我正尝试在我的React Native项目中添加一些动态样式。我使用的是React Native Snackbar,但现在它出现在我的浮动Action Button前面。这不是材料设计的设计规则。
出于这个原因,每当快餐栏处于活动状态时,我都需要移动我的FAB。我将此保持在一个状态,但我需要一个基于该状态的样式。
此时,我得到了以下代码:
constructor(props){
super(props);
this.state = {
snackbar: true,
}
}
../
const styles = StyleSheet.create({
container: {
marginBottom: this.state.snackbar ? 50 : 0,
backgroundColor: colors.accentColor,
height: Dimensions.get('window').width / 9,
width: Dimensions.get('window').width / 9,
},
});我得到的错误是it对象是未定义的。所以我不确定为什么这不起作用。也许有人能帮我解决这个问题。
现在,我用以下方法解决了这个问题:
constructor(props){
super(props);
this.state = {
snackbar: true,
}
Snackbar.addEventListener('show',()=>{this.setState({snackbar:true})})
Snackbar.addEventListener('hidden',()=>{this.setState({snackbar:false})})
}
render() {
return <ActionButton
onPress={() => {this.props.nav.push(routes.takePicture)}}
style={this.state.snackbar ? stylesFabUp : styles}
/>;
}
}
const stylesFabUp = StyleSheet.create({
container: {
marginBottom: 40,
backgroundColor: colors.accentColor,
height: Dimensions.get('window').width / 9,
width: Dimensions.get('window').width / 9,
},
})
const styles = StyleSheet.create({
container: {
// marginBottom: this.state.snackbar ? 50 : 0,
backgroundColor: colors.accentColor,
height: Dimensions.get('window').width / 9,
width: Dimensions.get('window').width / 9,
},
});但是我不喜欢这个解决方案,因为我有两个样式表
发布于 2017-03-15 19:12:12
您不需要有2个样式表。
考虑以下代码:
constructor(props){
super(props);
this.state = {
snackbar: true,
}
Snackbar.addEventListener('show', () => {this.setState({snackbar:true})})
Snackbar.addEventListener('hidden', () => {this.setState({snackbar:false})})
}
render() {
const fabPositionStyle = this.state.snackbar ? styles.pushUp : styles.normal
return <ActionButton
onPress={() => {this.props.nav.push(routes.takePicture)}}
style={[ styles.fab, fabPositionStyle ]}
/>;
}
}
const styles = StyleSheet.create({
fab: {
height: Dimensions.get('window').width / 9,
width: Dimensions.get('window').width / 9,
backgroundColor: colors.accentColor,
},
pushUp: {
marginBottom: 40,
},
normal: {
marginBottom: 0,
},
})https://stackoverflow.com/questions/42807555
复制相似问题