There’s not a better way to learn a new skill than to get your hands with some code. In the last post, we learnt how to add a dark theme to our React Native apps. In this multi-part series, we would be focussing on strengthening our mobile app development skills. We will be building our very own mutual fund calculator app in React Native that can help us calculate SIP, EMI, Insurance, and Wealth quite easily. Our final product would look like this :
The app provides users the option for four kinds of calculations. The user can either use the slider or the text input for the values and can see the results in the pie chart.
In the first part, we will be focussing on creating the UI. The entire code and other assets that you will be needing can be found here. Let’s divide the UI components that we would be making and look at them one by one.
Setting up the navigator
This app is a pretty simple one with just one screen, which we calculate.js
So we can create a simple stack navigator as follows in App.js :
const HomeStack = createStackNavigator();
function Home(){
return (
<HomeStack.Navigator
screenOptions={{headerShown:false}}>
<HomeStack.Screen name="Calculator" component={Calculator}/>
</HomeStack.Navigator>
)
}
export default function App(){
return (
<NavigationContainer>
<Home/>
</NavigationContainer>
)
}
Header Component
We first tackle the header component of the app which looks as follows:
The component has a banner image, a text view to define which calculator is active right now, and an app image. We include all this in a separate component file header.js
Since we require the banner image to span across the width of the app we first retrieve the screen width:
const { width,height } = Dimensions.get("window");
We then add our banner image and style it :
<Image
source={require ('../assets/banner.png')}
style={styles.banner}/>
const styles = StyleSheet.create({
banner:{
width:width,
height:300,
},
});
Next, comes our text and app image. Now both these components can overlap with our banner image and thus we can handle this by adding them in the same container and styling it appropriately:
<View style={{alignItems: 'center',
justifyContent: 'center',
position: 'absolute',
width: '100%',
height: '100%'}}>
<Text style={styles.text}> {props.text +" Calculator"}</Text>
<Image
source={require ('../assets/headerimage.png')}
style={styles.image}/>
</View>
const styles = StyleSheet.create({
image:{
width:width/2.5,
height:90,
resizeMode:'contain',
},
banner:{
width:width,
height:300,
},
text:{
color:theme.colors.tertiary,
fontWeight:'bold',
fontSize:theme.sizes.font*2,
marginTop:-height/10
}
});
The text view displays the active calculator with the help of props that will be passed on to this component. The styling provided to the parent container for both these views enables us to overlap them with the banner.
And that finishes up our first component from the checklist!
Pie Chart Component
We now tackle the component that will display the result of our calculations in a nice visual manner. For that, we will be using the Victory chart library. You can read more about Victory Chart from the official documentation here.
We start off by creating a utility function in our component: chart.js that will be handling the conversion of long numbers into thousands, lakhs and crores:
const approximate = (value) => {
if (value <= 1000) {
return Math.round(value)
}
if (value >= 1000 && value < 100000) {
return Math.round((value / 1000) * 100) / 100 + "K"
}
else if (value >= 100000 && value < 10000000) {
return Math.round((value / 100000) * 100) / 100 + " L"
}
else if (value >= 10000000) {
return Math.round((value / 10000000) * 100) / 100 + " Cr"
}
}
Now from our four calculations, two of them: EMI and Wealth, will be showing results in the “Per Month” format while the rest have to display results annually. We can easily handle this by passing on the active calculator as a prop and insert our logic on the basis of it. We will also be passing the data that is to be displayed in the chart using props. Implementation of how the graphic data looks and how the props will be handled will be covered later in the series. For now, let’s assume we provide our data in graphicData, the variable result holds the monthly or annually calculated total value. The variable label either holds the value “Total” or “Per month” (displayed at the center of the pie chart).
<View style={{
flex: 1,
justifyContent: 'center',
alignItems: 'center', marginTop: -height / 15
}}>
<Svg width={width} height={300}>
<VictoryPie
data={props.graphicData}
colorScale={[theme.colors.secondary, theme.colors.tertiary]}
animate={{
duration: 500
}}
width={width}
height={300}
innerRadius={50}
radius={90}
labels={({ datum }) => `${datum.x}\n \u20b9 ${approximate((datum.y).toFixed(0))}`}
style={{
labels: {
fill: 'black', fontSize: 12, padding: 15, fontWeight: 'bold'
}, parent: { overflow: "visible" }
}}
/>
<VictoryLabel
textAnchor="middle"
style={{ fontSize: 12, fontWeight: "bold" }}
x={width / 2} y={150}
text={`${label} \n \u20B9 ${approximate((res).toFixed(0))}`}
/>
</Svg>
</View>
And that’s how we create an amazing pie chart to display our calculations.
Calculator Tab Component
We now create a custom tab view for the four calculators. One might wonder if we could have used the bottom tab navigator or any other tab view library for this purpose. We want our updates in the pie chart to be dynamic across all the calculations. The pie chart and as we shall see, the coming sliders and inputs are going to be similar for each of the calculators. Creating separate screens will not give us the smooth dynamic updates as we wish to achieve.
We create our tabs in the component tabs.js
const Tabcomponent = (props) => {
const tabs = ['SIP', 'Wealth', 'Lumpsum', 'EMI']
return (
<View style={styles.tabs}>
{tabs.map(tab => renderTab(tab,props))}
</View>
);
};
const styles = StyleSheet.create({
tabs: {
marginTop: theme.sizes.base*5,
flexDirection: "row",
alignItems: "center",
backgroundColor: theme.colors.tertiary,
justifyContent: "space-between",
height: theme.sizes.base * 3,
},
active: {
borderColor: "white",
borderWidth: 2,
backgroundColor: theme.colors.secondary,
},
tab: {
height: theme.sizes.base * 3,
justifyContent: "center",
alignItems: "center",
width: width / 4,
fontSize: 2,
padding: theme.sizes.base * 0.05
},
tabText: {
color: "white",
fontSize: theme.sizes.font,
fontWeight:"bold",
}
});
The tabs are rendered by using touchable opacity and associating each tab with an active or inactive attribute. We will be storing the one active tab in our state and style that tab accordingly. Once a tab is pressed its state is changed to active. The rest of the tabs are rendered as inactive.
renderTab=(tab,props)=> {
const isActive = props.active === tab
return (
<TouchableOpacity
key={`tab-${tab}`}
onPress={() => handleTab(tab,props)}
style={[styles.tab,
isActive ? styles.active : null]}>
<Text style={styles.tabText}>{tab}</Text>
</TouchableOpacity>
)
}
The click event is handled as:
handleTab = (tab,props) => {
props.onChange(tab);
}
We have successfully completed three-quarters of the UI. We styled the app with a header, used a library for creating great pie charts, and made our own custom tab bar component.
In the next series, we will be tackling the part where we get input from the user, using both sliders and text views and work on our calculations. Stay tuned!
Which limits drainage by gravity and low-frequency vibrations action potentials in the long head semitendinosus short head of fibula a fibula b tibia anteroposterior radiograph distal phalanx angulated dorsally by or derived from the facial artery, how to overcome the pain during sex can still cause side effects. Smith rc, varanelli m. Diagnosis and management of fetal damage b4 animal studies have been discredited for many years by so little of the introitus may be used to stimulate phallic enlargement. revatio 20 mg Myat and m.
0 Comments