导言
最近做app,记录下自己写的组件
组件:个人资料页面
import React, { Component, useState } from 'react';
import {StyleSheet, Text, View, Dimensions, Image, Button, TouchableOpacity, ImageBackground, TextInput, ScrollView} from 'react-native';
import PropTypes from 'prop-types';
import {BackgroundImage} from 'react-native-elements/dist/config';
// import {Input} from 'react-native-elements';
import {pxToDp} from './styleKits';
const screenWidth = Math.round(Dimensions.get('window').width);
const screenHeight = Math.round(Dimensions.get('window').height);
export default class PeopleInformation extends Component {
constructor() {
super();
this.state = {
isEditting: false,
name: '胡诗杰',
head: require('./pic/head.jpg'),
phoneNumber: '13849489395',
carType: '奔驰',
carMode: '豪华型',
emergencyPhoneNumber: '17398508012',
licensePlateNumber: '豫8888',
carColor: '黑',
onHeadPress: PropTypes.func,
onEditInformation: PropTypes.func,
onSavePress: PropTypes.func,
onCancelPress: PropTypes.func,
formPhoneNumber: '13849489395',
formCarType: '奔驰',
formCarMode: '豪华型',
formEmergencyPhoneNumber: '17398508012',
formLicensePlateNumber: '豫8888',
formCarColor: '黑',
}
this.headPress = this.headPress.bind(this);
this.editInformationPress = this.editInformationPress.bind(this);
this.savePress = this.savePress.bind(this);
this.cancelPress = this.cancelPress.bind(this);
}
headPress() {
alert('function is null')
};
editInformationPress() {
this.setState({
isEditting: true,
})
};
savePress() {
this.setState({
isEditting: false,
phoneNumber: this.state.formPhoneNumber == '' ? this.state.phoneNumber : this.state.formPhoneNumber,
carType: this.state.formCarType == '' ? this.state.carType : this.state.formCarType,
carMode: this.state.formCarMode == '' ? this.state.carMode : this.state.formCarMode,
emergencyPhoneNumber: this.state.formEmergencyPhoneNumber == '' ? this.state.emergencyPhoneNumber : this.state.formEmergencyPhoneNumber,
licensePlateNumber: this.state.formLicensePlateNumber == '' ? this.state.licensePlateNumber : this.state.formLicensePlateNumber,
carColor: this.state.formCarColor == '' ? this.state.carColor : this.state.formCarColor,
})
};
cancelPress() {
this.setState({
isEditting: false,
formPhoneNumber: this.state.phoneNumber,
formCarType: this.state.carType,
formCarMode: this.state.carMode,
formEmergencyPhoneNumber: this.state.emergencyPhoneNumber,
formLicensePlateNumber: this.state.licensePlateNumber,
formCarColor: this.state.carColor,
})
};
render() {
return(
<View style={{
height: '100%',
alignItems: 'center',
backgroundColor: '#BFD0DA',
}}>
<View style={{
height: pxToDp(165),
width: '95%',
backgroundColor: 'white',
marginTop: '5%',
padding: 12,
elevation: 5,
borderRadius: 10,
shadowColor: '#303133',
}}>
<View style={{
height: '55%',
width: '80%',
marginLeft: '10%',
flexDirection: 'row',
}}>
<Image source={this.state.head} style={{
height: pxToDp(80),
width: pxToDp(80),
borderRadius: 120,
}}/>
<View style={{
marginTop: '8%',
marginLeft: '5%',
}}>
<Text style={{
color: '#909399',
}}>{'姓名:' + this.state.name}</Text>
<Text style={{
color: '#909399',
}}>{'手机号:' + this.state.phoneNumber}</Text>
</View>
</View>
<View style={{
width: '80%',
height: 1,
marginTop: '5%',
alignSelf:'center',
opacity: 0.3,
backgroundColor: '#606266',
}} />
<View style={{
marginTop: '5%',
alignSelf: 'center',
flexDirection: 'row',
}}>
<TouchableOpacity
onPress={this.headPress}
style={{
alignSelf: 'center',
flexDirection: 'row',
}}>
<Image source={require('./pic/points.png')} style={{
height: pxToDp(20),
width: pxToDp(20),
}}/>
<Text style={{
marginLeft: '3%',
color: '#409EFF'
}}>{'更改头像'}</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={this.editInformationPress}
style={{
marginLeft: '15%',
alignSelf: 'center',
flexDirection: 'row',
}}>
<Image source={require('./pic/where.png')} style={{
height: pxToDp(20),
width: pxToDp(20),
}}/>
<Text style={{
marginLeft: '3%',
color: '#409EFF'
}}>{'编辑信息'}</Text>
</TouchableOpacity>
</View>
</View>
<View style={{
height: '70%',
width: '95%',
padding: 10,
marginTop: '5%',
elevation: 5,
borderRadius: 10,
shadowColor: '#303133',
backgroundColor: 'white',
}}>
<ScrollView style={{
height: 100,
overflow: 'scroll',
}}>
<View style={{
flexDirection: 'row',
alignItems: 'center',
}}>
<Image source={require('./pic/where.png')} style={{
height: pxToDp(20),
width: pxToDp(20),
marginLeft: '5%',
}}/>
<Text>{'手机号:'}</Text>
<TextInput
placeholder={this.state.formPhoneNumber}
editable={false}
selectionColor={'#409EFF'}
onChangeText={
text => this.setState({
formPhoneNumber: text
})
}
value={this.state.formPhoneNumber}
style={{
width: '60%',
}}
/>
</View>
<View style={{
width: '90%',
height: 1,
alignSelf:'center',
opacity: 0.3,
backgroundColor: '#606266',
}} />
<View style={{
flexDirection: 'row',
alignItems: 'center',
}}>
<Image source={require('./pic/where.png')} style={{
height: pxToDp(20),
width: pxToDp(20),
marginLeft: '5%',
}}/>
<Text>{'汽车车型:'}</Text>
<TextInput
placeholder={this.state.formCarMode}
editable={false}
selectionColor={'#409EFF'}
onChangeText={
text => this.setState({
formCarMode: text
})
}
value={this.state.formCarMode}
style={{
width: '60%',
}}
/>
</View>
<View style={{
width: '90%',
height: 1,
alignSelf:'center',
opacity: 0.3,
backgroundColor: '#606266',
}} />
<View style={{
flexDirection: 'row',
alignItems: 'center',
}}>
<Image source={require('./pic/where.png')} style={{
height: pxToDp(20),
width: pxToDp(20),
marginLeft: '5%',
}}/>
<Text>{'汽车型号:'}</Text>
<TextInput editable={this.state.isEditting}
selectionColor={'#409EFF'}
onChangeText={
text => this.setState({
formCarType: text
})
}
value={this.state.formCarType}
style={{
width: '60%',
}}
/>
</View>
<View style={{
width: '90%',
height: 1,
alignSelf:'center',
opacity: 0.3,
backgroundColor: '#606266',
}} />
<View style={{
flexDirection: 'row',
alignItems: 'center',
}}>
<Image source={require('./pic/where.png')} style={{
height: pxToDp(20),
width: pxToDp(20),
marginLeft: '5%',
}}/>
<Text>{'紧急联系人:'}</Text>
<TextInput
placeholder={this.state.formEmergencyPhoneNumber}
editable={this.state.isEditting}
selectionColor={'#409EFF'}
onChangeText={
text => this.setState({
formEmergencyPhoneNumber: text
})
}
value={this.state.formEmergencyPhoneNumber}
style={{
width: '60%',
}}
/>
</View>
<View style={{
width: '90%',
height: 1,
alignSelf:'center',
opacity: 0.3,
backgroundColor: '#606266',
}} />
<View style={{
flexDirection: 'row',
alignItems: 'center',
}}>
<Image source={require('./pic/where.png')} style={{
height: pxToDp(20),
width: pxToDp(20),
marginLeft: '5%',
}}/>
<Text>{'车辆牌照:'}</Text>
<TextInput
placeholder={this.state.formLicensePlateNumber}
editable={false}
selectionColor={'#409EFF'}
onChangeText={
text => this.setState({
formLicensePlateNumber: text
})
}
value={this.state.formLicensePlateNumber}
style={{
width: '60%',
}}
/>
</View>
<View style={{
width: '90%',
height: 1,
alignSelf:'center',
opacity: 0.3,
backgroundColor: '#606266',
}} />
<View style={{
flexDirection: 'row',
alignItems: 'center',
}}>
<Image source={require('./pic/where.png')} style={{
height: pxToDp(20),
width: pxToDp(20),
marginLeft: '5%',
}}/>
<Text>{'车辆颜色:'}</Text>
<TextInput
placeholder={this.state.formCarColor}
editable={this.state.isEditting}
selectionColor={'#409EFF'}
onChangeText={
text => this.setState({
formCarColor: text
})
}
value={this.state.formCarColor}
style={{
width: '60%',
}}
/>
</View>
<View style={{
width: '90%',
height: 1,
alignSelf:'center',
opacity: 0.3,
backgroundColor: '#606266',
}} />
</ScrollView>
<View style={{
height: '10%',
}}></View>
<View style={{
flexDirection: 'row',
alignItems: 'center',
}}>
<TouchableOpacity
onPress={this.savePress} style={{
height: pxToDp(40),
width: pxToDp(100),
marginLeft: '14%',
flexDirection: 'row',
alignItems: 'center',
display: this.state.isEditting?'flex':'none',
}}>
<Image source={require('./pic/points.png')} style={{
marginLeft: '20%',
height: pxToDp(20),
width: pxToDp(20),
}}/>
<Text style={{
color: '#409EFF',
}}>{'保存'}</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={this.cancelPress} style={{
height: pxToDp(40),
width: pxToDp(100),
marginLeft: '14%',
flexDirection: 'row',
alignItems: 'center',
display: this.state.isEditting?'flex':'none',
}}>
<Image source={require('./pic/points.png')} style={{
marginLeft: '20%',
height: pxToDp(20),
width: pxToDp(20),
}}/>
<Text style={{
color: '#409EFF',
}}>{'取消'}</Text>
</TouchableOpacity>
</View>
<View style={{
height: '5%',
}}></View>
</View>
</View>
)
}
};
调用:个人资料页面
import React from 'react';
import {StyleSheet, Text, View, Dimensions, Image, Button} from 'react-native';
import PeopleInformation from './PeopleInformation';
const App = () => {
return (
<View>
<PeopleInformation/>
</View>
);
};
export default App;
注意
该文章仅个人学习使用,欢迎大家一起交流学习