Full-Width Country Picker with Flags
Screen is created using these React Native Core Components: <SafeAreaView />, <TouchableOpacity />, <ScrollView />, <Image />, <View />, <Text />
import React from 'react';
import {
StyleSheet,
SafeAreaView,
TouchableOpacity,
ScrollView,
Image,
View,
Text,
} from 'react-native';
import FeatherIcon from 'react-native-vector-icons/Feather''@expo/vector-icons/Feather';
import FontAwesome from 'react-native-vector-icons/FontAwesome5''@expo/vector-icons/FontAwesome5';
const countries = [
{ id: 'US', name: 'United States' },
{ id: 'GB', name: 'United Kingdom' },
{ id: 'AF', name: 'Afghanistan' },
{ id: 'AL', name: 'Albania' },
{ id: 'DZ', name: 'Algeria' },
{ id: 'AS', name: 'American Samoa' },
{ id: 'AD', name: 'Andorra' },
{ id: 'AO', name: 'Angola' },
{ id: 'AI', name: 'Anguilla' },
{ id: 'AQ', name: 'Antarctica' },
{ id: 'AG', name: 'Antigua and Barbuda' },
{ id: 'AR', name: 'Argentina' },
{ id: 'AM', name: 'Armenia' },
{ id: 'AW', name: 'Aruba' },
{ id: 'AU', name: 'Australia' },
{ id: 'AT', name: 'Austria' },
// other countries
];
export default function Example() {
const [value, setValue] = React.useState(0);
return (
<SafeAreaView style={{ flex: 1, backgroundColor: '#f6f6f6' }}>
<TouchableOpacity
onPress={() => {
// handle onPress
}}
style={styles.headerClose}>
<FeatherIcon color="#1d1d1d" name="x" size={24} />
</TouchableOpacity>
<View style={styles.header}>
<Text style={styles.headerTitle}>Select Country</Text>
<Text style={styles.headerSubtitle}>
The terms and services which apply to you will depend on your country of residence
</Text>
</View>
<ScrollView contentContainerStyle={styles.content}>
{countries.map(({ id, name }, index) => {
const isActive = value === index;
return (
<TouchableOpacity
key={id}
onPress={() => {
setValue(index);
}}
style={styles.radioWrapper}>
<Image
alt={`Flag of ${name}`}
style={styles.radioImage}
source={{
uri: `https://flagsapi.com/${id}/flat/64.png`,
}} />
<View
style={[styles.radio, index === 0 && { borderTopWidth: 0 }]}>
<Text style={styles.radioLabel}>{name}</Text>
<View
style={[
styles.radioCheck,
isActive && styles.radioCheckActive,
]}>
<FontAwesome
color="#fff"
name="check"
style={!isActive && { display: 'none' }}
size={12} />
</View>
</View>
</TouchableOpacity>
);
})}
</ScrollView>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
content: {
marginTop: 12,
backgroundColor: '#fff',
borderTopWidth: 1,
borderBottomWidth: 1,
borderColor: '#e8e8e8',
},
/** Header */
header: {
paddingHorizontal: 24,
marginBottom: 12,
},
headerClose: {
alignSelf: 'flex-end',
paddingHorizontal: 20,
marginTop: 6,
marginBottom: 16,
},
headerTitle: {
fontSize: 32,
fontWeight: '700',
color: '#1d1d1d',
},
headerSubtitle: {
fontSize: 15,
fontWeight: '500',
color: '#929292',
marginTop: 6,
},
/** Radio */
radio: {
flexGrow: 1,
flexShrink: 1,
flexBasis: 0,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
borderTopWidth: 1,
borderColor: '#e8e8e8',
height: 54,
paddingRight: 24,
},
radioWrapper: {
paddingLeft: 24,
position: 'relative',
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'flex-start',
},
radioImage: {
width: 30,
height: 30,
marginRight: 12,
alignItems: 'center',
justifyContent: 'center',
},
radioLabel: {
fontSize: 17,
fontWeight: '600',
color: '#222222',
marginBottom: 2,
},
radioCheck: {
width: 22,
height: 22,
borderRadius: 9999,
alignItems: 'center',
justifyContent: 'center',
marginLeft: 'auto',
borderWidth: 1,
borderColor: '#999B9A',
},
radioCheckActive: {
borderColor: '#007bff',
backgroundColor: '#007bff',
},
});Modal Integration 📄
UI/UX implementation of the React Native Component in Modal.
import React from 'react';
import { StyleSheet, Text, SafeAreaView, ScrollView, StatusBar } from 'react-native';
const App = () => {
return (
<SafeAreaView style={styles.container}>
<ScrollView style={styles.scrollView}>
<Text style={styles.text}>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.
</Text>
<View>
<Text style={styles.text}>in voluptate velit esse cillum dolore eu fugiat nulla</Text>
</View>
</ScrollView>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: StatusBar.currentHeight,
},
scrollView: {
backgroundColor: 'pink',
marginHorizontal: 20,
},
text: {
fontSize: 42,
},
});
export default App;
Dependencies
Before getting started, make sure you have all the necessary dependencies for this component. Follow the steps below to install any missing dependencies.
-
Install the package.
npm install --save react-native-vector-icons
-
Link the native packages for iOS.
npx pod-install
In React Native 0.60+ the CLI autolink feature links the module while building the app.
-
Add Fonts to the
Info.plistfile.
Open your XCode project, right click on the Info.plist, and select Open As -> Source Code.
Next, copy the fonts below into your Info.plist.
<key>UIAppFonts</key>
<array>
<string>AntDesign.ttf</string>
<string>Entypo.ttf</string>
<string>EvilIcons.ttf</string>
<string>Feather.ttf</string>
<string>FontAwesome.ttf</string>
<string>FontAwesome5_Brands.ttf</string>
<string>FontAwesome5_Regular.ttf</string>
<string>FontAwesome5_Solid.ttf</string>
<string>FontAwesome6_Brands.ttf</string>
<string>FontAwesome6_Regular.ttf</string>
<string>FontAwesome6_Solid.ttf</string>
<string>Foundation.ttf</string>
<string>Ionicons.ttf</string>
<string>MaterialIcons.ttf</string>
<string>MaterialCommunityIcons.ttf</string>
<string>SimpleLineIcons.ttf</string>
<string>Octicons.ttf</string>
<string>Zocial.ttf</string>
<string>Fontisto.ttf</string>
</array>
Note: You can only select the fonts you would like to use in your React Native Application.
After you added these fonts the Info.plist file should look like this:
![]()
-
Add
fonts.gradlefor Android.
Open android/app/build.gradle (NOT android/build.gradle) and add the following code to the bottom:
apply from: file("../../node_modules/react-native-vector-icons/fonts.gradle")
If you want to customize fonts, use:
project.ext.vectoricons = [
iconFontNames: [ 'MaterialIcons.ttf', 'EvilIcons.ttf' ]
]
apply from: file("../../node_modules/react-native-vector-icons/fonts.gradle")
Original instructions can be found on Github: https://github.com/oblador/react-native-vector-icons