GradeSelectionModal.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**
  2. * Sample React Native App
  3. * https://github.com/facebook/react-native
  4. *
  5. * @format
  6. * @flow
  7. */
  8. import React, { Component } from "react";
  9. import {
  10. Platform,
  11. StyleSheet,
  12. Text,
  13. View,
  14. Image,
  15. TouchableOpacity,
  16. ImageBackground,
  17. TextInput,
  18. Button,
  19. StatusBar,
  20. Modal,
  21. ScrollView,
  22. TouchableHighlight,
  23. DeviceEventEmitter,
  24. findNodeHandle,
  25. UIManager
  26. } from "react-native";
  27. import CitysData from "../../data/citys.json";
  28. type Props = {};
  29. export default class GradeSelectionModal extends Component<Props> {
  30. state = {
  31. modalVisible: false,
  32. click_index: this.props.grade_index,
  33. item_width: 0,
  34. item_height: 0,
  35. grade_data: [
  36. "一年级",
  37. "二年级",
  38. "三年级",
  39. "四年级",
  40. "五年级",
  41. "六年级",
  42. "七年级",
  43. "八年级",
  44. "九年级"
  45. ]
  46. };
  47. render() {
  48. return (
  49. <Modal
  50. // animationType="fade"
  51. animationType="none"
  52. transparent={true}
  53. visible={this.state.modalVisible}
  54. onRequestClose={() => {
  55. this.setState({ modalVisible: false });
  56. }}
  57. >
  58. <View
  59. style={{
  60. flex: 1,
  61. justifyContent: "center",
  62. alignItems: "center"
  63. }}
  64. >
  65. <View
  66. style={{
  67. width: "92%",
  68. backgroundColor: "white",
  69. height: "80%",
  70. borderRadius: 20,
  71. flexDirection: "column",
  72. overflow: "hidden",
  73. alignItems: "center",
  74. justifyContent: "center"
  75. }}
  76. >
  77. <View
  78. style={{
  79. flex: 1,
  80. justifyContent: "center",
  81. alignItems: "center"
  82. }}
  83. >
  84. <Text
  85. style={{
  86. textAlign: "center",
  87. fontSize: 22,
  88. color: "rgba(58, 58, 58, 1)"
  89. }}
  90. >
  91. 我所在的年级
  92. </Text>
  93. </View>
  94. <View
  95. style={{
  96. flex: 3,
  97. width: "100%",
  98. marginTop: 5
  99. }}
  100. >
  101. {this.centerView()}
  102. </View>
  103. <View style={{ flex: 2, width: "90%" }}>
  104. <ImageBackground
  105. source={require("../images/userInfo/logoutbg1.png")}
  106. style={{
  107. flex: 1,
  108. width: "100%",
  109. alignItems: "center",
  110. justifyContent: "center",
  111. height: "100%"
  112. }}
  113. imageStyle={{
  114. resizeMode: "contain"
  115. }}
  116. >
  117. <TouchableOpacity
  118. style={{
  119. width: "100%",
  120. alignItems: "center",
  121. height: "40%",
  122. justifyContent: "center"
  123. }}
  124. activeOpacity={1}
  125. onPress={() => this.commit()}
  126. >
  127. <Text
  128. style={{
  129. fontSize: 30,
  130. color: "white",
  131. width: "100%",
  132. textAlign: "center"
  133. }}
  134. >
  135. 确定
  136. </Text>
  137. </TouchableOpacity>
  138. </ImageBackground>
  139. </View>
  140. <View style={{ flex: 0.5 }} />
  141. </View>
  142. </View>
  143. </Modal>
  144. );
  145. }
  146. centerView() {
  147. return (
  148. <View
  149. style={{
  150. flex: 1,
  151. flexDirection: "column",
  152. alignItems: "center",
  153. justifyContent: "center"
  154. }}
  155. >
  156. <View
  157. style={{
  158. flex: 1,
  159. justifyContent: "space-evenly",
  160. width: "90%",
  161. flexDirection: "row",
  162. //换行显示
  163. flexWrap: "wrap",
  164. alignItems: "center"
  165. }}
  166. onLayout={event => this.onLayout(event)}
  167. >
  168. {this.centerItem()}
  169. </View>
  170. </View>
  171. );
  172. }
  173. onLayout(event) {
  174. // alert(event.nativeEvent.layout.width);
  175. // alert(event.nativeEvent.layout.width / 3);
  176. this.setState({
  177. item_width: event.nativeEvent.layout.width / 3.5,
  178. item_height: event.nativeEvent.layout.height / 3
  179. });
  180. }
  181. centerItem() {
  182. let arr = [];
  183. for (let i = 0; i < this.state.grade_data.length; i++) {
  184. let index = i;
  185. let color = "";
  186. if (this.state.click_index == i) {
  187. color = "rgba(88, 168, 250, 1)";
  188. } else {
  189. color = "rgba(61, 192, 252, 0.15)";
  190. }
  191. arr[i] = (
  192. <TouchableOpacity
  193. activeOpacity={1}
  194. key={i}
  195. style={{
  196. margin: 1,
  197. height: this.state.item_height,
  198. alignItems: "center",
  199. justifyContent: "center"
  200. }}
  201. onPress={() => this.clickGrade(index)}
  202. >
  203. <View
  204. style={{
  205. width: this.state.item_width,
  206. height: this.state.item_height / 2,
  207. backgroundColor: color,
  208. borderRadius: 20
  209. }}
  210. >
  211. <Text style={styles.item_text}>{this.state.grade_data[i]}</Text>
  212. </View>
  213. </TouchableOpacity>
  214. );
  215. }
  216. return arr;
  217. }
  218. commit() {
  219. this.props.commitGrade(
  220. this.state.grade_data[this.state.click_index],
  221. this.state.click_index
  222. );
  223. this.setModalVisible(false);
  224. }
  225. setModalVisible(visible) {
  226. this.setState({
  227. modalVisible: visible
  228. });
  229. }
  230. clickGrade(index) {
  231. this.setState({
  232. click_index: index
  233. });
  234. }
  235. }
  236. const styles = StyleSheet.create({
  237. item_text: {
  238. flex: 1,
  239. textAlign: "center",
  240. textAlignVertical: "center",
  241. color: "rgba(58, 58, 58, 1)",
  242. fontSize: 16
  243. }
  244. });