Change the code soo it uses the Material UI Dark Mode… · The Chat Archive
Explore / Programming Change the code soo it uses the Material UI Dark Mode color scheme where the Main color… Save Share Download ▾ ReportChange the code soo it uses the Material UI Dark Mode color scheme where the Main color is Blue.
// screens/RegistrationScreen.js
import React, { useState, useContext } from 'react';
import {
StyleSheet,
ScrollView,
Alert,
KeyboardAvoidingView,
Platform,
View,
} from 'react-native';
import axios from 'axios';
import { Picker } from '@react-native-picker/picker';
import { AuthContext } from '../context/AuthContext';
import {
Text,
TextInput,
Button,
Card,
ActivityIndicator,
useTheme,
} from 'react-native-paper';
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
import CustomMultiSelect from '../Components/CustomMultiSelect.js';
const RegistrationScreen = ({ navigation }) => {
const [username, setUsername] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [desiredPhysique, setDesiredPhysique] = useState('lean');
const [fitnessGoals, setFitnessGoals] = useState([]);
const [dietaryPreferences, setDietaryPreferences] = useState([]);
const [dietaryRestrictions, setDietaryRestrictions] = useState([]);
const [loading, setLoading] = useState(false);
const { signIn } = useContext(AuthContext);
const theme = useTheme();
// Sample options for multi-select fields
const fitnessGoalsOptions = [
{ id: 'strength', name: 'Strength' },
{ id: 'endurance', name: 'Endurance' },
{ id: 'flexibility', name: 'Flexibility' },
{ id: 'weight_loss', name: 'Weight Loss' },
{ id: 'muscle_gain', name: 'Muscle Gain' },
];
const dietaryPreferencesOptions = [
{ id: 'vegetarian', name: 'Vegetarian' },
{ id: 'vegan', name: 'Vegan' },
{ id: 'keto', name: 'Keto' },
{ id: 'paleo', name: 'Paleo' },
{ id: 'mediterranean', name: 'Mediterranean' },
];
const dietaryRestrictionsOptions = [
{ id: 'gluten_free', name: 'Gluten-Free' },
{ id: 'dairy_free', name: 'Dairy-Free' },
{ id: 'nut_allergy', name: 'Nut Allergy' },
{ id: 'lactose_intolerant', name: 'Lactose Intolerant' },
{ id: 'none', name: 'None' },
];
const handleRegistration = async () => {
// Basic validation
if (!username || !email || !password || !desiredPhysique) {
Alert.alert('Error', 'Please fill in all required fields.');
return;
}
Show full message · 576 lines Assistant2024-10-01 19:15 UTC Copy response
Copy first prompt
Details
Provider OpenAI
Model provenance As recorded by source dataset
Turns 1 (2 messages)
Language en
Conversation date Oct 1, 2024
Added to archive Nov 17, 2026
ID 01m7h41vyd86rswr2y8366t9z8Source & license Imported from WildChat-4.8M (AI2) (source ). Imported records earn no Research Points.
Upstream record: 4d3d6b8dfc830f781e98a95371cb6a56
Zhao et al., WildChat: 1M ChatGPT Interaction Logs in the Wild (ICLR 2024). Contains information from WildChat-4.8M, made available under the ODC Attribution License.
License: ODC-BY-1.0
setLoading(true);
try {
const response = await axios.post('http://10.0.0.203:5000/api/auth/register', {
username,
email,
password,
desiredPhysique,
fitnessGoals,
dietaryPreferences,
dietaryRestrictions,
});
if (response.status === 201) {
const { token } = response.data;
Alert.alert('Success', 'Registration successful!', [
{ text: 'OK', onPress: () => navigation.navigate('Login') },
]);
signIn(token);
}
} catch (error) {
console.error('Registration Error:', error);
console.error('Backend URL:', '10.0.0.203:5000');
if (error.response) {
console.error('Error Response:', error.response.data);
Alert.alert('Registration Failed', error.response.data.error || error.response.data.message);
} else if (error.request) {
console.error('No Response:', error.request);
Alert.alert('Registration Failed', 'No response from server. Please try again.');
} else {
console.error('Error Setting Up Request:', error.message);
Alert.alert('Registration Failed', error.message);
}
} finally {
setLoading(false);
}
return (
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
style={styles.container}
>
<Card.Title
title="Create Account"
titleStyle={styles.cardTitle}
left={(props) => <Icon {...props} name="account-plus" size={30} color={theme.colors.primary} />}
/>
<Card.Content>
<TextInput
label="Username"
value={username}
onChangeText={setUsername}
mode="outlined"
style={styles.input}
autoCapitalize="none"
left={<TextInput.Icon name="account" color={theme.colors.placeholder} />}
placeholderTextColor={theme.colors.placeholder}
/>
<TextInput
label="Email"
value={email}
onChangeText={setEmail}
mode="outlined"
style={styles.input}
keyboardType="email-address"
autoCapitalize="none"
left={<TextInput.Icon name="email" color={theme.colors.placeholder} />}
placeholderTextColor={theme.colors.placeholder}
/>
<TextInput
label="Password"
value={password}
onChangeText={setPassword}
mode="outlined"
style={styles.input}
secureTextEntry
left={<TextInput.Icon name="lock" color={theme.colors.placeholder} />}
placeholderTextColor={theme.colors.placeholder}
/>
code Copy code
<Text style={styles.label}>Desired Physique</Text>
<View style={[styles.pickerContainer, { borderColor: theme.colors.placeholder }]}>
<Picker
selectedValue={desiredPhysique}
onValueChange={(itemValue) => setDesiredPhysique(itemValue)}
style={[styles.picker, { color: theme.colors.text }]}
dropdownIconColor={theme.colors.placeholder}
mode="dropdown"
>
<Picker.Item label="Lean" value="lean" />
<Picker.Item label="Muscular" value="muscular" />
<Picker.Item label="Athletic" value="athletic" />
</Picker>
</View>
{/* Custom MultiSelect Components */}
<CustomMultiSelect
label="Fitness Goals"
items={fitnessGoalsOptions}
selectedItems={fitnessGoals}
onSelectedItemsChange={setFitnessGoals}
placeholder="Select Fitness Goals"
/>
<CustomMultiSelect
label="Dietary Preferences"
items={dietaryPreferencesOptions}
selectedItems={dietaryPreferences}
onSelectedItemsChange={setDietaryPreferences}
placeholder="Select Dietary Preferences"
/>
<CustomMultiSelect
label="Dietary Restrictions"
items={dietaryRestrictionsOptions}
selectedItems={dietaryRestrictions}
onSelectedItemsChange={setDietaryRestrictions}
placeholder="Select Dietary Restrictions"
/>
{loading ? (
<ActivityIndicator animating={true} color={theme.colors.primary} style={styles.loader} />
) : (
<Button
mode="contained"
onPress={handleRegistration}
style={styles.button}
contentStyle={styles.buttonContent}
disabled={loading}
>
Register
</Button>
)}
<View style={styles.loginRedirect}>
<Text style={styles.redirectText}>Already have an account?</Text>
<Button
mode="text"
onPress={() => navigation.navigate('Login')}
compact
uppercase={false}
labelStyle={styles.redirectButtonText}
>
Login
</Button>
</View>
</Card.Content>
</Card>
</ScrollView>
</KeyboardAvoidingView>
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#121212', // Dark background
},
scrollContainer: {
flexGrow: 1,
justifyContent: 'center',
padding: 20,
},
card: {
backgroundColor: '#1e1e1e',
borderRadius: 10,
padding: 10,
elevation: 5,
},
cardTitle: {
color: '#ffffff',
fontSize: 24,
fontWeight: 'bold',
},
input: {
backgroundColor: '#2c2c2c',
marginBottom: 10,
},
label: {
fontSize: 16,
color: '#ffffff',
marginTop: 10,
marginBottom: 5,
},
pickerContainer: {
borderRadius: 5,
borderWidth: 1,
backgroundColor: '#2c2c2c',
marginBottom: 15,
overflow: 'hidden',
},
picker: {
height: 50,
width: '100%',
},
button: {
marginTop: 10,
borderRadius: 25,
},
buttonContent: {
paddingVertical: 8,
},
loginRedirect: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
marginTop: 20,
},
redirectText: {
color: '#ffffff',
},
redirectButtonText: {
color: '#03dac6',
fontWeight: '600',
},
loader: {
marginTop: 10,
},
});
export default RegistrationScreen;
// screens/LoginScreen.js
import React, { useState, useContext } from 'react';
import {
StyleSheet,
Alert,
KeyboardAvoidingView,
Platform,
ScrollView,
View,
} from 'react-native';
import axios from 'axios';
import { AuthContext } from '../context/AuthContext';
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
import { LinearGradient } from 'expo-linear-gradient';
import {
Text,
TextInput,
Button,
Card,
ActivityIndicator,
useTheme,
} from 'react-native-paper';
const LoginScreen = ({ navigation }) => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
code Copy code
const { signIn } = useContext(AuthContext);
const [error, setError] = useState(null);
const [loading, setLoading] = useState(false);
const theme = useTheme();
const handleLogin = async () => {
if (!email || !password) {
Alert.alert('Error', 'Please enter both email and password.');
return;
}
setLoading(true);
try {
const res = await axios.post('http://10.0.0.203:5000/api/auth/login', {
email,
password
});
const { token } = res.data;
signIn(token);
} catch (err) {
console.error(err);
setError(err.response?.data?.message || 'Login failed');
Alert.alert('Login Failed', err.response?.data?.message || 'Invalid credentials');
} finally {
setLoading(false);
}
};
return (
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
style={styles.container}
>
<ScrollView
contentContainerStyle={styles.scrollContainer}
keyboardShouldPersistTaps="handled"
>
<Card style={styles.card}>
<Card.Title
title="Login"
titleStyle={styles.cardTitle}
left={(props) => <Icon {...props} name="login" size={30} color={theme.colors.primary} />}
/>
<Card.Content>
{/* Email Input */}
<TextInput
label="Email"
value={email}
onChangeText={setEmail}
mode="outlined"
style={styles.input}
keyboardType="email-address"
autoCapitalize="none"
left={<TextInput.Icon name="email" color={theme.colors.placeholder} />}
placeholderTextColor={theme.colors.placeholder}
/>
{/* Password Input */}
<TextInput
label="Password"
value={password}
onChangeText={setPassword}
mode="outlined"
style={styles.input}
secureTextEntry
left={<TextInput.Icon name="lock" color={theme.colors.placeholder} />}
placeholderTextColor={theme.colors.placeholder}
/>
{/* Error Message */}
{error && <Text style={styles.errorText}>{error}</Text>}
{/* Login Button */}
{loading ? (
<ActivityIndicator animating={true} color={theme.colors.primary} style={styles.loader} />
) : (
<Button
mode="contained"
onPress={handleLogin}
style={styles.button}
contentStyle={styles.buttonContent}
disabled={loading}
>
Login
</Button>
)}
{/* Forgot Password */}
<Button
mode="text"
onPress={() => navigation.navigate('ForgotPassword')}
style={styles.forgotPassword}
labelStyle={styles.forgotPasswordText}
>
Forgot Password?
</Button>
{/* Divider */}
<View style={styles.dividerContainer}>
<View style={styles.line} />
<Text style={styles.dividerText}>OR</Text>
<View style={styles.line} />
</View>
{/* Social Login Buttons */}
<Button
mode="outlined"
icon="facebook"
onPress={() => {}}
style={styles.socialButton}
contentStyle={styles.socialButtonContent}
labelStyle={{ color: '#3b5998' }}
>
Login with Facebook
</Button>
<Button
mode="outlined"
icon="google"
onPress={() => {}}
style={styles.socialButton}
contentStyle={styles.socialButtonContent}
labelStyle={{ color: '#db4a39' }}
>
Login with Google
</Button>
{/* Register Redirect */}
<View style={styles.registerRedirect}>
<Text style={styles.redirectText}>Don't have an account?</Text>
<Button
mode="text"
onPress={() => navigation.navigate('Register')}
compact
uppercase={false}
labelStyle={styles.redirectButtonText}
>
Register
</Button>
</View>
</Card.Content>
</Card>
</ScrollView>
</KeyboardAvoidingView>
);
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#121212', // Dark background
},
scrollContainer: {
flexGrow: 1,
justifyContent: 'center',
padding: 20,
},
card: {
backgroundColor: '#1e1e1e',
borderRadius: 10,
padding: 10,
elevation: 5,
},
cardTitle: {
color: '#ffffff',
fontSize: 24,
fontWeight: 'bold',
},
input: {
backgroundColor: '#2c2c2c',
marginBottom: 10,
},
errorText: {
color: '#cf6679',
textAlign: 'center',
marginBottom: 10,
},
button: {
marginTop: 10,
borderRadius: 25,
},
buttonContent: {
paddingVertical: 8,
},
loader: {
marginTop: 10,
},
forgotPassword: {
marginTop: 10,
},
forgotPasswordText: {
color: '#03dac6',
textAlign: 'center',
},
dividerContainer: {
flexDirection: 'row',
alignItems: 'center',
marginVertical: 20,
},
line: {
flex: 1,
height: 1,
backgroundColor: '#3e3e3e',
},
dividerText: {
marginHorizontal: 10,
color: '#ffffff',
},
socialButton: {
marginBottom: 10,
},
socialButtonContent: {
flexDirection: 'row',
justifyContent: 'center',
},
registerRedirect: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
marginTop: 10,
},
redirectText: {
color: '#ffffff',
},
redirectButtonText: {
color: '#03dac6',
fontWeight: '600',
},
});
export default LoginScreen;
// App.js
import React from 'react';
import { AuthProvider } from './context/AuthContext';
import AppNavigator from './navigation/AppNavigator';
import { Provider as PaperProvider, DarkTheme } from 'react-native-paper';
import { StatusBar } from 'expo-status-bar';
const App = () => {
return (
);
};
To implement the Material UI Dark Mode color scheme with Blue as the main color in your React Native application using react-native-paper, you'll need to perform the following steps:
Define a Custom Dark Theme with Blue as the Primary Color
Apply the Theme to the Entire Application
Update Your Screens to Utilize the Theme Colors Instead of Hardcoded Values
Below are detailed steps and the updated code for App.js, RegistrationScreen.js, and LoginScreen.js. Additionally, ensure that any custom components like CustomMultiSelect are also adjusted to use the theme.
1. Define a Custom Dark Theme with Blue as the Primary Color
First, you'll create a custom theme by extending the default DarkTheme provided by react-native-paper. This theme will set Blue (#2196F3) as the primary color.
App.js
javascript Copy code
// App.js
import React from 'react';
import { AuthProvider } from './context/AuthContext';
import AppNavigator from './navigation/AppNavigator';
import { Provider as PaperProvider, DarkTheme as PaperDarkTheme } from 'react-native-paper';
import { StatusBar } from 'expo-status-bar';
// Define a custom dark theme with Blue as the primary color
const customTheme = {
...PaperDarkTheme,
colors: {
...PaperDarkTheme.colors,
primary: '#2196F3', // Blue as the primary color
accent: '#03dac6', // You can customize the accent color if desired
background: '#121212', // Default dark background
surface: '#1e1e1e', // Default surface color (e.g., for cards)
text: '#ffffff', // Text color
placeholder: '#a1a1a1', // Placeholder text color
error: '#cf6679', // Error text color
},
};
const App = () => {
return (
<AuthProvider>
<PaperProvider theme={customTheme}>
<StatusBar style="light" />
<AppNavigator />
</PaperProvider>
</AuthProvider>
);
};
export default App;
2. Apply the Theme to the Screens
For each screen (RegistrationScreen.js and LoginScreen.js), you'll utilize the useTheme hook from react-native-paper to access theme colors and replace any hardcoded color values with the theme's colors.
RegistrationScreen.js
Changes Implemented:
Removed hardcoded color values and replaced them with theme-based colors.
Updated styles to be dynamic by implementing a getStyles function that takes the theme as a parameter.
Ensured that all text, backgrounds, and other UI elements adhere to the dark theme.
LoginScreen.js
Changes Implemented:
Similar to RegistrationScreen.js, replaced hardcoded color values with theme-based colors.
Updated styles dynamically to adhere to the dark theme.
3. Update Custom Components (e.g., CustomMultiSelect)
If you have custom components like CustomMultiSelect, ensure they also utilize the theme colors. Here's a generic example of how you might update such a component.
CustomMultiSelect.js
javascript Copy code
// Components/CustomMultiSelect.js
import React from 'react';
import { View, StyleSheet } from 'react-native';
import { Text, HelperText, useTheme } from 'react-native-paper';
import MultiSelect from 'react-native-multiple-select'; // Assuming you're using this library
const CustomMultiSelect = ({ label, items, selectedItems, onSelectedItemsChange, placeholder }) => {
const theme = useTheme();
const styles = getStyles(theme);
return (
<View style={styles.container}>
<Text style={styles.label}>{label}</Text>
<MultiSelect
items={items}
uniqueKey="id"
onSelectedItemsChange={onSelectedItemsChange}
selectedItems={selectedItems}
selectText={placeholder}
searchInputPlaceholderText="Search Items..."
tagRemoveIconColor={theme.colors.primary}
tagBorderColor={theme.colors.primary}
tagTextColor={theme.colors.primary}
selectedItemTextColor={theme.colors.primary}
selectedItemIconColor={theme.colors.primary}
itemTextColor={theme.colors.text}
searchInputStyle={{ color: theme.colors.text }}
submitButtonColor={theme.colors.primary}
submitButtonText="Submit"
styleMainWrapper={styles.multiSelectWrapper}
styleDropdownMenuSubsection={styles.dropdownSubsection}
/>
{/* Optionally add helper text or error messages */}
</View>
);
};
const getStyles = (theme) => StyleSheet.create({
container: {
marginBottom: 15,
},
label: {
fontSize: 16,
marginBottom: 5,
color: theme.colors.text,
},
multiSelectWrapper: {
backgroundColor: theme.colors.background,
borderColor: theme.colors.placeholder,
},
dropdownSubsection: {
backgroundColor: theme.colors.background,
borderColor: theme.colors.placeholder,
},
});
export default CustomMultiSelect;
Note: Adjust the above component based on the actual multi-select library you are using and its available props.
4. Summary of Changes
Theme Definition: Created a customTheme in App.js extending DarkTheme from react-native-paper and set Blue (#2196F3) as the primary color.
Provider Application: Wrapped the entire application with PaperProvider and passed the customTheme to ensure all nested components receive the theme context.
Dynamic Styling: Updated RegistrationScreen.js and LoginScreen.js to utilize useTheme and dynamic styles. Replaced all hardcoded color values with corresponding theme colors.
Custom Components: Ensured custom components like CustomMultiSelect also utilize theme colors for consistency across the app.
Additional Tips
Consistent Theming:
Always use theme.colors for styling to maintain consistency.
Avoid hardcoding colors directly in styles; instead, use theme values.
Handling Light and Dark Modes:
If you plan to support both light and dark modes, you can toggle between themes by managing state (e.g., using a toggle switch) and providing the appropriate theme to PaperProvider.
Testing:
After implementing the changes, thoroughly test each screen to ensure all elements adhere to the dark theme and Blue primary color.
Pay special attention to custom components and ensure they respond correctly to theme changes.
Extending the Theme:
You can further customize the theme by adjusting other color properties or overriding default component styles as needed.
By following the above steps, your application should now adhere to the Material UI Dark Mode color scheme with Blue as the main color, providing a visually consistent and aesthetically pleasing user experience.
Show full message · 712 lines Copy first response
Not yet included in a published release.
// screens/RegistrationScreen.js
import React, { useState, useContext } from 'react';
import {
StyleSheet,
ScrollView,
Alert,
KeyboardAvoidingView,
Platform,
View,
} from 'react-native';
import axios from 'axios';
import { Picker } from '@react-native-picker/picker';
import { AuthContext } from '../context/AuthContext';
import {
Text,
TextInput,
Button,
Card,
ActivityIndicator,
useTheme,
} from 'react-native-paper';
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
import CustomMultiSelect from '../Components/CustomMultiSelect.js';
const RegistrationScreen = ({ navigation }) => {
const [username, setUsername] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [desiredPhysique, setDesiredPhysique] = useState('lean');
const [fitnessGoals, setFitnessGoals] = useState([]);
const [dietaryPreferences, setDietaryPreferences] = useState([]);
const [dietaryRestrictions, setDietaryRestrictions] = useState([]);
const [loading, setLoading] = useState(false);
const { signIn } = useContext(AuthContext);
const theme = useTheme();
const styles = getStyles(theme);
// Sample options for multi-select fields
const fitnessGoalsOptions = [
{ id: 'strength', name: 'Strength' },
{ id: 'endurance', name: 'Endurance' },
{ id: 'flexibility', name: 'Flexibility' },
{ id: 'weight_loss', name: 'Weight Loss' },
{ id: 'muscle_gain', name: 'Muscle Gain' },
];
const dietaryPreferencesOptions = [
{ id: 'vegetarian', name: 'Vegetarian' },
{ id: 'vegan', name: 'Vegan' },
{ id: 'keto', name: 'Keto' },
{ id: 'paleo', name: 'Paleo' },
{ id: 'mediterranean', name: 'Mediterranean' },
];
const dietaryRestrictionsOptions = [
{ id: 'gluten_free', name: 'Gluten-Free' },
{ id: 'dairy_free', name: 'Dairy-Free' },
{ id: 'nut_allergy', name: 'Nut Allergy' },
{ id: 'lactose_intolerant', name: 'Lactose Intolerant' },
{ id: 'none', name: 'None' },
];
const handleRegistration = async () => {
// Basic validation
if (!username || !email || !password || !desiredPhysique) {
Alert.alert('Error', 'Please fill in all required fields.');
return;
}
setLoading(true);
try {
const response = await axios.post('http://10.0.0.203:5000/api/auth/register', {
username,
email,
password,
desiredPhysique,
fitnessGoals,
dietaryPreferences,
dietaryRestrictions,
});
if (response.status === 201) {
const { token } = response.data;
Alert.alert('Success', 'Registration successful!', [
{ text: 'OK', onPress: () => navigation.navigate('Login') },
]);
signIn(token);
}
} catch (error) {
console.error('Registration Error:', error);
console.error('Backend URL:', '10.0.0.203:5000');
if (error.response) {
console.error('Error Response:', error.response.data);
Alert.alert('Registration Failed', error.response.data.error || error.response.data.message);
} else if (error.request) {
console.error('No Response:', error.request);
Alert.alert('Registration Failed', 'No response from server. Please try again.');
} else {
console.error('Error Setting Up Request:', error.message);
Alert.alert('Registration Failed', error.message);
}
} finally {
setLoading(false);
}
};
return (
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
style={styles.container}
>
<ScrollView contentContainerStyle={styles.scrollContainer}>
<Card style={styles.card}>
<Card.Title
title="Create Account"
titleStyle={styles.cardTitle}
left={(props) => <Icon {...props} name="account-plus" size={30} color={theme.colors.primary} />}
/>
<Card.Content>
<TextInput
label="Username"
value={username}
onChangeText={setUsername}
mode="outlined"
style={styles.input}
autoCapitalize="none"
left={<TextInput.Icon name="account" color={theme.colors.placeholder} />}
placeholderTextColor={theme.colors.placeholder}
/>
<TextInput
label="Email"
value={email}
onChangeText={setEmail}
mode="outlined"
style={styles.input}
keyboardType="email-address"
autoCapitalize="none"
left={<TextInput.Icon name="email" color={theme.colors.placeholder} />}
placeholderTextColor={theme.colors.placeholder}
/>
<TextInput
label="Password"
value={password}
onChangeText={setPassword}
mode="outlined"
style={styles.input}
secureTextEntry
left={<TextInput.Icon name="lock" color={theme.colors.placeholder} />}
placeholderTextColor={theme.colors.placeholder}
/>
<Text style={[styles.label]}>Desired Physique</Text>
<View style={[styles.pickerContainer, { borderColor: theme.colors.placeholder }]}>
<Picker
selectedValue={desiredPhysique}
onValueChange={(itemValue) => setDesiredPhysique(itemValue)}
style={[styles.picker, { color: theme.colors.text }]}
dropdownIconColor={theme.colors.placeholder}
mode="dropdown"
>
<Picker.Item label="Lean" value="lean" />
<Picker.Item label="Muscular" value="muscular" />
<Picker.Item label="Athletic" value="athletic" />
</Picker>
</View>
{/* Custom MultiSelect Components */}
<CustomMultiSelect
label="Fitness Goals"
items={fitnessGoalsOptions}
selectedItems={fitnessGoals}
onSelectedItemsChange={setFitnessGoals}
placeholder="Select Fitness Goals"
/>
<CustomMultiSelect
label="Dietary Preferences"
items={dietaryPreferencesOptions}
selectedItems={dietaryPreferences}
onSelectedItemsChange={setDietaryPreferences}
placeholder="Select Dietary Preferences"
/>
<CustomMultiSelect
label="Dietary Restrictions"
items={dietaryRestrictionsOptions}
selectedItems={dietaryRestrictions}
onSelectedItemsChange={setDietaryRestrictions}
placeholder="Select Dietary Restrictions"
/>
{loading ? (
<ActivityIndicator animating={true} color={theme.colors.primary} style={styles.loader} />
) : (
<Button
mode="contained"
onPress={handleRegistration}
style={styles.button}
contentStyle={styles.buttonContent}
disabled={loading}
>
Register
</Button>
)}
<View style={styles.loginRedirect}>
<Text style={{ color: theme.colors.text, marginRight: 5 }}>Already have an account?</Text>
<Button
mode="text"
onPress={() => navigation.navigate('Login')}
compact
uppercase={false}
labelStyle={{ color: theme.colors.primary, fontWeight: '600' }}
>
Login
</Button>
</View>
</Card.Content>
</Card>
</ScrollView>
</KeyboardAvoidingView>
);
};
// Dynamic styles based on the current theme
const getStyles = (theme) => StyleSheet.create({
container: {
flex: 1,
backgroundColor: theme.colors.background, // Uses theme background
},
scrollContainer: {
flexGrow: 1,
justifyContent: 'center',
padding: 20,
},
card: {
backgroundColor: theme.colors.surface, // Uses theme surface color
borderRadius: 10,
padding: 10,
elevation: 5,
},
cardTitle: {
color: theme.colors.text, // Uses theme text color
fontSize: 24,
fontWeight: 'bold',
},
input: {
backgroundColor: theme.colors.background, // Matches theme background
marginBottom: 10,
},
label: {
fontSize: 16,
marginTop: 10,
marginBottom: 5,
color: theme.colors.text, // Uses theme text color
},
pickerContainer: {
borderRadius: 5,
borderWidth: 1,
backgroundColor: theme.colors.background, // Matches theme background
marginBottom: 15,
overflow: 'hidden',
},
picker: {
height: 50,
width: '100%',
},
button: {
marginTop: 10,
borderRadius: 25,
},
buttonContent: {
paddingVertical: 8,
},
loginRedirect: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
marginTop: 20,
},
loader: {
marginTop: 10,
},
});
export default RegistrationScreen;
// screens/LoginScreen.js
import React, { useState, useContext } from 'react';
import {
StyleSheet,
Alert,
KeyboardAvoidingView,
Platform,
ScrollView,
View,
} from 'react-native';
import axios from 'axios';
import { AuthContext } from '../context/AuthContext';
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
import {
Text,
TextInput,
Button,
Card,
ActivityIndicator,
useTheme,
} from 'react-native-paper';
const LoginScreen = ({ navigation }) => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const { signIn } = useContext(AuthContext);
const [error, setError] = useState(null);
const [loading, setLoading] = useState(false);
const theme = useTheme();
const styles = getStyles(theme);
const handleLogin = async () => {
if (!email || !password) {
Alert.alert('Error', 'Please enter both email and password.');
return;
}
setLoading(true);
try {
const res = await axios.post('http://10.0.0.203:5000/api/auth/login', {
email,
password
});
const { token } = res.data;
signIn(token);
} catch (err) {
console.error(err);
setError(err.response?.data?.message || 'Login failed');
Alert.alert('Login Failed', err.response?.data?.message || 'Invalid credentials');
} finally {
setLoading(false);
}
};
return (
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
style={styles.container}
>
<ScrollView
contentContainerStyle={styles.scrollContainer}
keyboardShouldPersistTaps="handled"
>
<Card style={styles.card}>
<Card.Title
title="Login"
titleStyle={styles.cardTitle}
left={(props) => <Icon {...props} name="login" size={30} color={theme.colors.primary} />}
/>
<Card.Content>
{/* Email Input */}
<TextInput
label="Email"
value={email}
onChangeText={setEmail}
mode="outlined"
style={styles.input}
keyboardType="email-address"
autoCapitalize="none"
left={<TextInput.Icon name="email" color={theme.colors.placeholder} />}
placeholderTextColor={theme.colors.placeholder}
/>
{/* Password Input */}
<TextInput
label="Password"
value={password}
onChangeText={setPassword}
mode="outlined"
style={styles.input}
secureTextEntry
left={<TextInput.Icon name="lock" color={theme.colors.placeholder} />}
placeholderTextColor={theme.colors.placeholder}
/>
{/* Error Message */}
{error && <Text style={[styles.errorText]}>{error}</Text>}
{/* Login Button */}
{loading ? (
<ActivityIndicator animating={true} color={theme.colors.primary} style={styles.loader} />
) : (
<Button
mode="contained"
onPress={handleLogin}
style={styles.button}
contentStyle={styles.buttonContent}
disabled={loading}
>
Login
</Button>
)}
{/* Forgot Password */}
<Button
mode="text"
onPress={() => navigation.navigate('ForgotPassword')}
style={styles.forgotPassword}
labelStyle={{ color: theme.colors.primary }}
>
Forgot Password?
</Button>
{/* Divider */}
<View style={styles.dividerContainer}>
<View style={[styles.line, { backgroundColor: theme.colors.placeholder }]} />
<Text style={[styles.dividerText, { color: theme.colors.text }]}>OR</Text>
<View style={[styles.line, { backgroundColor: theme.colors.placeholder }]} />
</View>
{/* Social Login Buttons */}
<Button
mode="outlined"
icon="facebook"
onPress={() => {}}
style={styles.socialButton}
contentStyle={styles.socialButtonContent}
labelStyle={{ color: '#3b5998' }}
>
Login with Facebook
</Button>
<Button
mode="outlined"
icon="google"
onPress={() => {}}
style={styles.socialButton}
contentStyle={styles.socialButtonContent}
labelStyle={{ color: '#db4a39' }}
>
Login with Google
</Button>
{/* Register Redirect */}
<View style={styles.registerRedirect}>
<Text style={{ color: theme.colors.text, marginRight: 5 }}>Don't have an account?</Text>
<Button
mode="text"
onPress={() => navigation.navigate('Register')}
compact
uppercase={false}
labelStyle={{ color: theme.colors.primary, fontWeight: '600' }}
>
Register
</Button>
</View>
</Card.Content>
</Card>
</ScrollView>
</KeyboardAvoidingView>
);
};
// Dynamic styles based on the current theme
const getStyles = (theme) => StyleSheet.create({
container: {
flex: 1,
backgroundColor: theme.colors.background, // Uses theme background
},
scrollContainer: {
flexGrow: 1,
justifyContent: 'center',
padding: 20,
},
card: {
backgroundColor: theme.colors.surface, // Uses theme surface color
borderRadius: 10,
padding: 10,
elevation: 5,
},
cardTitle: {
color: theme.colors.text, // Uses theme text color
fontSize: 24,
fontWeight: 'bold',
},
input: {
backgroundColor: theme.colors.background, // Matches theme background
marginBottom: 10,
},
errorText: {
color: theme.colors.error, // Uses theme error color
textAlign: 'center',
marginBottom: 10,
},
button: {
marginTop: 10,
borderRadius: 25,
},
buttonContent: {
paddingVertical: 8,
},
loader: {
marginTop: 10,
},
forgotPassword: {
marginTop: 10,
},
dividerContainer: {
flexDirection: 'row',
alignItems: 'center',
marginVertical: 20,
},
line: {
flex: 1,
height: 1,
backgroundColor: theme.colors.placeholder, // Uses theme placeholder color
},
dividerText: {
marginHorizontal: 10,
color: theme.colors.text, // Uses theme text color
},
socialButton: {
marginBottom: 10,
},
socialButtonContent: {
flexDirection: 'row',
justifyContent: 'center',
},
registerRedirect: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
marginTop: 10,
},
});
export default LoginScreen;