Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Flutter Project Structure

Flutter Project Structure

Understanding the directory structure of a Flutter project is essential for efficient development.

This section will describe in detail the components of a Flutter project and their functions.


Project directory structure

When you use flutter create After creating a new project, you will get the following directory structure:



my_app/

├── android/          # Android 平台相关代码

├── ios/              # iOS 平台相关代码

├── lib/              # Dart 代码目录(主要开发目录)

│   └── main.dart     # 应用入口文件

├── web/              # Web 平台相关代码

├── windows/          # Windows 桌面相关代码

├── macos/            # macOS 桌面相关代码

├── linux/            # Linux 桌面相关代码

├── test/             # 测试代码目录

├── pubspec.yaml      # 项目配置文件

├── pubspec.lock      # 依赖锁定文件(自动生成)

├── analysis_options.yaml  # Dart 分析器配置

└── README.md         # 项目说明文件


Main Directories and Files

lib/ directory - Core code directory

lib/ The directory is the main place where you write Dart code. All application logic should be placed here.

File/Directory Description
main.dart The entry file of the app, containing the main function and the root Widget
*.dart Other Dart source files, usually organized by feature module

android/ directory - Android Platform

Contains Android native project files, used for:

ios/ directory - iOS Platform

Contains iOS native project files (Xcode project), used for:

test/ directory - Test Code

Contains the app's test code, usually including:

pubspec.yaml - Project Configuration

pubspec.yaml is the core configuration file of a Flutter project, defining the project name, version, dependencies, etc.

Example: pubspec.yaml Sample

Project name

name

:

my_app

Project version

version

:

1.0.0+

1

Project description

description

:

"My First Flutter App"

Runtime environment

environment

:

  sdk

:

^3.0.0

Project Dependencies

dependencies

:

  flutter

:

    sdk

:

flutter

 

Add third-party dependencies

  http

:

^1.0.0

  provider

:

^6.0.0

Development dependencies (used only during development)

dev_dependencies

:

  flutter_test

:

    sdk

:

flutter

  flutter_lints

:

^3.0.0

Flutter configuration

flutter

:

  uses-material-design

:

true

 

Configure asset files

  assets

:

   - images/

    - assets/data.json

Common pubspec.yaml Fields

Field Description
name Project name, must use lowercase letters and underscores
version Project version number, in the format x.y.z
description Project description text
dependencies Packages required at runtime
dev_dependencies Packages required during development (such as testing tools)
flutter.assets Asset files to be packaged

Dependency Management

Add Dependency

Use flutter pub add command to add a dependency:



$ flutter pub add http

or edit manually pubspec.yaml then run:



$ flutter pub get

Common Dependency Commands

Command Description
flutter pub get Get all dependencies
flutter pub add package Add Dependency
flutter pub remove package Remove dependencies
flutter pub outdated Check for outdated dependencies
flutter pub upgrade Upgrade dependencies to the latest version

Recommended project structure

As the app grows, it is recommended to adopt a clear project structure:



lib/

├── main.dart              # 入口文件

├── app.dart               # 应用配置

├── core/                  # 核心功能

│   ├── constants/         # 常量定义

│   ├── theme/             # 主题配置

│   └── utils/             # 工具类

├── data/                  # 数据层

│   ├── models/            # 数据模型

│   ├── repositories/     # 仓库(数据接口)

│   └── services/          # 网络服务

├── domain/                # 领域层

│   └── entities/          # 实体类

├── presentation/          # 展示层

│   ├── pages/             # 页面

│   ├── widgets/           # 组件

│   └── providers/         # 状态管理

└── routes/                # 路由配置

A good project structure improves code maintainability; it is recommended to choose a suitable structure based on the app size.