🚀 Ứng dụng đầu tiên
1. Tạo Project mới
Phần tiêu đề “1. Tạo Project mới”Qua Terminal
Phần tiêu đề “Qua Terminal”# Tạo project với tên my_appflutter create my_app
# Di chuyển vào thư mụccd my_appQua VS Code
Phần tiêu đề “Qua VS Code”- Mở Command Palette (
Cmd+Shift+P/Ctrl+Shift+P) - Gõ
Flutter: New Project - Chọn Application
- Chọn thư mục lưu
- Nhập tên project
2. Chạy ứng dụng
Phần tiêu đề “2. Chạy ứng dụng”# Liệt kê devicesflutter devices
# Chạy appflutter runHoặc trong VS Code: Nhấn F5 hoặc click Run → Start Debugging
3. Xem code mẫu
Phần tiêu đề “3. Xem code mẫu”Mở file lib/main.dart:
import 'package:flutter/material.dart';
void main() { runApp(const MyApp());}
class MyApp extends StatelessWidget { const MyApp({super.key});
@override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), useMaterial3: true, ), home: const MyHomePage(title: 'Flutter Demo Home Page'), ); }}4. Viết Hello World đơn giản
Phần tiêu đề “4. Viết Hello World đơn giản”Thay thế nội dung lib/main.dart:
import 'package:flutter/material.dart';
void main() { runApp(const MyApp());}
class MyApp extends StatelessWidget { const MyApp({super.key});
@override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: const Text('Hello Flutter'), backgroundColor: Colors.blue, ), body: const Center( child: Text( 'Hello, World!', style: TextStyle(fontSize: 24), ), ), ), ); }}Hot Reload: Nhấn r trong terminal hoặc Ctrl+S để thấy thay đổi ngay!
5. Giải thích code
Phần tiêu đề “5. Giải thích code”main()
Phần tiêu đề “main()”void main() { runApp(const MyApp()); // Entry point, khởi chạy MyApp widget}MaterialApp
Phần tiêu đề “MaterialApp”MaterialApp( home: ..., // Widget hiển thị đầu tiên)MaterialApp là widget gốc cung cấp:
- Material Design theme
- Navigation
- Localization
Scaffold
Phần tiêu đề “Scaffold”Scaffold( appBar: AppBar(...), // Thanh tiêu đề body: ..., // Nội dung chính)Scaffold tạo cấu trúc trang cơ bản với app bar, body, floating button, v.v.
Center và Text
Phần tiêu đề “Center và Text”Center( // Widget căn giữa child: Text('Hello'), // Widget hiển thị text)6. Thử thay đổi
Phần tiêu đề “6. Thử thay đổi”Đổi màu và text
Phần tiêu đề “Đổi màu và text”AppBar( title: const Text('Ứng dụng của tôi'), backgroundColor: Colors.green, // Đổi màu)Thêm Icon
Phần tiêu đề “Thêm Icon”body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Icon( Icons.flutter_dash, size: 100, color: Colors.blue, ), const SizedBox(height: 20), const Text( 'Chào mừng đến Flutter!', style: TextStyle(fontSize: 24), ), ], ),),7. Thêm Button tương tác
Phần tiêu đề “7. Thêm Button tương tác”import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget { const MyApp({super.key});
@override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('Button Demo')), body: Center( child: ElevatedButton( onPressed: () { print('Button pressed!'); }, child: const Text('Click me'), ), ), ), ); }}8. Debug Console
Phần tiêu đề “8. Debug Console”Khi chạy app, bạn có thể thấy output trong Debug Console:
flutter: Button pressed!Sử dụng print() hoặc debugPrint() để debug.
📝 Tóm tắt
Phần tiêu đề “📝 Tóm tắt”| Command | Mô tả |
|---|---|
flutter create <name> |
Tạo project mới |
flutter run |
Chạy app |
r |
Hot reload |
R |
Hot restart |
q |
Thoát |
Widget cơ bản đã học:
MaterialApp- App wrapperScaffold- Page structureAppBar- Top barCenter- Căn giữaText- Hiển thị textElevatedButton- Button