Bỏ qua để đến nội dung

📁 Cấu trúc dự án

Khi tạo project Flutter mới, bạn sẽ thấy cấu trúc sau:

my_app/
├── android/ # Native Android code
├── ios/ # Native iOS code
├── lib/ # Dart code (chính)
│ └── main.dart # Entry point
├── test/ # Unit tests
├── web/ # Web support
├── linux/ # Linux desktop
├── macos/ # macOS desktop
├── windows/ # Windows desktop
├── pubspec.yaml # Dependencies
├── pubspec.lock # Lock file
├── analysis_options.yaml
└── README.md

Đây là thư mục quan trọng nhất, chứa toàn bộ Dart code:

lib/
├── main.dart # Entry point
├── screens/ # Các màn hình
├── widgets/ # Custom widgets
├── models/ # Data models
├── services/ # API, database
└── utils/ # Utilities
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp()); // Entry point
}

name: my_app
description: A new Flutter project.
version: 1.0.0+1
environment:
sdk: '>=3.0.0 <4.0.0'
dependencies:
flutter:
sdk: flutter
http: ^1.1.0 # HTTP client
provider: ^6.0.0 # State management
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^3.0.0
flutter:
uses-material-design: true
assets:
- assets/images/
Terminal window
# Thêm package
flutter pub add http
# Cập nhật dependencies
flutter pub get

android/
├── app/
│ ├── build.gradle # App config
│ └── src/main/
│ ├── AndroidManifest.xml
│ ├── kotlin/ # Native Kotlin code
│ └── res/ # Resources
└── build.gradle # Project config

Khi nào sửa:

  • Thêm permissions
  • Cấu hình signing
  • Native Android integrations
ios/
├── Runner/
│ ├── Info.plist # App config
│ ├── AppDelegate.swift # Native Swift code
│ └── Assets.xcassets/ # App icons
├── Podfile # CocoaPods
└── Runner.xcworkspace # Xcode project

Khi nào sửa:

  • Thêm permissions
  • Cấu hình App Store
  • Native iOS integrations

test/
├── widget_test.dart # Widget tests
└── unit_test.dart # Unit tests

Chạy tests:

Terminal window
flutter test

Tạo thư mục assets/ cho hình ảnh, fonts, v.v:

my_app/
├── assets/
│ ├── images/
│ │ └── logo.png
│ └── fonts/
│ └── Roboto.ttf

Khai báo trong pubspec.yaml:

flutter:
assets:
- assets/images/
fonts:
- family: Roboto
fonts:
- asset: assets/fonts/Roboto.ttf

Sử dụng trong code:

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

File Mục đích
pubspec.lock Lock versions, commit vào git
analysis_options.yaml Cấu hình linter
.gitignore Files không commit
.metadata Flutter metadata

lib/
├── main.dart
├── app.dart # MaterialApp setup
├── core/
│ ├── constants/
│ ├── theme/
│ └── utils/
├── features/
│ ├── auth/
│ │ ├── screens/
│ │ ├── widgets/
│ │ └── providers/
│ └── home/
│ ├── screens/
│ ├── widgets/
│ └── providers/
├── models/
├── services/
└── shared/
└── widgets/

Thư mục/File Vai trò
lib/ Code Dart chính
lib/main.dart Entry point
pubspec.yaml Dependencies
android/, ios/ Native code
test/ Unit & widget tests
assets/ Hình ảnh, fonts