关于 mobile-development
mobile-development 是面向 React Native 或 Flutter 移动界面开发者的参考技能,涵盖组件模式、导航、响应式布局、平台差异和常见误区。
主要功能
- 01React Native 组件与列表模式
- 02类型化 React Native 导航
- 03Flutter Widget 结构
- 04适配不同屏幕尺寸的布局
- 05平台专属样式与行为
- 06移动开发检查清单
适用场景
- 01构建 React Native 页面结构
- 02配置类型化应用导航
- 03适配手机和平板布局
- 04检查移动代码中的常见误区
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ rohitg00/ awesome-claude-code-toolkit --skill mobile-development安装后在 Claude Code 中输入/mobile-development确认已加载。
详细介绍
移动开发
React Native 组件结构
import { View, Text, FlatList, StyleSheet, Platform } from "react-native";
import { SafeAreaView } from "react-native-safe-area-context";
interface Product {
id: string;
name: string;
price: number;
image: string;
}
function ProductList({ products }: { products: Product[] }) {
return (
<SafeAreaView style={styles.container}>
<FlatList
data={products}
keyExtractor={(item) => item.id}
renderItem={({ item }) => <ProductCard product={item} />}
contentContainerStyle={styles.list}
ItemSeparatorComponent={() => <View style={styles.separator} />}
ListEmptyComponent={<EmptyState message="No products found" />}
initialNumToRender={10}
maxToRenderPerBatch={10}
windowSize={5}
/>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
},
list: {
padding: 16,
},
separator: {
height: 12,
},
});
对于可滚动列表,请使用 FlatList(切勿将 ScrollView 与 .map() 搭配使用)。对于大型列表,请设置 windowSize 和 maxToRenderPerBatch。
React Native 导航
import { NavigationContainer } from "@react-navigation/native";
import { createNativeStackNavigator } from "@react-navigation/native-stack";
import { createBottomTabNavigator } from "@react-navigation/bottom-tabs";
type RootStackParams = {
Tabs: undefined;
ProductDetail: { productId: string };
Cart: undefined;
};
const Stack = createNativeStackNavigator<RootStackParams>();
const Tab = createBottomTabNavigator();
function TabNavigator() {
return (
<Tab.Navigator screenOptions={{ headerShown: false }}>
<Tab.Screen name="Home" component={HomeScreen} />
<Tab.Screen name="Search" component={SearchScreen} />
<Tab.Screen name="Profile" component={ProfileScreen} />
</Tab.Navigator>
);
}
function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Tabs" component={TabNavigator} options={{ headerShown: false }} />
<Stack.Screen name="ProductDetail" component={ProductDetailScreen} />
<Stack.Screen name="Cart" component={CartScreen} options={{ presentation: "modal" }} />
</Stack.Navigator>
</NavigationContainer>
);
}
Flutter Widget 模式
class ProductCard extends StatelessWidget {
final Product product;
final VoidCallback onTap;
const ProductCard({
super.key,
required this.product,
required this.onTap,
});
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onTap,
child: Card(
elevation: 2,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ClipRRect(
borderRadius: const BorderRadius.vertical(top: Radius.circular(8)),
child: Image.network(
product.imageUrl,
height: 200,
width: double.infinity,
fit: BoxFit.cover,
errorBuilder: (_, __, ___) => const Icon(Icons.broken_image, size: 64),
),
),
Padding(
padding: const EdgeInsets.all(12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(product.name, style: Theme.of(context).textTheme.titleMedium),
const SizedBox(height: 4),
Text("\$${product.price.toStringAsFixed(2)}",
style: Theme.of(context).textTheme.bodyLarge),
],
),
),
],
),
),
);
}
}
响应式布局
import { useWindowDimensions } from "react-native";
function useResponsive() {
const { width } = useWindowDimensions();
return {
isPhone: width < 768,
isTablet: width >= 768 && width < 1024,
isDesktop: width >= 1024,
columns: width < 768 ? 1 : width < 1024 ? 2 : 3,
};
}
function ProductGrid({ products }: { products: Product[] }) {
const { columns } = useResponsive();
return (
<FlatList
data={products}
numColumns={columns}
key={columns}
keyExtractor={(item) => item.id}
renderItem={({ item }) => (
<View style={{ flex: 1, maxWidth: `${100 / columns}%`, padding: 8 }}>
<ProductCard product={item} />
</View>
)}
/>
);
}
平台专属代码
import { Platform } from "react-native";
const styles = StyleSheet.create({
shadow: Platform.select({
ios: {
shadowColor: "#000",
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.1,
shadowRadius: 4,
},
android: {
elevation: 4,
},
default: {},
}),
});
反模式
- 对动态列表使用
ScrollView和.map()(请改用FlatList或SectionList) - 将所有状态都存储在全局存储中,而不是放在所属组件附近
- 未处理安全区域(刘海、状态栏、主屏幕指示条)
- 每次渲染都使用内联样式(请使用
StyleSheet.create定义样式) - 因繁重计算阻塞 JS 线程(请使用
InteractionManager) - 忽视平台专属的 UX 规范(iOS 返回手势、Android 返回按钮)
检查清单
- 所有可滚动列表均使用带有
keyExtractor的FlatList - 使用 TypeScript 路由参数定义导航类型
- 使用
SafeAreaView处理安全区域内边距 - 使用
StyleSheet.create定义样式(而非内联对象) - 响应式布局适配手机、平板和桌面设备
- 使用
Platform.select处理平台专属样式 - 图片已缓存,并设置加载和错误状态
- 将繁重计算移出 JS 线程
评论
支持 Markdown