A fancy wheel picker for height and weight, eh? Sounds like a delightful challenge! As a senior fullstack developer specializing in React Native, I'll guide you through creating a stunning mobile page with not one, but two gorgeous wheel pickers for selecting height and weight. Buckle up!
We'll use:
- React Native as our framework (obviously!)
- react-native-wheel-picker-android for Android (since RN doesn't have a built-in wheel picker)
- react-native-wheel-pick for iOS (a cross-platform alternative)
- A dash of StyleSheet magic for that fancy look
Ready? Let's dive in!
Step 1: Set up a new React Native project
If you haven't already, create a new RN project:
npx react-native init HeightWeightPicker
Step 2: Install required libraries
We'll install both wheel picker libraries to ensure cross-platform compatibility:
npm install react-native-wheel-picker-android react-native-wheel-pick
Link the libraries (not needed for RN 0.60+ with auto-linking):
react-native link react-native-wheel-picker-android
react-native link react-native-wheel-pick
Step 3: Create the Height & Weight Picker screen
Create a new file: screens/HeightWeightPickerScreen.js. This will be our main component:
// screens/HeightWeightPickerScreen.js
import React, { useState } from 'react';
import { View, Text, StyleSheet } from 'react-native';
import { WheelPicker } from 'react-native-wheel-picker-android'; // Android
import WheelPickerIOS from 'react-native-wheel-pick'; // iOS
import { Platform } from 'react-native';
const heights = Array.from({ length: 50 }, (_, i) => (i + 140).toString()); // 140-190 cm
const weights = Array.from({ length: 100 }, (_, i) => (i + 30).toString()); // 30-130 kg
const HeightWeightPickerScreen = () => {
const [selectedHeight, setSelectedHeight] = useState(heights[0]);
const [selectedWeight, setSelectedWeight] = useState(weights[0]);
const onHeightChange = (index) => {
setSelectedHeight(heights[index]);
};
const onWeightChange = (index) => {
setSelectedWeight(weights[index]);
};
const WheelPickerComponent = Platform.OS === 'android' ? WheelPicker : WheelPickerIOS;
return (
<View style={styles.container}>
<Text style={styles.header}>Set your height & weight</Text>
<View style={styles.pickerContainer}>
<View style={styles.pickerWrapper}>
<Text style={styles.pickerLabel}>Height (cm)</Text>
<WheelPickerComponent
data={heights}
selectedIndex={heights.indexOf(selectedHeight)}
onItemSelected={onHeightChange}
itemTextSize={24}
itemTextColor="#666"
selectedItemTextColor="#3498db"
style={styles.wheelPicker}
/>
<Text style={styles.selectedValue}>{selectedHeight} cm</Text>
</View>
<View style={styles.pickerWrapper}>
<Text style={styles.pickerLabel}>Weight (kg)</Text>
<WheelPickerComponent
data={weights}
selectedIndex={weights.indexOf(selectedWeight)}
onItemSelected={onWeightChange}
itemTextSize={24}
itemTextColor="#666"
selectedItemTextColor="#e74c3c"
style={styles.wheelPicker}
/>
<Text style={styles.selectedValue}>{selectedWeight} kg</Text>
</View>
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#f9f9f9',
},
header: {
fontSize: 28,
fontWeight: 'bold',
color: '#333',
marginBottom: 40,
},
pickerContainer: {
flexDirection: 'row',
justifyContent: 'space-around',
width: '100%',
},
pickerWrapper: {
alignItems: 'center',
width: '45%',
},
pickerLabel: {
fontSize: 18,
color: '#999',
marginBottom: 10,
},
wheelPicker: {
width: 120,
height: 180,
},
selectedValue: {
fontSize: 24,
fontWeight: 'bold',
color: '#333',
marginTop: 10,
},
});
export default HeightWeightPickerScreen;
Let's break down the magic:
- We define two arrays:
heights (140-190 cm) and weights (30-130 kg).
- We use
useState to store the selected values.
- We conditionally render either
WheelPicker (Android) or WheelPickerIOS based on the platform.
- Each wheel picker has its own
onItemSelected handler to update the state.
- We style our screen with a clean, centered layout, using
flexbox for responsiveness.
Step 4: Update App.js to render our new screen
Open App.js and replace its content with:
// App.js
import React from 'react';
import HeightWeightPickerScreen from './screens/HeightWeightPickerScreen';
const App = () => {
return (
<HeightWeightPickerScreen />
);
};
export default App;
Step 5: Run your app
Launch your app on an Android emulator or physical device (or iOS simulator):
npx react-native run-android
# or
npx react-native run-ios
Ta-da!
You should now see a beautiful screen with two gorgeous wheel pickers. Spin those wheels and watch the selected values update in real-time!
Tips & Variations:
- Customize the wheel picker's appearance by tweaking
itemTextSize, itemTextColor, selectedItemTextColor, and style.
- Add more units (e.g., inches, pounds, stones) by modifying the
heights and weights arrays.
- Implement validation or boundaries (e.g., minimum/maximum values).
- Store the selected values in a global state management system (e.g., Redux, MobX) or local storage.
- Animate the selected value text for a smoother experience.
Now, go ahead and show off your fancy wheel pickers to the world!
Happy coding, and may your React Native apps always be Pixel-Perfect!