对于flatlist
,我创建了一个演示组件并将项目作为道具传递给子组件,这很好用。现在,我在演示组件中使用TouchableOpacity
,并在其他组件中使用navigate
。我该如何实现??
MyflastList:
_renderItem({item,index}) {
return(
<View>
{index == 0 && <MachineInformationCard {...item}/>}
{index > 0 && <MachineFunctionality {...item} />}
</View>
)
}
我的Presentational层:
import React from 'react';
import {View,Text,StyleSheet,TouchableOpacity} from 'react-native';
export default MachineFunctionality = (props) => {
let acheivedPercentage = ((props.acheived_value/props.target_value)*100);
let acheivedPercentageValue = acheivedPercentage;
acheivedPercentage = acheivedPercentageValue + '%';
return(
<TouchableOpacity style={styles.CardItem}
onPress={()=> this.props.navigation.navigate('MachineFunctionalityDetail')}>
<Text style={{fontSize:18,color:'black'}}>{props.level}</Text>
<Text style={{marginTop: 5}}>{props.taget_label}</Text>
<View style={[styles.barContainer,{width:'100%'}]}>
{/* <Text style={{textAlign:'center',fontSize:12,padding:2}}>100%</Text> */}
</View>
<Text style={{marginTop: 5}}>{props.acheived_label} ({Math.ceil(acheivedPercentageValue)}%)</Text>
<View style={[styles.barContainer,{width: acheivedPercentage}]}>
</View>
</TouchableOpacity>
)
}
const styles = StyleSheet.create({
CardItem: {
flex: 1,height:120,padding:8,backgroundColor:'#e7e7e7',borderRadius: 5,overflow: 'hidden',margin: 5,shadowColor: 'black',shadowOpacity: 0.8,elevation: 8
},barText: {
justifyContent: 'center',fontSize: 13,margin: 10,fontWeight: 'bold',},barContainer: {
height: 10,marginTop:5,backgroundColor:'lightskyblue',borderColor: 'blue',borderWidth: 1,borderRadius: 3,shadowOpacity: 0,elevation: 2,})
MyNavigator
const MachinestackNavigator = createStackNavigator({
MachineHome: {screen: AllMachine,navigationOptions:{header: null}},MachineFunctionalityDetail: {screen:MachineFunctionalityDetail,MachineProfile:{screen:MachineProfile,navigationOptions:{header: null}}
})