Flutter项目结构 #

一、项目目录概览 #

1.1 默认目录结构 #

text
my_app/
├── android/                # Android原生项目
│   ├── app/
│   ├── gradle/
│   ├── build.gradle
│   └── settings.gradle
├── ios/                    # iOS原生项目
│   ├── Runner/
│   ├── Runner.xcodeproj/
│   └── Podfile
├── web/                    # Web项目
│   ├── index.html
│   ├── manifest.json
│   └── icons/
├── lib/                    # Dart源代码(主要开发目录)
│   └── main.dart           # 应用入口
├── test/                   # 测试代码
│   └── widget_test.dart
├── linux/                  # Linux桌面项目
├── macos/                  # macOS桌面项目
├── windows/                # Windows桌面项目
├── pubspec.yaml            # 项目配置文件
├── pubspec.lock            # 依赖锁定文件
├── analysis_options.yaml   # 代码分析配置
└── README.md               # 项目说明

1.2 核心目录说明 #

目录/文件 说明
lib/ Dart代码目录,主要开发区域
test/ 测试代码目录
android/ Android平台原生代码
ios/ iOS平台原生代码
web/ Web平台资源
pubspec.yaml 项目配置文件

二、lib目录详解 #

2.1 推荐的项目结构 #

text
lib/
├── main.dart               # 应用入口
├── app.dart                # 应用配置
├── models/                 # 数据模型
│   ├── user.dart
│   └── product.dart
├── providers/              # 状态管理
│   ├── user_provider.dart
│   └── theme_provider.dart
├── services/               # 服务层
│   ├── api_service.dart
│   └── storage_service.dart
├── screens/                # 页面
│   ├── home/
│   │   ├── home_screen.dart
│   │   └── home_controller.dart
│   └── settings/
│       └── settings_screen.dart
├── widgets/                # 公共组件
│   ├── common/
│   │   ├── custom_button.dart
│   │   └── loading_indicator.dart
│   └── home/
│       └── product_card.dart
├── utils/                  # 工具类
│   ├── constants.dart
│   ├── validators.dart
│   └── extensions.dart
├── theme/                  # 主题配置
│   ├── app_theme.dart
│   └── colors.dart
└── routes/                 # 路由配置
    ├── app_routes.dart
    └── route_generator.dart

2.2 main.dart示例 #

dart
import 'package:flutter/material.dart';
import 'app.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const MyApp());
}

2.3 app.dart示例 #

dart
import 'package:flutter/material.dart';
import 'screens/home/home_screen.dart';
import 'theme/app_theme.dart';

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My App',
      theme: AppTheme.lightTheme,
      darkTheme: AppTheme.darkTheme,
      home: const HomeScreen(),
      debugShowCheckedModeBanner: false,
    );
  }
}

三、pubspec.yaml配置 #

3.1 完整配置示例 #

yaml
name: my_app
description: 学习 Flutter 跨平台开发,用于移动应用开发
publish_to: 'none'
version: 1.0.0+1

environment:
  sdk: '>=3.0.0 <4.0.0'

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.6
  provider: ^6.1.1
  dio: ^5.4.0
  shared_preferences: ^2.2.2
  flutter_screenutil: ^5.9.0

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: ^3.0.1
  build_runner: ^2.4.7

flutter:
  uses-material-design: true
  
  assets:
    - assets/images/
    - assets/icons/
    - assets/fonts/
  
  fonts:
    - family: CustomFont
      fonts:
        - asset: assets/fonts/CustomFont-Regular.ttf
        - asset: assets/fonts/CustomFont-Bold.ttf
          weight: 700

3.2 配置项说明 #

配置项 说明
name 包名,必须小写
description 项目描述
version 版本号(major.minor.patch+build)
environment.sdk Dart SDK版本约束
dependencies 生产依赖
dev_dependencies 开发依赖
assets 静态资源
fonts 自定义字体

3.3 版本号规则 #

text
version: 1.2.3+4
         │ │ │ │
         │ │ │ └── 构建号(Android: versionCode, iOS: build number)
         │ │ └──── 修订号
         │ └────── 次版本号
         └──────── 主版本号

3.4 添加依赖 #

命令行添加:

bash
flutter pub add provider
flutter pub add dev:build_runner

手动添加后更新:

bash
flutter pub get

四、平台目录详解 #

4.1 Android目录 #

