iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 10
0

https://ithelp.ithome.com.tw/upload/images/20190926/20104220blKuG6tdOB.png

為了達成上圖效果,我們需要建立./app/QRScanner.js 改動./apps/router.js

# 010 QRScanner.js

import React, { Component } from 'react';
import {
  StyleSheet,
  View,
  Text,
  TouchableOpacity,
  Alert,
} from 'react-native';
import QRCodeScanner from 'react-native-qrcode-scanner';
let data = null;
const data_Context = React.createContext(data);
export { data_Context }
export default class Scanner extends Component {
  constructor(props) {
    super(props);
    this.state = {
      userToken: "A",
      camera: false,
      A: false,
      camera_state: true,
      myName: null
    };
  }
  componentDidMount() {
    ///生命週期 component 載入前執行
  }
  componentWillReceiveProps(nextProps) {
    this.setState({
      camera: nextProps.camera,
    });
  }
  onSuccess = async (e) => {
    data = e.data;
    Alert.alert(data);
  }
  render() {
    return (
      <View style={styles.container}>
        <QRCodeScanner
          onRead={this.onSuccess}
          topContent={null}
          reactivate={this.state.camera_state}
          //reactivate={false}
          /////////切換頁面時要關閉/開啟
          reactivateTimeout={1500}
          bottomContent={
            <TouchableOpacity style={styles.buttonTouchable} onPress={() => {
              this.setState({ camera_state: !this.state.camera_state })
              this.testForceFun.bind(this)
            }} >
              <Text style={styles.buttonText}>{this.state.camera_state}</Text>
            </TouchableOpacity>
          }
        />
      </View>
    );
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: 'white',
  }, centerText: {
    flex: 1,
    fontSize: 18,
    padding: 32,
    color: '#777',
  },
  textBold: {
    fontWeight: '500',
    color: '#000',
  },
  buttonText: {
    fontSize: 21,
    color: 'rgb(0,122,255)',
  },
  buttonTouchable: {
    padding: 16,
  },
});
# 010 router.js

import {
  StyleSheet,
···
  Button,
} from 'react-native';
+++ import QRScanner from './QRScanner';
///引入我們剛剛建立的檔案中的QRScanner

const TabNavigator = createBottomTabNavigator(
  {
    Home: HomeScreen,
    Settings: SettingsScreen,
+++ QRScanner:QRScanner,
///TabBottom:QRScanner 指派到 我們引入的QRScanner
  },
  
  {
    defaultNavigationOptions: ({ navigation }) => ({
      tabBarIcon: ({ focused, horizontal, tintColor }) => {
        const { routeName } = navigation.state;
        let iconName;
        //有時我們想在某些Tab上添加icons。
        if (routeName === 'Home') {
          // iconName = `ios-information-circle${focused ? '' : '-outline'}`;
          iconName = `feed`;
        }
        ···
+++     else if (routeName === 'QRScanner') {
+++       // iconName = `ios-options`;
+++       iconName = `code`
+++     }
 

上一篇
[Day9] 開工嘍,開始建立結構
下一篇
[Day11] 快完工嘍『可是我想去喝酒,可以拖稿ㄇ』,新增Firebase 登入功能與資料庫
系列文
小孩子才做選擇 ! Flutter & React Native 我全都要32
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言