طراحی Material SnackBar در React Native


سلام دوستان.در این آموزش متنی قصد داریم یاد بگیریم چجوری یک Material SnackBar در React Native طراحی کنیم.SnackBar یک لایه سفارشی کشویی رو به بالا حاوی یک پیام متنی یک خطی با دکمه UNDO است که بصورت اتوماتیک بعد از مدتی پنهان میشه.SnackBar برای نمایش دادن بازخورد عملیات ها در گوشی های android استفاده میشه ولی ما میتونیم با React Native در هر دو گوشی های Android و IOS برای نمایش بازخورد استفاده کنیم.
1.ایمپورت کردن کامپوننت های AppRegistry, Animated, View, Platform, Button, Image, StyleSheet و Text در پروژه.
1 2 3 |
import React, { Component } from 'react'; import { AppRegistry, Animated, View, Platform, Button, Image, StyleSheet, Text } from 'react-native'; |
2.ایجاد یک کلاس جدید به نام MaterialDesignSnackBar در پروژه.
ما در این آموزش از کامپوننت Animated برای نمایش و پنهان کردن SnackBar استفاده میکنیم.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
{ constructor() { super(); this.animatedValue = new Animated.Value(60); this.ShowSnackBar = false; this.HideSnackBar = true; this.state = { SnackBarInsideMsgHolder: '' }; } ShowSnackBarFunction(SnackBarInsideMsgHolder="Default SnackBar Message...", duration=3000) { if( this.ShowSnackBar === false ) { this.setState({ SnackBarInsideMsgHolder: SnackBarInsideMsgHolder }); this.ShowSnackBar = true; Animated.timing ( this.animatedValue, { toValue: , duration: 350 } ).start(this.hide(duration)); } } hide = (duration) => { this.timerID = setTimeout(() => { if(this.HideSnackBar === true) { this.HideSnackBar = false; Animated.timing ( this.animatedValue, { toValue: 60, duration: 350 } ).start(() => { this.HideSnackBar = true; this.ShowSnackBar = false; clearTimeout(this.timerID); }) } }, duration); } SnackBarCloseFunction = () => { if(this.HideSnackBar === true) { this.HideSnackBar = false; clearTimeout(this.timerID); Animated.timing ( this.animatedValue, { toValue: 60, duration: 350 } ).start(() => { this.ShowSnackBar = false; this.HideSnackBar = true; }); } } render() { return( <Animated.View style = {[{ transform: [{ translateY: this.animatedValue }]}, styles.SnackBarContainterView ]}> <Text numberOfLines = { 1 } style = { styles.SnackBarInsideTextStyle }>{ this.state.SnackBarInsideMsgHolder }</Text> <Text style = { styles.SnackBarUndoTextStyle} onPress = { this.SnackBarCloseFunction } > UNDO </Text> </Animated.View> ); } |
3.ایجاد constructor در کلاس اصلی
1 2 3 4 |
constructor() { super(); } |
4.ایجاد یک تابع به نام DisplayMaterialDesignSnackBar .
در این تابع ما پیغام خودمون رو برای نمایش در SnackBar پاس میدیم.
1 2 3 4 |
DisplayMaterialDesignSnackBar = () => { this.refs.ReactNativeCodeSnackBar.ShowSnackBarFunction("reactapp.ir"); } |
5.اضافه کردن یک Parent View در بلاک render’s return.
1 2 3 4 5 6 7 8 9 10 |
render() { return( <View style = { styles.MainContainer }> </View> ); } |
6.اضافه کردن کامپوننت Button درون View اصلی و فراخوانی تابع DisplayMaterialDesignSnackBar در رویداد onPress این button.
1 2 3 4 5 6 7 8 9 10 11 |
render() { return( <View style = { styles.MainContainer }> <Button onPress = { this.DisplayMaterialDesignSnackBar } title=" Show Material Design SnackBar " > </Button> </View> ); } |
7.ایجاد یک object از کلاس MaterialDesignSnackBar بعد از کامپوننت Button و پاس دادن یک props به نام ref = “ReactNativeCodeSnackBar” .
1 2 3 4 5 6 7 8 9 10 11 12 |
render() { return( <View style = { styles.MainContainer }> <Button onPress = { this.DisplayMaterialDesignSnackBar } title=" Show Material Design SnackBar " > </Button> <MaterialDesignSnackBar ref = "ReactNativeCodeSnackBar"/> </View> ); } |
8.ایجاد استایل
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
const styles = StyleSheet.create( { MainContainer: { flex: 1, justifyContent: 'center', paddingHorizontal: 20, paddingTop: (Platform.OS) === 'ios' ? 20 : }, SnackBarContainterView: { position: 'absolute', backgroundColor: '#009688', flexDirection: 'row', alignItems: 'center', left: , bottom: , right: , height: 60, paddingLeft: 10, paddingRight: 55 }, SnackBarInsideTextStyle: { color: '#fff', fontSize: 18 }, SnackBarUndoTextStyle:{ color: '#FFEB3B', fontSize: 18, position: 'absolute', right: 10, justifyContent: 'center', padding: 5 } }); |
9.کد کامل برنامه در فایل App.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
import React, { Component } from 'react'; import { AppRegistry, Animated, View, Platform, Button, Image, StyleSheet, Text } from 'react-native'; class MaterialDesignSnackBar extends Component { constructor() { super(); this.animatedValue = new Animated.Value(60); this.ShowSnackBar = false; this.HideSnackBar = true; this.state = { SnackBarInsideMsgHolder: '' }; } ShowSnackBarFunction(SnackBarInsideMsgHolder="Default SnackBar Message...", duration=3000) { if( this.ShowSnackBar === false ) { this.setState({ SnackBarInsideMsgHolder: SnackBarInsideMsgHolder }); this.ShowSnackBar = true; Animated.timing ( this.animatedValue, { toValue: , duration: 350 } ).start(this.hide(duration)); } } hide = (duration) => { this.timerID = setTimeout(() => { if(this.HideSnackBar === true) { this.HideSnackBar = false; Animated.timing ( this.animatedValue, { toValue: 60, duration: 350 } ).start(() => { this.HideSnackBar = true; this.ShowSnackBar = false; clearTimeout(this.timerID); }) } }, duration); } SnackBarCloseFunction = () => { if(this.HideSnackBar === true) { this.HideSnackBar = false; clearTimeout(this.timerID); Animated.timing ( this.animatedValue, { toValue: 60, duration: 350 } ).start(() => { this.ShowSnackBar = false; this.HideSnackBar = true; }); } } render() { return( <Animated.View style = {[{ transform: [{ translateY: this.animatedValue }]}, styles.SnackBarContainterView ]}> <Text numberOfLines = { 1 } style = { styles.SnackBarInsideTextStyle }>{ this.state.SnackBarInsideMsgHolder }</Text> <Text style = { styles.SnackBarUndoTextStyle} onPress = { this.SnackBarCloseFunction } > UNDO </Text> </Animated.View> ); } } class Project extends Component { constructor() { super(); } DisplayMaterialDesignSnackBar = () => { this.refs.ReactNativeCodeSnackBar.ShowSnackBarFunction("reactapp.ir"); } render() { return( <View style = { styles.MainContainer }> <Button onPress = { this.DisplayMaterialDesignSnackBar } title=" Show Material Design SnackBar " > </Button> <MaterialDesignSnackBar ref = "ReactNativeCodeSnackBar"/> </View> ); } } const styles = StyleSheet.create( { MainContainer: { flex: 1, justifyContent: 'center', paddingHorizontal: 20, paddingTop: (Platform.OS) === 'ios' ? 20 : }, SnackBarContainterView: { position: 'absolute', backgroundColor: '#009688', flexDirection: 'row', alignItems: 'center', left: , bottom: , right: , height: 60, paddingLeft: 10, paddingRight: 55 }, SnackBarInsideTextStyle: { color: '#fff', fontSize: 18 }, SnackBarUndoTextStyle:{ color: '#FFEB3B', fontSize: 18, position: 'absolute', right: 10, justifyContent: 'center', padding: 5 } }); |
دیدگاهتان را بنویسید