React-navigation之StackNavigator_2进阶

完整代码:
import React from 'react';
import {
  AppRegistry,
  Text,
  View,
  Button,
  ScrollView,
} from 'react-native';
import { StackNavigator } from 'react-navigation';


const BaseScreen = ({navigation, banner}) => (  
    <ScrollView>  
        <Text style = {{fontSize : 30 , color : 'red' }}>{banner}</Text>  
        <Text></Text>
        <Button  
            //传参数测试
            onPress={() => navigation.navigate('Chat', { name: 'Jane' })}  
            title="Go to a chat screen"  
        />  
         <Text style = {{width : 20 }}></Text>
          <Text></Text>
        <Button  
            //模式测试
            onPress={() => navigation.navigate('Profile', { name: 'profile' })}  
            title="Go to a profile screen"  
        />  
        <Text style = {{width : 20 }}></Text>
        <Text></Text>
        <Button
            //header 测试
            onPress = {()=> navigation.navigate('HeaderTest')}
             title="Go to a header screen"
        />
         <Text style = {{width : 20 }}></Text>
         <Text></Text>
        
        {navigation.state.routeName === 'HeaderTest' &&
             // 这句代码意思是:当跳转的界面是HeaderTest时,显示  "Toggle Header" 按钮 即在HeaderTest界面显示该按钮
             <Button
                 title="Toggle Header"
                 //  headerVisible 的默认值为 undefined 第二次 true 第三次  false 第四次 true ....
                 onPress={() =>navigation.setParams({
                           headerVisible: !navigation.state.params ||!navigation.state.params.headerVisible,
                       })
                 }
              />
        }
         <Text style = {{width : 20 }}></Text>
         <Text></Text>
         <Button  
            //返回
            onPress={() => navigation.goBack(null)}  
            title="Go back"  
        />  
        <Text style = {{width : 20 }}></Text>
    </ScrollView>  
);  


const HomeScreen = ({navigation}) => ( 
  <View>
     <BaseScreen  
        banner="Home Screen"  
        navigation={navigation}  
    />  
  </View> 
    
);  


HomeScreen.navigationOptions = ({navigation}) =>{
	return {
          // 展示数据 "`" 不是单引号 
         title: 'home Screen',
            //导航栏的style
         headerStyle: {
                backgroundColor: '#fff'
            },
            //导航栏的title的style
         headerTitleStyle: {
                color: 'green',
                //居中显示
                alignSelf : 'center',
         },
         //按压返回按钮显示的颜色 API > 5.0 有效
         headerPressColorAndroid : 'blue',
         //返回按钮的颜色
         headerTintColor : 'red',
         //是否允许右滑返回,在iOS上默认为true,在Android上默认为false
         gesturesEnabled: true,
	};
};


const ChatScreen = ({navigation}) => (  
	<View>
        <BaseScreen  
           banner={`${navigation.state.params.name}'s Photos`} 
           navigation={navigation}  
        />  
	</View>
   
); 

ChatScreen.navigationOptions = {
     title : "chat screen"
};


const ProfileScreen = ({navigation}) => (  
	<View>
        <BaseScreen  
           banner={  
             `${navigation.state.params.mode === 'edit' ? 'Now Editing ' : ''}
              ${navigation.state.params.name}'s Profile`  
           }  
           navigation={navigation}  
        />  
	</View>
   
); 


ProfileScreen.navigationOptions = ({navigation}) => {
	return {
         title : `${navigation.state.params.name}`,
         //右边按钮
         headerRight:(  
         	<View>
                <Button  
                   //
                   title= {navigation.state.params.mode === 'edit' ? 'Done' : 'Edit'}
                   //注意 setParams 是对象navigation中的 函数  下面的代码实现模式切换 
                   onPress={() => navigation.setParams({ mode: navigation.state.params.mode === 'edit' ? '' : 'edit' })}  
                />  
         	</View>   
        ),
	};
};

//header 测试 
const HeaderTestScreen = ({ navigation }) => (
	<View>
        <BaseScreen  
           banner={'Full screen view'} 
           navigation={navigation}  
        />  
	</View>
);

HeaderTestScreen.navigationOptions = ({ navigation }) => {
  const headerVisible = navigation.state.params && navigation.state.params.headerVisible;
  console.log("-------headerVisible---------->");
  console.log(headerVisible);
  return {
    header: headerVisible ? undefined : null,
    title: 'Now you see me',
  };
};


// MyFirstProject 是你的react native 项目名  注意: 这块代码要放置到HomeScreen,ChatScreen...的下面否则会出错:Home不存在。
const MyFirstProject = StackNavigator({
  Home: { screen: HomeScreen ,
          // Optional: When deep linking or using react-navigation in a web app, this path is used:
          path: 'home/:name',
         // The action and route params are extracted from the path.
  },
  Chat: { screen: ChatScreen },
  Profile: {screen: ProfileScreen},
  HeaderTest:{screen: HeaderTestScreen},
  // navigationOptions: {}  // 此处设置了, 会覆盖组件内的`static navigationOptions`设置. 
},{
	initialRouteName: 'Home', // 默认显示界面
    onTransitionStart: ()=>{ console.log('导航栏切换开始'); },  // 回调
    onTransitionEnd: ()=>{ console.log('导航栏切换结束'); },  // 回调
    mode : 'card',
    headerMode: 'screen'

});


AppRegistry.registerComponent('MyFirstProject', () => MyFirstProject);
效果图:

参考: 资料




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值