text
android/
├── app/
│   ├── src/
│   │   ├── main/
│   │   │   ├── kotlin/
│   │   │   │   └── com/example/my_app/
│   │   │   │       └── MainActivity.kt
│   │   │   ├── res/
│   │   │   │   └── drawable/
│   │   │   │       └── launch_background.xml
│   │   │   └── AndroidManifest.xml
│   │   └── profile/
│   └── build.gradle
├── gradle/
└── build.gradle

修改包名:

AndroidManifest.xml 中修改 package 属性。

修改应用名:

res/values/strings.xml 中修改。

4.2 iOS目录 #

text
ios/
├── Runner/
│   ├── Assets.xcassets/
│   │   └── AppIcon.appiconset/
│   ├── AppDelegate.swift
│   └── Info.plist
├── Runner.xcodeproj/
│   └── project.pbxproj
└── Podfile

修改Bundle ID:

在 Xcode 中修改 Bundle Identifier

修改应用名:

Info.plist 中修改 CFBundleName

4.3 Web目录 #

text
web/
├── index.html          # 入口HTML
├── manifest.json       # PWA配置
├── favicon.png         # 网站图标
└── icons/              # 各尺寸图标

修改index.html:

html
<!DOCTYPE html>
<html>
<head>
  <base href="$FLUTTER_BASE_HREF">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>My App</title>
  <link rel="manifest" href="manifest.json">
</head>
<body>
  <script src="flutter_bootstrap.js" async></script>
</body>
</html>

五、资源管理 #

5.1 资源目录结构 #

text
assets/
├── images/
│   ├── 2.0x/
│   │   └── logo.png
│   ├── 3.0x/
│   │   └── logo.png
│   └── logo.png
├── icons/
│   └── menu.svg
├── fonts/
│   └── CustomFont.ttf
└── json/
    └── config.json

5.2 配置资源 #

yaml
flutter:
  assets:
    - assets/images/
    - assets/icons/
    - assets/json/config.json

5.3 使用资源 #

图片:

dart
Image.asset('assets/images/logo.png')

JSON:

dart
import 'dart:convert';
import 'package:flutter/services.dart';

Future<Map<String, dynamic>> loadJson() async {
  String data = await rootBundle.loadString('assets/json/config.json');
  return json.decode(data);
}

5.4 图片适配 #

Flutter自动根据设备像素密度选择合适的图片:

text
logo.png       → 1.0x
2.0x/logo.png  → 2.0x
3.0x/logo.png  → 3.0x

六、代码规范 #

6.1 文件命名 #

  • 使用小写字母
  • 使用下划线分隔
  • 示例:user_profile_screen.dart

6.2 类命名 #

  • 使用大驼峰命名
  • 示例:UserProfileScreen

6.3 变量命名 #

  • 使用小驼峰命名
  • 私有变量以下划线开头
  • 示例:userName, _privateField

6.4 常量命名 #

  • 使用小驼峰命名
  • 示例:maxRetryCount

6.5 目录组织原则 #

原则 说明
按功能模块 相关文件放在同一目录
按类型 models、views、controllers分离
混合方式 按功能模块,内部按类型分

七、analysis_options.yaml #

7.1 配置示例 #

yaml
include: package:flutter_lints/flutter.yaml

linter:
  rules:
    - prefer_const_constructors
    - prefer_const_declarations
    - avoid_print
    - prefer_single_quotes
    - sort_child_properties_last

analyzer:
  exclude:
    - "**/*.g.dart"
    - "**/*.freezed.dart"
  errors:
    invalid_annotation_target: ignore

7.2 常用规则 #

规则 说明
prefer_const_constructors 优先使用const构造函数
avoid_print 避免使用print
prefer_single_quotes 优先使用单引号
sort_child_properties_last child属性放最后

八、.gitignore配置 #

gitignore
.dart_tool/
.packages
build/
.flutter-plugins
.flutter-plugins-dependencies
pubspec.lock

.idea/
*.iml
.vscode/

*.log
.DS_Store

android/app/google-services.json
ios/Runner/GoogleService-Info.plist

九、总结 #

9.1 核心要点 #

要点 说明
lib/ 主要开发目录
pubspec.yaml 项目配置文件
assets/ 静态资源目录
平台目录 原生代码配置

9.2 下一步 #

了解了项目结构后,让我们学习 Dart语言基础,掌握Flutter开发语言!

最后更新:2026-03-28