如何使用DrawerNavigator for React-Native

时间:2018-07-04 21:54:26

标签: react-native flexbox toolbar side-menu

我正在将DrawerNavigator用于React-Native,并在Drawer中使用以下CustomDrawerContentComponent ...

const uiTheme = {
  palette: {
    primaryColor: COLOR.blue500,
  },
  toolbar: {
    container: {
      height: 80,
    },
  },
};

const propTypes = {
  navigation: PropTypes.shape({
    goBack: PropTypes.func.isRequired,
  }).isRequired,
};

const CustomDrawerContentComponent = props => (
  <Container>
    <Toolbar
      leftElement="arrow-back"
      onLeftElementPress={() => this.props.navigation.goBack()}
      centerElement="Menu"
    />
    <View>
      <Drawer.Header>
        <Drawer.Header.Account
          avatar={<Avatar text="K" />}
          footer={{
            dense: true,
            centerElement: {
              primaryText: 'Account',
              secondaryText: 'xxxx@yahoo.com',
            },
            rightElement: 'arrow-drop-down',
          }}
        />
      </Drawer.Header>
      <DrawerItems {...props} />
    </View>
  </Container>
);

const MainRoot = DrawerNavigator(
  {
    Login: {
      path: '/login',
      screen: Login,
    },
    Profile: {
      path: '/profile',
      screen: Profile,
    },
    Settings: {
      path: '/settings',
      screen: Settings,
    },
  },
  {
    initialRouteName: 'Settings',
    contentOptions: {
      activeTintColor: '#2089b0',
      activeBackgroundColor: 'transparent',
      inactiveTintColor: '#000000',
      inactiveBackgroundColor: 'transparent',
      labelStyle: {
        fontSize: 18,
        marginLeft: 0,
        fontFamily: 'sans-serif-thin',
      },
    },
    drawerWidth: SCREEN_WIDTH * 0.8,
    contentComponent: CustomDrawerContentComponent,
    drawerOpenRoute: 'DrawerOpen',
    drawerCloseRoute: 'DrawerClose',
    drawerToggleRoute: 'DrawerToggle',
  }
);

export default class AppContainer extends Component {
  render() {    
    return (
      <ThemeContext.Provider value={getTheme(uiTheme)}>
        <MainRoot />
      </ThemeContext.Provider>
    );
  }
}

Expo.registerRootComponent(AppContainer);

我想要做的是将工具栏的元素朝下放置...

Side Menu

这是怎么做到的? (很抱歉,React-Native相对较新...)

还有代码onLeftElementPress = {()=> this.props.navigation.goBack()} 对于this.props.navigation返回null。

是否需要传递一些东西??

思想。

1 个答案:

答案 0 :(得分:0)

Am使用react-native-material-ui,因此工具栏具有leftElementContainer和centerElementContainer元素。因此可以使用以下样式设置样式:

const uiTheme = {
  palette: {
    primaryColor: COLOR.blue500,
  },
  toolbar: {
    container: {
      height: 80,
    },
    leftElementContainer: {
      marginTop: 20,
    },
    centerElementContainer: {
      marginTop: 20,
    },
  },
};