React Native之Modal组件实现遮罩层效果

React-Native中Modal的使用

  
  
  1. /**
  2. * React Native App
  3. * dongtao 2017/04/22
  4. * @flow
  5. */
  6. import React, { Component } from 'react';
  7. import {
  8. AppRegistry,
  9. StyleSheet,
  10. Text,
  11. Modal,
  12. PixelRatio,
  13. View
  14. } from 'react-native';
  15. class ModalDemo extends Component {
  16. constructor(props) {
  17. super(props);//这一句不能省略,照抄即可
  18. this.state = {
  19. animationType: 'none',//none slide fade
  20. modalVisible: false,//模态场景是否可见
  21. transparent: true,//是否透明显示
  22. };
  23. }
  24. render() {
  25. let modalBackgroundStyle = {
  26. backgroundColor: this.state.transparent ? 'rgba(0, 0, 0, 0.5)' : 'red',
  27. };
  28. let innerContainerTransparentStyle = this.state.transparent
  29. ? { backgroundColor: '#fff', padding: 20 }
  30. : null;
  31. return (
  32. <View style={{ alignItems: 'center', flex: 1 }}>
  33. <Modal
  34. animationType={this.state.animationType}
  35. transparent={this.state.transparent}
  36. visible={this.state.modalVisible}
  37. onRequestClose={() => { this._setModalVisible(false) } }
  38. onShow={this.startShow}
  39. >
  40. <View style={[styles.container, modalBackgroundStyle]}>
  41. <View style={[styles.innerContainer, innerContainerTransparentStyle]}>
  42. <Text style={styles.date}>2016-08-11</Text>
  43. <View style={styles.row}>
  44. <View >
  45. <Text style={styles.station}>长沙站</Text>
  46. <Text style={styles.mp10}>8: 00出发</Text>
  47. </View>
  48. <View>
  49. <View style={styles.at}></View>
  50. <Text style={[styles.mp10, { textAlign: 'center' }]}>G888</Text>
  51. </View>
  52. <View >
  53. <Text style={[styles.station, { textAlign: 'right' }]}>北京站</Text>
  54. <Text style={[styles.mp10, { textAlign: 'right' }]}>18: 00抵达</Text>
  55. </View>
  56. </View>
  57. <View style={styles.mp10}>
  58. <Text>票价:¥600.00元</Text>
  59. <Text>乘车人:东方耀</Text>
  60. <Text>长沙站 火车南站 网售</Text>
  61. </View>
  62. <View style={[styles.mp10, styles.btn, { alignItems: 'center' }]}>
  63. <Text style={styles.btn_text}>去支付</Text>
  64. </View>
  65. <Text
  66. onPress={this._setModalVisible.bind(this,false) }
  67. style={{fontSize:20,marginTop:10}}>
  68. 关闭
  69. </Text>
  70. </View>
  71. </View>
  72. </Modal>
  73. <Text style={{ fontSize: 30,color:'red' }} onPress={this._setModalVisible.bind(this, true) }>预定火车票</Text>
  74. </View>
  75. );
  76. }
  77. _setModalVisible = (visible) => {
  78. this.setState({ modalVisible: visible });
  79. }
  80. startShow=()=>{
  81. alert('开始显示了');
  82. }
  83. }
  84. const styles = StyleSheet.create({
  85. container: {
  86. flex: 1,
  87. justifyContent: 'center',
  88. padding: 40,
  89. },
  90. innerContainer: {
  91. borderRadius: 10,
  92. alignItems: 'center',
  93. },
  94. row: {
  95. alignItems: 'center',
  96. flex: 1,
  97. flexDirection: 'row',
  98. marginBottom: 20,
  99. },
  100. rowTitle: {
  101. flex: 1,
  102. fontWeight: 'bold',
  103. },
  104. button: {
  105. borderRadius: 5,
  106. flex: 1,
  107. height: 44,
  108. alignSelf: 'stretch',
  109. justifyContent: 'center',
  110. overflow: 'hidden',
  111. },
  112. buttonText: {
  113. fontSize: 18,
  114. margin: 5,
  115. textAlign: 'center',
  116. },
  117. page: {
  118. flex: 1,
  119. position: 'absolute',
  120. bottom: 0,
  121. left: 0,
  122. right: 0,
  123. top: 0,
  124. },
  125. zhifu: {
  126. height: 150,
  127. },
  128. flex: {
  129. flex: 1,
  130. },
  131. at: {
  132. borderWidth: 1 / PixelRatio.get(),
  133. width: 80,
  134. marginLeft:10,
  135. marginRight:10,
  136. borderColor: '#18B7FF',
  137. height: 1,
  138. marginTop: 10
  139. },
  140. date: {
  141. textAlign: 'center',
  142. marginBottom: 5
  143. },
  144. station: {
  145. fontSize: 20
  146. },
  147. mp10: {
  148. marginTop: 5,
  149. },
  150. btn: {
  151. width: 60,
  152. height: 30,
  153. borderRadius: 3,
  154. backgroundColor: '#FFBA27',
  155. padding: 5,
  156. },
  157. btn_text: {
  158. lineHeight: 18,
  159. textAlign: 'center',
  160. color: '#fff',
  161. },
  162. });
  163. AppRegistry.registerComponent('ModalDemo', () => ModalDemo);

1.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值