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

🚀 Ứng dụng đầu tiên

Terminal window
# Tạo project với tên my_app
flutter create my_app
# Di chuyển vào thư mục
cd my_app
  1. Mở Command Palette (Cmd+Shift+P / Ctrl+Shift+P)
  2. Flutter: New Project
  3. Chọn Application
  4. Chọn thư mục lưu
  5. Nhập tên project

Terminal window
# Liệt kê devices
flutter devices
# Chạy app
flutter run

Hoặc trong VS Code: Nhấn F5 hoặc click Run → Start Debugging


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'),
);
}
}

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!


void main() {
runApp(const MyApp()); // Entry point, khởi chạy MyApp widget
}
MaterialApp(
home: ..., // Widget hiển thị đầu tiên
)

MaterialApp là widget gốc cung cấp:

  • Material Design theme
  • Navigation
  • Localization
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( // Widget căn giữa
child: Text('Hello'), // Widget hiển thị text
)

AppBar(
title: const Text('Ứng dụng của tôi'),
backgroundColor: Colors.green, // Đổi màu
)
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),
),
],
),
),

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'),
),
),
),
);
}
}

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.


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 wrapper
  • Scaffold - Page structure
  • AppBar - Top bar
  • Center - Căn giữa
  • Text - Hiển thị text
  • ElevatedButton - Button