Skip to main content

31 posts tagged with "flutter"

View All Tags

Flutter: Dự án thiết kế ứng dụng Todo List

· 8 min read

Todo List là một ứng dụng phổ biến và là dự án tuyệt vời để học Flutter. Bài viết này sẽ hướng dẫn bạn xây dựng một ứng dụng Todo List hoàn chỉnh với các tính năng cơ bản và nâng cao.

Todo List App

1. Cấu trúc dự án

1.1. Cấu trúc thư mục

lib/
├── models/
│ └── todo.dart
├── providers/
│ └── todo_provider.dart
├── screens/
│ ├── home_screen.dart
│ └── add_todo_screen.dart
├── widgets/
│ ├── todo_item.dart
│ └── todo_list.dart
└── main.dart

1.2. Cài đặt dependencies

dependencies:
flutter:
sdk: flutter
provider: ^6.0.0
sqflite: ^2.0.0
path: ^1.8.0
intl: ^0.17.0

2. Model

2.1. Todo Model

class Todo {
final int? id;
final String title;
final String description;
final DateTime dueDate;
final bool isCompleted;
final Priority priority;

Todo({
this.id,
required this.title,
required this.description,
required this.dueDate,
this.isCompleted = false,
this.priority = Priority.medium,
});

Todo copyWith({
int? id,
String? title,
String? description,
DateTime? dueDate,
bool? isCompleted,
Priority? priority,
}) {
return Todo(
id: id ?? this.id,
title: title ?? this.title,
description: description ?? this.description,
dueDate: dueDate ?? this.dueDate,
isCompleted: isCompleted ?? this.isCompleted,
priority: priority ?? this.priority,
);
}

Map<String, dynamic> toMap() {
return {
'id': id,
'title': title,
'description': description,
'dueDate': dueDate.toIso8601String(),
'isCompleted': isCompleted ? 1 : 0,
'priority': priority.index,
};
}

factory Todo.fromMap(Map<String, dynamic> map) {
return Todo(
id: map['id'],
title: map['title'],
description: map['description'],
dueDate: DateTime.parse(map['dueDate']),
isCompleted: map['isCompleted'] == 1,
priority: Priority.values[map['priority']],
);
}
}

enum Priority { low, medium, high }

3. Provider

3.1. Todo Provider

class TodoProvider extends ChangeNotifier {
List<Todo> _todos = [];
bool _isLoading = false;
String? _error;

List<Todo> get todos => _todos;
bool get isLoading => _isLoading;
String? get error => _error;

Future<void> loadTodos() async {
_isLoading = true;
_error = null;
notifyListeners();

try {
final db = await DatabaseHelper.instance.database;
final List<Map<String, dynamic>> maps = await db.query('todos');
_todos = maps.map((map) => Todo.fromMap(map)).toList();
} catch (e) {
_error = e.toString();
} finally {
_isLoading = false;
notifyListeners();
}
}

Future<void> addTodo(Todo todo) async {
try {
final db = await DatabaseHelper.instance.database;
final id = await db.insert('todos', todo.toMap());
_todos.add(todo.copyWith(id: id));
notifyListeners();
} catch (e) {
_error = e.toString();
notifyListeners();
}
}

Future<void> updateTodo(Todo todo) async {
try {
final db = await DatabaseHelper.instance.database;
await db.update(
'todos',
todo.toMap(),
where: 'id = ?',
whereArgs: [todo.id],
);
final index = _todos.indexWhere((t) => t.id == todo.id);
_todos[index] = todo;
notifyListeners();
} catch (e) {
_error = e.toString();
notifyListeners();
}
}

Future<void> deleteTodo(int id) async {
try {
final db = await DatabaseHelper.instance.database;
await db.delete(
'todos',
where: 'id = ?',
whereArgs: [id],
);
_todos.removeWhere((todo) => todo.id == id);
notifyListeners();
} catch (e) {
_error = e.toString();
notifyListeners();
}
}
}

3.2. Database Helper

class DatabaseHelper {
static final DatabaseHelper instance = DatabaseHelper._init();
static Database? _database;

DatabaseHelper._init();

Future<Database> get database async {
if (_database != null) return _database!;
_database = await _initDB('todos.db');
return _database!;
}

Future<Database> _initDB(String filePath) async {
final dbPath = await getDatabasesPath();
final path = join(dbPath, filePath);

return await openDatabase(
path,
version: 1,
onCreate: _createDB,
);
}

Future<void> _createDB(Database db, int version) async {
await db.execute('''
CREATE TABLE todos(
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
description TEXT,
dueDate TEXT NOT NULL,
isCompleted INTEGER NOT NULL,
priority INTEGER NOT NULL
)
''');
}
}

4. UI Components

4.1. Todo Item Widget

class TodoItem extends StatelessWidget {
final Todo todo;
final VoidCallback onToggle;
final VoidCallback onDelete;

const TodoItem({
Key? key,
required this.todo,
required this.onToggle,
required this.onDelete,
}) : super(key: key);

@override
Widget build(BuildContext context) {
return Card(
margin: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: ListTile(
leading: Checkbox(
value: todo.isCompleted,
onChanged: (_) => onToggle(),
),
title: Text(
todo.title,
style: TextStyle(
decoration: todo.isCompleted ? TextDecoration.lineThrough : null,
),
),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(todo.description),
SizedBox(height: 4),
Text(
'Due: ${DateFormat('MMM dd, yyyy').format(todo.dueDate)}',
style: TextStyle(
color: todo.dueDate.isBefore(DateTime.now())
? Colors.red
: Colors.grey,
),
),
],
),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
_buildPriorityIndicator(),
IconButton(
icon: Icon(Icons.delete),
onPressed: onDelete,
),
],
),
),
);
}

Widget _buildPriorityIndicator() {
Color color;
switch (todo.priority) {
case Priority.high:
color = Colors.red;
break;
case Priority.medium:
color = Colors.orange;
break;
case Priority.low:
color = Colors.green;
break;
}
return Container(
width: 12,
height: 12,
decoration: BoxDecoration(
color: color,
shape: BoxShape.circle,
),
);
}
}

4.2. Todo List Widget

class TodoList extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Consumer<TodoProvider>(
builder: (context, provider, child) {
if (provider.isLoading) {
return Center(child: CircularProgressIndicator());
}

if (provider.error != null) {
return Center(
child: Text(
'Error: ${provider.error}',
style: TextStyle(color: Colors.red),
),
);
}

if (provider.todos.isEmpty) {
return Center(
child: Text('No todos yet. Add one!'),
);
}

return ListView.builder(
itemCount: provider.todos.length,
itemBuilder: (context, index) {
final todo = provider.todos[index];
return TodoItem(
todo: todo,
onToggle: () {
provider.updateTodo(
todo.copyWith(isCompleted: !todo.isCompleted),
);
},
onDelete: () {
provider.deleteTodo(todo.id!);
},
);
},
);
},
);
}
}

5. Screens

5.1. Home Screen

class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Todo List'),
actions: [
IconButton(
icon: Icon(Icons.sort),
onPressed: () {
// Hiển thị dialog sắp xếp
},
),
],
),
body: TodoList(),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (_) => AddTodoScreen()),
);
},
child: Icon(Icons.add),
),
);
}
}

5.2. Add Todo Screen

class AddTodoScreen extends StatefulWidget {
@override
_AddTodoScreenState createState() => _AddTodoScreenState();
}

class _AddTodoScreenState extends State<AddTodoScreen> {
final _formKey = GlobalKey<FormState>();
final _titleController = TextEditingController();
final _descriptionController = TextEditingController();
DateTime _dueDate = DateTime.now();
Priority _priority = Priority.medium;

@override
void dispose() {
_titleController.dispose();
_descriptionController.dispose();
super.dispose();
}

Future<void> _selectDate() async {
final DateTime? picked = await showDatePicker(
context: context,
initialDate: _dueDate,
firstDate: DateTime.now(),
lastDate: DateTime.now().add(Duration(days: 365)),
);
if (picked != null && picked != _dueDate) {
setState(() {
_dueDate = picked;
});
}
}

void _submitForm() {
if (_formKey.currentState!.validate()) {
final todo = Todo(
title: _titleController.text,
description: _descriptionController.text,
dueDate: _dueDate,
priority: _priority,
);

context.read<TodoProvider>().addTodo(todo);
Navigator.pop(context);
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Add Todo'),
),
body: Form(
key: _formKey,
child: ListView(
padding: EdgeInsets.all(16),
children: [
TextFormField(
controller: _titleController,
decoration: InputDecoration(
labelText: 'Title',
border: OutlineInputBorder(),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter a title';
}
return null;
},
),
SizedBox(height: 16),
TextFormField(
controller: _descriptionController,
decoration: InputDecoration(
labelText: 'Description',
border: OutlineInputBorder(),
),
maxLines: 3,
),
SizedBox(height: 16),
ListTile(
title: Text('Due Date'),
subtitle: Text(
DateFormat('MMM dd, yyyy').format(_dueDate),
),
trailing: Icon(Icons.calendar_today),
onTap: _selectDate,
),
SizedBox(height: 16),
DropdownButtonFormField<Priority>(
value: _priority,
decoration: InputDecoration(
labelText: 'Priority',
border: OutlineInputBorder(),
),
items: Priority.values.map((priority) {
return DropdownMenuItem(
value: priority,
child: Text(priority.toString().split('.').last),
);
}).toList(),
onChanged: (value) {
if (value != null) {
setState(() {
_priority = value;
});
}
},
),
SizedBox(height: 24),
ElevatedButton(
onPressed: _submitForm,
child: Text('Add Todo'),
style: ElevatedButton.styleFrom(
padding: EdgeInsets.symmetric(vertical: 16),
),
),
],
),
),
);
}
}

6. Main App

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (_) => TodoProvider(),
child: MaterialApp(
title: 'Todo List',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: HomeScreen(),
),
);
}
}

7. Tính năng nâng cao

7.1. Sắp xếp và lọc

enum SortOption { dueDate, priority, title }

class TodoProvider extends ChangeNotifier {
// ... existing code ...

SortOption _sortOption = SortOption.dueDate;
bool _showCompleted = true;

List<Todo> get filteredTodos {
var todos = _todos;

if (!_showCompleted) {
todos = todos.where((todo) => !todo.isCompleted).toList();
}

switch (_sortOption) {
case SortOption.dueDate:
todos.sort((a, b) => a.dueDate.compareTo(b.dueDate));
break;
case SortOption.priority:
todos.sort((a, b) => b.priority.index.compareTo(a.priority.index));
break;
case SortOption.title:
todos.sort((a, b) => a.title.compareTo(b.title));
break;
}

return todos;
}

void setSortOption(SortOption option) {
_sortOption = option;
notifyListeners();
}

void toggleShowCompleted() {
_showCompleted = !_showCompleted;
notifyListeners();
}
}

7.2. Thông báo

class NotificationService {
static Future<void> scheduleNotification(Todo todo) async {
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();

const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('@mipmap/ic_launcher');

final InitializationSettings initializationSettings =
InitializationSettings(android: initializationSettingsAndroid);

await flutterLocalNotificationsPlugin.initialize(initializationSettings);

await flutterLocalNotificationsPlugin.zonedSchedule(
todo.id!,
'Todo Reminder',
todo.title,
TZDateTime.from(todo.dueDate, local),
NotificationDetails(
android: AndroidNotificationDetails(
'todo_reminders',
'Todo Reminders',
importance: Importance.high,
priority: Priority.high,
),
),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
);
}
}

Kết luận

Ứng dụng Todo List là một dự án tuyệt vời để học Flutter. Nó bao gồm nhiều khái niệm quan trọng như:

  • Quản lý state với Provider
  • Lưu trữ dữ liệu local với SQLite
  • Xây dựng UI phức tạp
  • Xử lý form và validation
  • Quản lý thông báo

Bạn có thể mở rộng dự án này bằng cách thêm các tính năng như:

  • Đồng bộ hóa với backend
  • Phân loại todo theo danh mục
  • Tìm kiếm và lọc nâng cao
  • Giao diện người dùng tùy chỉnh
  • Hỗ trợ đa ngôn ngữ

Tài liệu tham khảo:

Flutter: Dự án xây dựng UI cho ứng dụng

· 6 min read

Thiết kế giao diện người dùng (UI) là một phần quan trọng trong phát triển ứng dụng Flutter. Bài viết này sẽ hướng dẫn bạn cách xây dựng UI đẹp và hiệu quả cho ứng dụng Flutter.

1. Nguyên tắc thiết kế UI

1.1. Material Design

Material Design là hệ thống thiết kế của Google, cung cấp các nguyên tắc và thành phần để xây dựng giao diện người dùng đẹp và nhất quán.

MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
// Customize theme
textTheme: TextTheme(
headline1: TextStyle(fontSize: 32, fontWeight: FontWeight.bold),
bodyText1: TextStyle(fontSize: 16),
),
),
home: HomeScreen(),
)

1.2. Responsive Design

Thiết kế responsive giúp ứng dụng hoạt động tốt trên nhiều kích thước màn hình khác nhau.

class ResponsiveLayout extends StatelessWidget {
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
if (constraints.maxWidth > 600) {
return DesktopLayout();
} else {
return MobileLayout();
}
},
);
}
}

2. Custom Widgets

2.1. Custom Button

class CustomButton extends StatelessWidget {
final String text;
final VoidCallback onPressed;
final Color? color;

const CustomButton({
Key? key,
required this.text,
required this.onPressed,
this.color,
}) : super(key: key);

@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: onPressed,
style: ElevatedButton.styleFrom(
primary: color ?? Theme.of(context).primaryColor,
padding: EdgeInsets.symmetric(horizontal: 32, vertical: 16),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: Text(
text,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
);
}
}

2.2. Custom Card

class CustomCard extends StatelessWidget {
final String title;
final String subtitle;
final Widget? trailing;
final VoidCallback? onTap;

const CustomCard({
Key? key,
required this.title,
required this.subtitle,
this.trailing,
this.onTap,
}) : super(key: key);

@override
Widget build(BuildContext context) {
return Card(
elevation: 4,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
child: InkWell(
onTap: onTap,
borderRadius: BorderRadius.circular(12),
child: Padding(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: Theme.of(context).textTheme.headline6,
),
SizedBox(height: 8),
Text(
subtitle,
style: Theme.of(context).textTheme.bodyText2,
),
if (trailing != null) ...[
SizedBox(height: 16),
trailing!,
],
],
),
),
),
);
}
}

3. Layout và Navigation

3.1. Bottom Navigation

class MainScreen extends StatefulWidget {
@override
_MainScreenState createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen> {
int _selectedIndex = 0;

final List<Widget> _screens = [
HomeScreen(),
SearchScreen(),
ProfileScreen(),
];

@override
Widget build(BuildContext context) {
return Scaffold(
body: _screens[_selectedIndex],
bottomNavigationBar: BottomNavigationBar(
currentIndex: _selectedIndex,
onTap: (index) {
setState(() {
_selectedIndex = index;
});
},
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.search),
label: 'Search',
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
label: 'Profile',
),
],
),
);
}
}

3.2. Drawer Navigation

class DrawerScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Drawer Example'),
),
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: [
DrawerHeader(
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
CircleAvatar(
radius: 30,
backgroundImage: NetworkImage('https://example.com/avatar.jpg'),
),
SizedBox(height: 16),
Text(
'John Doe',
style: TextStyle(
color: Colors.white,
fontSize: 18,
),
),
Text(
'john.doe@example.com',
style: TextStyle(
color: Colors.white70,
),
),
],
),
),
ListTile(
leading: Icon(Icons.home),
title: Text('Home'),
onTap: () {
Navigator.pop(context);
// Navigate to home
},
),
ListTile(
leading: Icon(Icons.settings),
title: Text('Settings'),
onTap: () {
Navigator.pop(context);
// Navigate to settings
},
),
],
),
),
body: Center(
child: Text('Main Content'),
),
);
}
}

4. Animations

4.1. Hero Animation

class HeroAnimation extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (_) => DetailScreen()),
);
},
child: Hero(
tag: 'imageHero',
child: Image.network(
'https://example.com/image.jpg',
width: 200,
height: 200,
),
),
);
}
}

4.2. Custom Animation

class AnimatedContainer extends StatefulWidget {
@override
_AnimatedContainerState createState() => _AnimatedContainerState();
}

class _AnimatedContainerState extends State<AnimatedContainer>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _animation;

@override
void initState() {
super.initState();
_controller = AnimationController(
duration: Duration(seconds: 2),
vsync: this,
);

_animation = CurvedAnimation(
parent: _controller,
curve: Curves.easeInOut,
);

_controller.repeat(reverse: true);
}

@override
void dispose() {
_controller.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: _animation,
builder: (context, child) {
return Transform.scale(
scale: 1.0 + (_animation.value * 0.2),
child: Container(
width: 200,
height: 200,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(20),
),
),
);
},
);
}
}

5. Best Practices

5.1. Tổ chức code

lib/
├── widgets/
│ ├── custom_button.dart
│ ├── custom_card.dart
│ └── animated_container.dart
├── screens/
│ ├── home_screen.dart
│ └── detail_screen.dart
├── theme/
│ └── app_theme.dart
└── main.dart

5.2. Tối ưu hiệu suất

  • Sử dụng const constructor khi có thể
  • Tránh rebuild không cần thiết
  • Sử dụng ListView.builder thay vì ListView
  • Tối ưu hình ảnh và assets
// Good
const CustomWidget({
required this.title,
required this.onTap,
});

// Bad
CustomWidget({
required this.title,
required this.onTap,
});

6. Ví dụ thực tế

6.1. Profile Screen

class ProfileScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
slivers: [
SliverAppBar(
expandedHeight: 200,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
title: Text('Profile'),
background: Image.network(
'https://example.com/cover.jpg',
fit: BoxFit.cover,
),
),
),
SliverToBoxAdapter(
child: Padding(
padding: EdgeInsets.all(16),
child: Column(
children: [
CircleAvatar(
radius: 50,
backgroundImage: NetworkImage('https://example.com/avatar.jpg'),
),
SizedBox(height: 16),
Text(
'John Doe',
style: Theme.of(context).textTheme.headline5,
),
Text(
'Software Developer',
style: Theme.of(context).textTheme.subtitle1,
),
SizedBox(height: 24),
CustomButton(
text: 'Edit Profile',
onPressed: () {
// Navigate to edit profile
},
),
],
),
),
),
],
),
);
}
}

6.2. Settings Screen

class SettingsScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Settings'),
),
body: ListView(
children: [
ListTile(
leading: Icon(Icons.notifications),
title: Text('Notifications'),
trailing: Switch(
value: true,
onChanged: (value) {
// Handle notification settings
},
),
),
ListTile(
leading: Icon(Icons.dark_mode),
title: Text('Dark Mode'),
trailing: Switch(
value: false,
onChanged: (value) {
// Handle theme settings
},
),
),
ListTile(
leading: Icon(Icons.language),
title: Text('Language'),
trailing: Text('English'),
onTap: () {
// Show language selection
},
),
],
),
);
}
}

Kết luận

Xây dựng UI trong Flutter là một quá trình sáng tạo và thú vị. Bằng cách tuân thủ các nguyên tắc thiết kế, sử dụng các widget có sẵn và tạo custom widget, bạn có thể xây dựng giao diện người dùng đẹp và hiệu quả.

Một số điểm cần lưu ý:

  • Tuân thủ Material Design guidelines
  • Tối ưu hiệu suất
  • Tổ chức code rõ ràng
  • Sử dụng animation một cách hợp lý
  • Thiết kế responsive

Tài liệu tham khảo:

Flutter: Truyền dữ liệu giữa các widget

· 5 min read

Truyền dữ liệu giữa các widget là một khía cạnh quan trọng trong phát triển ứng dụng Flutter. Bài viết này sẽ giới thiệu các phương pháp khác nhau để truyền dữ liệu giữa các widget.

Widget Data Passing

1. Truyền dữ liệu qua Constructor

1.1. Truyền dữ liệu đơn giản

class ParentWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChildWidget(
title: 'Hello',
count: 42,
);
}
}

class ChildWidget extends StatelessWidget {
final String title;
final int count;

const ChildWidget({
Key? key,
required this.title,
required this.count,
}) : super(key: key);

@override
Widget build(BuildContext context) {
return Column(
children: [
Text(title),
Text('Count: $count'),
],
);
}
}

1.2. Truyền Callback Functions

class ParentWidget extends StatelessWidget {
void _handleButtonPress() {
print('Button pressed!');
}

@override
Widget build(BuildContext context) {
return ChildWidget(
onButtonPressed: _handleButtonPress,
);
}
}

class ChildWidget extends StatelessWidget {
final VoidCallback onButtonPressed;

const ChildWidget({
Key? key,
required this.onButtonPressed,
}) : super(key: key);

@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: onButtonPressed,
child: Text('Press Me'),
);
}
}

2. InheritedWidget

2.1. Tạo InheritedWidget

class UserData extends InheritedWidget {
final String username;
final String email;

const UserData({
Key? key,
required this.username,
required this.email,
required Widget child,
}) : super(key: key, child: child);

static UserData of(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<UserData>()!;
}

@override
bool updateShouldNotify(UserData oldWidget) {
return username != oldWidget.username || email != oldWidget.email;
}
}

2.2. Sử dụng InheritedWidget

class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return UserData(
username: 'john_doe',
email: 'john@example.com',
child: MaterialApp(
home: HomeScreen(),
),
);
}
}

class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
final userData = UserData.of(context);
return Column(
children: [
Text('Username: ${userData.username}'),
Text('Email: ${userData.email}'),
],
);
}
}

3. Provider Package

3.1. Tạo Provider

class UserProvider extends ChangeNotifier {
String _username = '';
String _email = '';

String get username => _username;
String get email => _email;

void updateUser(String username, String email) {
_username = username;
_email = email;
notifyListeners();
}
}

3.2. Sử dụng Provider

class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => UserProvider(),
child: MaterialApp(
home: HomeScreen(),
),
);
}
}

class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
final userProvider = Provider.of<UserProvider>(context);
return Column(
children: [
Text('Username: ${userProvider.username}'),
Text('Email: ${userProvider.email}'),
ElevatedButton(
onPressed: () {
userProvider.updateUser('new_user', 'new@example.com');
},
child: Text('Update User'),
),
],
);
}
}

4. Stream và StreamBuilder

4.1. Tạo Stream

class DataStream {
final _controller = StreamController<String>();

Stream<String> get stream => _controller.stream;

void addData(String data) {
_controller.add(data);
}

void dispose() {
_controller.close();
}
}

4.2. Sử dụng StreamBuilder

class StreamWidget extends StatelessWidget {
final DataStream dataStream;

const StreamWidget({
Key? key,
required this.dataStream,
}) : super(key: key);

@override
Widget build(BuildContext context) {
return StreamBuilder<String>(
stream: dataStream.stream,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text('Data: ${snapshot.data}');
}
return CircularProgressIndicator();
},
);
}
}

5. Best Practices

5.1. Chọn phương pháp phù hợp

  • Constructor: Cho dữ liệu đơn giản và tĩnh
  • InheritedWidget: Cho dữ liệu được chia sẻ rộng rãi
  • Provider: Cho state management phức tạp
  • Stream: Cho dữ liệu thay đổi theo thời gian thực

5.2. Tối ưu hóa hiệu suất

// Sử dụng const constructor khi có thể
const ChildWidget({
required this.title,
required this.count,
});

// Tránh rebuild không cần thiết
class OptimizedWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Consumer<UserProvider>(
builder: (context, userProvider, child) {
return Column(
children: [
// Widget thay đổi
Text(userProvider.username),
// Widget không thay đổi
child!,
],
);
},
child: const StaticWidget(),
);
}
}

6. Ví dụ thực tế

6.1. Ứng dụng Todo

class TodoProvider extends ChangeNotifier {
List<Todo> _todos = [];

List<Todo> get todos => _todos;

void addTodo(Todo todo) {
_todos.add(todo);
notifyListeners();
}

void removeTodo(String id) {
_todos.removeWhere((todo) => todo.id == id);
notifyListeners();
}
}

class TodoList extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Consumer<TodoProvider>(
builder: (context, todoProvider, child) {
return ListView.builder(
itemCount: todoProvider.todos.length,
itemBuilder: (context, index) {
final todo = todoProvider.todos[index];
return TodoItem(todo: todo);
},
);
},
);
}
}

6.2. Ứng dụng Chat

class ChatProvider extends ChangeNotifier {
final _messages = <Message>[];
final _streamController = StreamController<Message>();

Stream<Message> get messageStream => _streamController.stream;

void sendMessage(Message message) {
_messages.add(message);
_streamController.add(message);
notifyListeners();
}

@override
void dispose() {
_streamController.close();
super.dispose();
}
}

class ChatScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Column(
children: [
Expanded(
child: StreamBuilder<Message>(
stream: context.read<ChatProvider>().messageStream,
builder: (context, snapshot) {
if (snapshot.hasData) {
return MessageBubble(message: snapshot.data!);
}
return Container();
},
),
),
MessageInput(
onSend: (text) {
context.read<ChatProvider>().sendMessage(
Message(text: text, timestamp: DateTime.now()),
);
},
),
],
);
}
}

Kết luận

Việc chọn phương pháp truyền dữ liệu phù hợp là rất quan trọng trong phát triển ứng dụng Flutter. Mỗi phương pháp có ưu điểm và trường hợp sử dụng riêng. Hiểu rõ các phương pháp này sẽ giúp bạn tạo ra ứng dụng có hiệu suất tốt và dễ bảo trì.


Tài liệu tham khảo:

Layout Widgets trong Flutter

· 4 min read

Layout Widgets in Flutter

Layout Widgets là những widget cơ bản trong Flutter dùng để sắp xếp và bố cục các widget con. Bài viết này sẽ giới thiệu về 4 widget bố cục phổ biến nhất: Container, Row, Column và Stack.

1. Container

Container là một widget linh hoạt nhất trong Flutter, cho phép bạn tùy chỉnh nhiều thuộc tính của widget con.

Đặc điểm của Container:

  • Có thể thêm padding và margin
  • Có thể tùy chỉnh kích thước
  • Có thể thêm border và background
  • Có thể thêm shadow và gradient

Ví dụ về Container:

Container(
width: 200,
height: 200,
padding: const EdgeInsets.all(16),
margin: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(8),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.2),
blurRadius: 4,
offset: const Offset(0, 2),
),
],
),
child: const Text(
'Hello World',
style: TextStyle(color: Colors.white),
),
)

2. Row

Row là widget dùng để sắp xếp các widget con theo chiều ngang.

Đặc điểm của Row:

  • Sắp xếp các widget con theo chiều ngang
  • Có thể điều chỉnh alignment và spacing
  • Có thể sử dụng Expanded và Flexible
  • Có thể wrap các widget con

Ví dụ về Row:

Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Text('Left'),
Container(
padding: const EdgeInsets.all(8),
color: Colors.blue,
child: const Text('Center'),
),
const Text('Right'),
],
)

Các thuộc tính quan trọng của Row:

  • mainAxisAlignment: Căn chỉnh theo chiều ngang
  • crossAxisAlignment: Căn chỉnh theo chiều dọc
  • mainAxisSize: Kích thước theo chiều ngang
  • textDirection: Hướng của text
  • verticalDirection: Hướng của các widget con

3. Column

Column là widget dùng để sắp xếp các widget con theo chiều dọc.

Đặc điểm của Column:

  • Sắp xếp các widget con theo chiều dọc
  • Có thể điều chỉnh alignment và spacing
  • Có thể sử dụng Expanded và Flexible
  • Có thể scroll nếu nội dung dài

Ví dụ về Column:

Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const Text('Top'),
Container(
padding: const EdgeInsets.all(8),
color: Colors.blue,
child: const Text('Center'),
),
const Text('Bottom'),
],
)

Các thuộc tính quan trọng của Column:

  • mainAxisAlignment: Căn chỉnh theo chiều dọc
  • crossAxisAlignment: Căn chỉnh theo chiều ngang
  • mainAxisSize: Kích thước theo chiều dọc
  • textDirection: Hướng của text
  • verticalDirection: Hướng của các widget con

4. Stack

Stack là widget dùng để xếp chồng các widget con lên nhau.

Đặc điểm của Stack:

  • Xếp chồng các widget con
  • Có thể định vị chính xác các widget con
  • Có thể sử dụng Positioned để đặt vị trí
  • Phù hợp cho các layout phức tạp

Ví dụ về Stack:

Stack(
children: [
Image.network('https://example.com/image.jpg'),
Positioned(
bottom: 16,
right: 16,
child: Container(
padding: const EdgeInsets.all(8),
color: Colors.black.withOpacity(0.5),
child: const Text(
'Overlay Text',
style: TextStyle(color: Colors.white),
),
),
),
],
)

Các thuộc tính quan trọng của Stack:

  • alignment: Căn chỉnh các widget con
  • fit: Cách widget con được fit vào Stack
  • clipBehavior: Cách xử lý overflow
  • children: Danh sách các widget con

5. Kết hợp các Layout Widgets

Ví dụ về form đăng nhập:

Container(
padding: const EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'Login',
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
const SizedBox(height: 16),
TextField(
decoration: InputDecoration(
labelText: 'Username',
border: OutlineInputBorder(),
),
),
const SizedBox(height: 8),
TextField(
decoration: InputDecoration(
labelText: 'Password',
border: OutlineInputBorder(),
),
obscureText: true,
),
const SizedBox(height: 16),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
TextButton(
onPressed: () {},
child: const Text('Forgot Password?'),
),
ElevatedButton(
onPressed: () {},
child: const Text('Login'),
),
],
),
],
),
)

6. Best Practices

6.1. Sử dụng Container

  • Sử dụng Container khi cần tùy chỉnh nhiều thuộc tính
  • Tránh lồng nhiều Container không cần thiết
  • Sử dụng BoxDecoration cho các hiệu ứng phức tạp

6.2. Sử dụng Row và Column

  • Sử dụng mainAxisAlignment và crossAxisAlignment phù hợp
  • Sử dụng Expanded và Flexible khi cần
  • Tránh Row và Column quá sâu

6.3. Sử dụng Stack

  • Sử dụng Positioned để đặt vị trí chính xác
  • Cẩn thận với overflow
  • Sử dụng clipBehavior phù hợp

Kết luận

Layout Widgets là những công cụ mạnh mẽ trong Flutter để tạo giao diện người dùng. Hiểu rõ cách sử dụng Container, Row, Column và Stack sẽ giúp bạn tạo ra các layout phức tạp một cách dễ dàng và hiệu quả.


Tài liệu tham khảo:

Flutter: ListView và GridView widgets

· 4 min read

ListView và GridView là hai widget quan trọng trong Flutter để hiển thị dữ liệu dạng danh sách và lưới. Bài viết này sẽ hướng dẫn bạn cách sử dụng chúng một cách hiệu quả.

1. ListView Widget

ListView là widget được sử dụng để hiển thị danh sách các phần tử theo chiều dọc hoặc ngang.

ListView Widget Examples

1.1. Basic ListView

ListView(
children: [
ListTile(
leading: Icon(Icons.star),
title: Text('Item 1'),
subtitle: Text('Description 1'),
),
ListTile(
leading: Icon(Icons.star),
title: Text('Item 2'),
subtitle: Text('Description 2'),
),
ListTile(
leading: Icon(Icons.star),
title: Text('Item 3'),
subtitle: Text('Description 3'),
),
],
)

1.2. ListView.builder

ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return ListTile(
leading: Icon(Icons.star),
title: Text(items[index].title),
subtitle: Text(items[index].description),
);
},
)

1.3. ListView.separated

ListView.separated(
itemCount: items.length,
separatorBuilder: (context, index) => Divider(),
itemBuilder: (context, index) {
return ListTile(
leading: Icon(Icons.star),
title: Text(items[index].title),
subtitle: Text(items[index].description),
);
},
)

1.4. Horizontal ListView

ListView(
scrollDirection: Axis.horizontal,
children: [
Container(
width: 160,
margin: EdgeInsets.all(8),
color: Colors.blue,
child: Center(child: Text('Item 1')),
),
Container(
width: 160,
margin: EdgeInsets.all(8),
color: Colors.green,
child: Center(child: Text('Item 2')),
),
Container(
width: 160,
margin: EdgeInsets.all(8),
color: Colors.red,
child: Center(child: Text('Item 3')),
),
],
)

2. GridView Widget

GridView là widget được sử dụng để hiển thị dữ liệu dạng lưới với các cột và hàng.

GridView Widget Examples

2.1. GridView.count

GridView.count(
crossAxisCount: 2,
children: [
Container(
margin: EdgeInsets.all(8),
color: Colors.blue,
child: Center(child: Text('Item 1')),
),
Container(
margin: EdgeInsets.all(8),
color: Colors.green,
child: Center(child: Text('Item 2')),
),
Container(
margin: EdgeInsets.all(8),
color: Colors.red,
child: Center(child: Text('Item 3')),
),
Container(
margin: EdgeInsets.all(8),
color: Colors.yellow,
child: Center(child: Text('Item 4')),
),
],
)

2.2. GridView.builder

GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
crossAxisSpacing: 10,
mainAxisSpacing: 10,
),
itemCount: items.length,
itemBuilder: (context, index) {
return Container(
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(8),
),
child: Center(
child: Text(items[index].title),
),
);
},
)

2.3. GridView.extent

GridView.extent(
maxCrossAxisExtent: 200,
children: [
Container(
margin: EdgeInsets.all(8),
color: Colors.blue,
child: Center(child: Text('Item 1')),
),
Container(
margin: EdgeInsets.all(8),
color: Colors.green,
child: Center(child: Text('Item 2')),
),
Container(
margin: EdgeInsets.all(8),
color: Colors.red,
child: Center(child: Text('Item 3')),
),
Container(
margin: EdgeInsets.all(8),
color: Colors.yellow,
child: Center(child: Text('Item 4')),
),
],
)

3. Best Practices

3.1. ListView

  • Sử dụng ListView.builder cho danh sách dài
  • Thêm padding và spacing phù hợp
  • Xử lý scroll physics khi cần
  • Sử dụng const constructor khi có thể

3.2. GridView

  • Chọn số cột phù hợp với kích thước màn hình
  • Thêm spacing giữa các item
  • Sử dụng GridView.builder cho danh sách dài
  • Tối ưu hóa kích thước item

3.3. Performance

  • Sử dụng const constructor
  • Tránh rebuild không cần thiết
  • Sử dụng ListView.builder và GridView.builder
  • Tối ưu hóa widget tree

4. Ví dụ thực tế

4.1. Danh sách sản phẩm

ListView.builder(
itemCount: products.length,
itemBuilder: (context, index) {
final product = products[index];
return Card(
child: ListTile(
leading: Image.network(product.imageUrl),
title: Text(product.name),
subtitle: Text(product.price.toString()),
trailing: IconButton(
icon: Icon(Icons.shopping_cart),
onPressed: () {
// Add to cart
},
),
),
);
},
)

4.2. Lưới hình ảnh

GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
crossAxisSpacing: 4,
mainAxisSpacing: 4,
),
itemCount: images.length,
itemBuilder: (context, index) {
return Image.network(
images[index],
fit: BoxFit.cover,
);
},
)

Kết luận

ListView và GridView là những widget mạnh mẽ trong Flutter để hiển thị dữ liệu dạng danh sách và lưới. Việc hiểu rõ cách sử dụng chúng sẽ giúp bạn tạo ra giao diện người dùng hiệu quả và đẹp mắt.


Tài liệu tham khảo:

Material Design và Cupertino Widgets trong Flutter

· 4 min read

Flutter cung cấp hai bộ widget chính để tạo giao diện người dùng: Material Design cho Android và Cupertino cho iOS. Bài viết này sẽ giúp bạn hiểu rõ về hai phong cách thiết kế này và cách sử dụng chúng.

1. Material Design

Material Design là ngôn ngữ thiết kế của Google, được sử dụng chủ yếu cho các ứng dụng Android.

1.1. Các widget Material cơ bản

AppBar

AppBar(
title: const Text('Material App'),
actions: [
IconButton(
icon: const Icon(Icons.search),
onPressed: () {},
),
IconButton(
icon: const Icon(Icons.more_vert),
onPressed: () {},
),
],
)

FloatingActionButton

FloatingActionButton(
onPressed: () {},
child: const Icon(Icons.add),
)

Card

Card(
child: ListTile(
leading: const Icon(Icons.person),
title: const Text('John Doe'),
subtitle: const Text('john.doe@example.com'),
trailing: IconButton(
icon: const Icon(Icons.edit),
onPressed: () {},
),
),
)

BottomNavigationBar

BottomNavigationBar(
items: const [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.business),
label: 'Business',
),
BottomNavigationBarItem(
icon: Icon(Icons.school),
label: 'School',
),
],
)

1.2. Material Design Themes

MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
brightness: Brightness.light,
textTheme: const TextTheme(
headline1: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
bodyText1: TextStyle(fontSize: 16),
),
),
home: const MyHomePage(),
)

2. Cupertino Widgets

Cupertino widgets là bộ widget theo phong cách iOS, cung cấp giao diện native cho người dùng iOS.

2.1. Các widget Cupertino cơ bản

CupertinoNavigationBar

CupertinoNavigationBar(
middle: const Text('Cupertino App'),
trailing: CupertinoButton(
child: const Icon(CupertinoIcons.add),
onPressed: () {},
),
)

CupertinoButton

CupertinoButton(
color: CupertinoColors.activeBlue,
child: const Text('Cupertino Button'),
onPressed: () {},
)

CupertinoListTile

CupertinoListTile(
leading: const Icon(CupertinoIcons.person),
title: const Text('John Doe'),
subtitle: const Text('john.doe@example.com'),
trailing: const Icon(CupertinoIcons.chevron_right),
)

CupertinoTabBar

CupertinoTabBar(
items: const [
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.settings),
label: 'Settings',
),
],
)

2.2. Cupertino Themes

CupertinoApp(
theme: const CupertinoThemeData(
primaryColor: CupertinoColors.systemBlue,
brightness: Brightness.light,
textTheme: CupertinoTextThemeData(
primaryColor: CupertinoColors.systemBlue,
),
),
home: const MyHomePage(),
)

3. So sánh Material và Cupertino

3.1. Điểm khác biệt chính

Đặc điểmMaterial DesignCupertino
Phong cáchGoogle MaterialApple iOS
Màu sắcĐa dạng, tươi sángTối giản, trung tính
AnimationPhức tạp, mượt màĐơn giản, nhanh
TypographyRobotoSan Francisco
IconographyMaterial IconsSF Symbols

3.2. Khi nào sử dụng cái nào?

Sử dụng Material Design khi:

  • Ứng dụng chủ yếu cho Android
  • Cần giao diện phong phú, nhiều animation
  • Muốn tùy chỉnh nhiều về giao diện
  • Cần các widget phức tạp

Sử dụng Cupertino khi:

  • Ứng dụng chủ yếu cho iOS
  • Cần giao diện đơn giản, tối giản
  • Muốn trải nghiệm native iOS
  • Cần hiệu năng tốt

4. Kết hợp Material và Cupertino

4.1. Platform-aware widgets

Widget build(BuildContext context) {
return Platform.isIOS
? CupertinoButton(
child: const Text('iOS Button'),
onPressed: () {},
)
: ElevatedButton(
onPressed: () {},
child: const Text('Android Button'),
);
}

4.2. Adaptive widgets

Scaffold(
appBar: AppBar(
title: const Text('Adaptive App'),
actions: [
IconButton(
icon: Icon(
Platform.isIOS
? CupertinoIcons.add
: Icons.add,
),
onPressed: () {},
),
],
),
body: Center(
child: Platform.isIOS
? const CupertinoActivityIndicator()
: const CircularProgressIndicator(),
),
)

5. Best Practices

5.1. Thiết kế nhất quán

  • Chọn một phong cách chính cho ứng dụng
  • Sử dụng các widget phù hợp với platform
  • Duy trì tính nhất quán trong toàn bộ ứng dụng

5.2. Performance

  • Sử dụng const constructor khi có thể
  • Tránh rebuild không cần thiết
  • Tối ưu hóa animation

5.3. Accessibility

  • Sử dụng semantic labels
  • Đảm bảo contrast ratio phù hợp
  • Hỗ trợ screen readers

Kết luận

Material Design và Cupertino widgets là hai công cụ mạnh mẽ trong Flutter để tạo giao diện người dùng. Việc hiểu rõ sự khác biệt và biết khi nào sử dụng cái nào sẽ giúp bạn tạo ra ứng dụng có trải nghiệm người dùng tốt nhất trên cả Android và iOS.


Tài liệu tham khảo:

Stateless và Stateful Widgets trong Flutter

· 4 min read

Stateless vs Stateful Widgets

Trong Flutter, có hai loại widget cơ bản: StatelessWidget và StatefulWidget. Việc hiểu rõ sự khác biệt giữa hai loại widget này là rất quan trọng để xây dựng ứng dụng Flutter hiệu quả.

1. StatelessWidget

StatelessWidget là widget không có state (trạng thái). Nó là immutable (không thể thay đổi) sau khi được tạo.

Đặc điểm của StatelessWidget:

  • Không có state
  • Không thể thay đổi sau khi được tạo
  • Phù hợp cho UI tĩnh
  • Hiệu năng tốt hơn vì không cần rebuild

Ví dụ về StatelessWidget:

class GreetingWidget extends StatelessWidget {
final String name;

const GreetingWidget({
super.key,
required this.name
});

@override
Widget build(BuildContext context) {
return Text('Hello, $name!');
}
}

Khi nào sử dụng StatelessWidget:

  • Hiển thị thông tin tĩnh
  • Widget chỉ phụ thuộc vào các tham số đầu vào
  • Không cần thay đổi UI theo thời gian
  • Không cần lưu trữ dữ liệu

2. StatefulWidget

StatefulWidget là widget có state (trạng thái). Nó có thể thay đổi trong quá trình sử dụng.

Đặc điểm của StatefulWidget:

  • Có state
  • Có thể thay đổi sau khi được tạo
  • Phù hợp cho UI động
  • Cần rebuild khi state thay đổi

Ví dụ về StatefulWidget:

class CounterWidget extends StatefulWidget {
const CounterWidget({super.key});

@override
State<CounterWidget> createState() => _CounterWidgetState();
}

class _CounterWidgetState extends State<CounterWidget> {
int _count = 0;

void _increment() {
setState(() {
_count++;
});
}

@override
Widget build(BuildContext context) {
return Column(
children: [
Text('Count: $_count'),
ElevatedButton(
onPressed: _increment,
child: const Text('Increment'),
),
],
);
}
}

Khi nào sử dụng StatefulWidget:

  • UI cần thay đổi theo thời gian
  • Cần lưu trữ dữ liệu
  • Có tương tác người dùng
  • Cần thực hiện các tác vụ bất đồng bộ

3. So sánh StatelessWidget và StatefulWidget

Đặc điểmStatelessWidgetStatefulWidget
StateKhông có
ImmutableKhông
Hiệu năngTốt hơnKém hơn
Sử dụngUI tĩnhUI động
CodeĐơn giảnPhức tạp hơn

4. Best Practices

4.1. Sử dụng StatelessWidget khi có thể

  • Ưu tiên sử dụng StatelessWidget nếu không cần state
  • Tách các phần UI tĩnh thành StatelessWidget riêng

4.2. Quản lý State hiệu quả

  • Chỉ lưu trữ state cần thiết
  • Sử dụng setState một cách hợp lý
  • Tránh rebuild không cần thiết

4.3. Tổ chức code

  • Tách biệt logic và UI
  • Sử dụng các widget có thể tái sử dụng
  • Đặt tên rõ ràng cho các widget

5. Ví dụ thực tế

5.1. StatelessWidget - ProductCard

class ProductCard extends StatelessWidget {
final String name;
final double price;
final String imageUrl;

const ProductCard({
super.key,
required this.name,
required this.price,
required this.imageUrl,
});

@override
Widget build(BuildContext context) {
return Card(
child: Column(
children: [
Image.network(imageUrl),
Text(name),
Text('\$$price'),
],
),
);
}
}

5.2. StatefulWidget - ShoppingCart

class ShoppingCart extends StatefulWidget {
const ShoppingCart({super.key});

@override
State<ShoppingCart> createState() => _ShoppingCartState();
}

class _ShoppingCartState extends State<ShoppingCart> {
final List<CartItem> _items = [];

void _addItem(CartItem item) {
setState(() {
_items.add(item);
});
}

void _removeItem(int index) {
setState(() {
_items.removeAt(index);
});
}

@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: _items.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(_items[index].name),
trailing: IconButton(
icon: const Icon(Icons.delete),
onPressed: () => _removeItem(index),
),
);
},
);
}
}

Kết luận

Việc lựa chọn giữa StatelessWidget và StatefulWidget phụ thuộc vào yêu cầu của ứng dụng. StatelessWidget đơn giản và hiệu quả cho UI tĩnh, trong khi StatefulWidget linh hoạt hơn cho UI động. Hiểu rõ sự khác biệt giữa hai loại widget này sẽ giúp bạn xây dựng ứng dụng Flutter tốt hơn.


Tài liệu tham khảo:

Flutter: Làm việc với Text, Image và Icon widgets

· 4 min read

Text, Image và Icon là những widget cơ bản và quan trọng trong Flutter. Bài viết này sẽ hướng dẫn bạn cách sử dụng chúng một cách hiệu quả.

1. Text Widget

Text widget được sử dụng để hiển thị văn bản trong ứng dụng Flutter.

Text Widget Examples

1.1. Cú pháp cơ bản

Text(
'Hello, Flutter!',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: Colors.blue,
),
)

1.2. Các thuộc tính phổ biến

Style

TextStyle(
fontSize: 20, // Kích thước chữ
fontWeight: FontWeight.bold, // Độ đậm
fontStyle: FontStyle.italic, // Kiểu chữ
color: Colors.red, // Màu chữ
letterSpacing: 2.0, // Khoảng cách giữa các chữ
wordSpacing: 5.0, // Khoảng cách giữa các từ
height: 1.5, // Chiều cao dòng
decoration: TextDecoration.underline, // Gạch chân
)

TextAlign

Text(
'Căn giữa văn bản',
textAlign: TextAlign.center,
)

MaxLines và Overflow

Text(
'Văn bản dài...',
maxLines: 2,
overflow: TextOverflow.ellipsis,
)

1.3. Rich Text

RichText(
text: TextSpan(
text: 'Hello ',
style: TextStyle(color: Colors.black),
children: <TextSpan>[
TextSpan(
text: 'Flutter',
style: TextStyle(
color: Colors.blue,
fontWeight: FontWeight.bold,
),
),
TextSpan(text: '!'),
],
),
)

2. Image Widget

Image widget được sử dụng để hiển thị hình ảnh trong ứng dụng Flutter.

Image Widget Examples

2.1. Các loại Image

Network Image

Image.network(
'https://example.com/image.jpg',
loadingBuilder: (context, child, loadingProgress) {
if (loadingProgress == null) return child;
return CircularProgressIndicator(
value: loadingProgress.expectedTotalBytes != null
? loadingProgress.cumulativeBytesLoaded /
loadingProgress.expectedTotalBytes!
: null,
);
},
)

Asset Image

Image.asset(
'assets/images/flutter_logo.png',
width: 200,
height: 200,
)

File Image

Image.file(
File('/path/to/image.jpg'),
fit: BoxFit.cover,
)

2.2. Các thuộc tính phổ biến

Fit

Image.network(
'https://example.com/image.jpg',
fit: BoxFit.cover, // Các giá trị: contain, cover, fill, fitWidth, fitHeight
)

Width và Height

Image.network(
'https://example.com/image.jpg',
width: 200,
height: 200,
)

Error Handling

Image.network(
'https://example.com/image.jpg',
errorBuilder: (context, error, stackTrace) {
return const Icon(Icons.error);
},
)

3. Icon Widget

Icon widget được sử dụng để hiển thị các biểu tượng trong ứng dụng Flutter.

Icon Widget Examples

3.1. Material Icons

Icon(
Icons.favorite,
color: Colors.red,
size: 30,
)

3.2. Cupertino Icons

Icon(
CupertinoIcons.heart_fill,
color: CupertinoColors.systemRed,
size: 30,
)

3.3. Custom Icons

Icon(
IconData(0xe800, fontFamily: 'CustomIcons'),
color: Colors.blue,
size: 30,
)

4. Kết hợp các Widget

4.1. Row với Icon và Text

Row(
children: [
Icon(Icons.star, color: Colors.amber),
const SizedBox(width: 8),
Text('Favorite'),
],
)

4.2. Column với Image và Text

Column(
children: [
Image.network(
'https://example.com/image.jpg',
width: 200,
height: 200,
),
const SizedBox(height: 16),
Text(
'Image Title',
style: TextStyle(fontSize: 20),
),
],
)

5. Best Practices

5.1. Text

  • Sử dụng const constructor khi có thể
  • Tách TextStyle thành các theme riêng
  • Xử lý overflow một cách phù hợp
  • Sử dụng RichText cho văn bản phức tạp

5.2. Image

  • Luôn chỉ định kích thước cho Image
  • Sử dụng loadingBuilder và errorBuilder
  • Tối ưu hóa kích thước hình ảnh
  • Sử dụng cache cho network images

5.3. Icon

  • Sử dụng Material Icons cho Android
  • Sử dụng Cupertino Icons cho iOS
  • Tạo custom icons khi cần thiết
  • Đảm bảo kích thước icon phù hợp

Kết luận

Text, Image và Icon là những widget cơ bản nhưng rất quan trọng trong Flutter. Việc hiểu rõ cách sử dụng chúng sẽ giúp bạn tạo ra giao diện người dùng đẹp và hiệu quả.


Tài liệu tham khảo:

Widget Tree và các loại Widget trong Flutter

· 4 min read

Flutter Widget Tree

Widget là thành phần cơ bản trong Flutter, mọi thứ bạn nhìn thấy trên màn hình đều là widget. Bài viết này sẽ giúp bạn hiểu rõ về cấu trúc Widget Tree và các loại widget phổ biến trong Flutter.

1. Widget Tree là gì?

Widget Tree là cấu trúc phân cấp của các widget trong ứng dụng Flutter. Mỗi widget có thể chứa các widget con, tạo thành một cây các widget.

MaterialApp
└── Scaffold
├── AppBar
│ └── Text
└── Column
├── Text
├── SizedBox
└── ElevatedButton
└── Text

Đặc điểm của Widget Tree:

  • Cấu trúc phân cấp
  • Widget cha chứa widget con
  • Mỗi widget có thể có nhiều widget con
  • Widget con kế thừa các thuộc tính từ widget cha

2. Các loại Widget cơ bản

2.1. StatelessWidget vs StatefulWidget

StatelessWidget

  • Widget không có state (trạng thái)
  • Không thể thay đổi sau khi được tạo
  • Phù hợp cho UI tĩnh
class GreetingWidget extends StatelessWidget {
final String name;

const GreetingWidget({super.key, required this.name});

@override
Widget build(BuildContext context) {
return Text('Hello, $name!');
}
}

StatefulWidget

  • Widget có state (trạng thái)
  • Có thể thay đổi trong quá trình sử dụng
  • Phù hợp cho UI động
class CounterWidget extends StatefulWidget {
const CounterWidget({super.key});

@override
State<CounterWidget> createState() => _CounterWidgetState();
}

class _CounterWidgetState extends State<CounterWidget> {
int _count = 0;

void _increment() {
setState(() {
_count++;
});
}

@override
Widget build(BuildContext context) {
return Column(
children: [
Text('Count: $_count'),
ElevatedButton(
onPressed: _increment,
child: const Text('Increment'),
),
],
);
}
}

2.2. Các Widget phổ biến

Layout Widgets

  1. Container

    • Widget linh hoạt nhất
    • Có thể định dạng padding, margin, border, background
    Container(
    padding: const EdgeInsets.all(16),
    margin: const EdgeInsets.all(8),
    decoration: BoxDecoration(
    color: Colors.blue,
    borderRadius: BorderRadius.circular(8),
    ),
    child: const Text('Hello'),
    )
  2. Row và Column

    • Sắp xếp các widget theo chiều ngang/dọc
    Row(
    mainAxisAlignment: MainAxisAlignment.spaceBetween,
    children: [
    Text('Left'),
    Text('Center'),
    Text('Right'),
    ],
    )
  3. Stack

    • Xếp chồng các widget lên nhau
    Stack(
    children: [
    Image.network('url'),
    Positioned(
    bottom: 16,
    right: 16,
    child: Text('Overlay'),
    ),
    ],
    )

Input Widgets

  1. TextField

    • Nhập liệu văn bản
    TextField(
    decoration: InputDecoration(
    labelText: 'Username',
    hintText: 'Enter your username',
    ),
    )
  2. ElevatedButton

    • Nút bấm có hiệu ứng nổi
    ElevatedButton(
    onPressed: () {
    // Handle press
    },
    child: const Text('Click me'),
    )

Display Widgets

  1. Text

    • Hiển thị văn bản
    Text(
    'Hello World',
    style: TextStyle(
    fontSize: 24,
    fontWeight: FontWeight.bold,
    ),
    )
  2. Image

    • Hiển thị hình ảnh
    Image.network(
    'https://example.com/image.jpg',
    width: 200,
    height: 200,
    )

3. Quản lý State trong Widget Tree

3.1. Local State

  • Sử dụng setState trong StatefulWidget
  • Phù hợp cho state đơn giản, chỉ ảnh hưởng đến widget hiện tại

3.2. Global State

  • Sử dụng state management (Provider, Bloc, GetX)
  • Phù hợp cho state phức tạp, được chia sẻ giữa nhiều widget

4. Best Practices

  1. Tổ chức Widget Tree

    • Tách các widget phức tạp thành các widget nhỏ hơn
    • Sử dụng các widget có thể tái sử dụng
    • Tránh widget tree quá sâu
  2. Performance

    • Sử dụng const constructor khi có thể
    • Tránh rebuild không cần thiết
    • Sử dụng ListView.builder cho danh sách dài
  3. State Management

    • Chọn giải pháp state management phù hợp
    • Tránh prop drilling
    • Tách biệt logic và UI

Kết luận

Hiểu rõ về Widget Tree và các loại widget là nền tảng quan trọng trong phát triển Flutter. Với kiến thức này, bạn có thể xây dựng UI phức tạp và quản lý state hiệu quả.


Tài liệu tham khảo:

Cơ bản về ngôn ngữ Dart - Biến, kiểu dữ liệu và hàm

· 4 min read

Dart là ngôn ngữ lập trình được phát triển bởi Google và là ngôn ngữ chính để phát triển ứng dụng Flutter. Bài viết này sẽ giúp bạn hiểu rõ về các khái niệm cơ bản trong Dart: biến, kiểu dữ liệu và hàm.

1. Biến trong Dart

Khai báo biến

Trong Dart, bạn có thể khai báo biến theo hai cách:

// Cách 1: Sử dụng var
var name = 'John';
var age = 25;

// Cách 2: Chỉ định kiểu dữ liệu
String name = 'John';
int age = 25;

Các từ khóa khai báo biến

  • var: Tự động suy luận kiểu dữ liệu
  • final: Biến chỉ được gán giá trị một lần
  • const: Biến hằng số, giá trị không thể thay đổi
  • late: Khai báo biến sẽ được khởi tạo sau
final String name = 'John';  // Không thể thay đổi giá trị
const double pi = 3.14; // Hằng số
late String description; // Sẽ được khởi tạo sau

2. Kiểu dữ liệu trong Dart

Kiểu dữ liệu cơ bản

  1. Numbers

    int age = 25;           // Số nguyên
    double height = 1.75; // Số thực
  2. String

    String name = 'John';
    String message = "Hello, $name!"; // String interpolation
  3. Boolean

    bool isActive = true;
    bool isCompleted = false;
  4. List (Mảng)

    List<String> fruits = ['apple', 'banana', 'orange'];
    var numbers = [1, 2, 3, 4, 5];
  5. Map (Dictionary)

    Map<String, dynamic> person = {
    'name': 'John',
    'age': 25,
    'isActive': true
    };
  6. Set (Tập hợp)

    Set<String> countries = {'Vietnam', 'USA', 'Japan'};

Kiểm tra kiểu dữ liệu

var value = 42;
print(value is int); // true
print(value is String); // false

3. Hàm trong Dart

Định nghĩa hàm cơ bản

// Hàm không có tham số và không trả về giá trị
void sayHello() {
print('Hello!');
}

// Hàm có tham số và trả về giá trị
int add(int a, int b) {
return a + b;
}

// Hàm với tham số tùy chọn
void greet(String name, [String? title]) {
if (title != null) {
print('Hello, $title $name!');
} else {
print('Hello, $name!');
}
}

Arrow Function

// Cú pháp ngắn gọn cho hàm chỉ có một câu lệnh
int multiply(int a, int b) => a * b;

Tham số đặt tên

void printUserInfo({required String name, int? age}) {
print('Name: $name');
if (age != null) {
print('Age: $age');
}
}

// Sử dụng
printUserInfo(name: 'John', age: 25);

Hàm bất đồng bộ

// Hàm bất đồng bộ với async/await
Future<String> fetchData() async {
// Giả lập việc tải dữ liệu
await Future.delayed(Duration(seconds: 2));
return 'Data loaded!';
}

// Sử dụng
void main() async {
String result = await fetchData();
print(result);
}

4. Ví dụ thực tế

class User {
final String name;
final int age;
final List<String> hobbies;

User({
required this.name,
required this.age,
required this.hobbies,
});

void introduce() {
print('Hi, I am $name, $age years old.');
print('My hobbies are: ${hobbies.join(", ")}');
}
}

void main() {
var user = User(
name: 'John',
age: 25,
hobbies: ['reading', 'coding', 'gaming'],
);

user.introduce();
}

Kết luận

Dart là một ngôn ngữ lập trình mạnh mẽ với cú pháp rõ ràng và dễ học. Hiểu rõ về biến, kiểu dữ liệu và hàm là nền tảng quan trọng để bắt đầu phát triển ứng dụng Flutter. Hãy thực hành nhiều với các ví dụ trên để nắm vững các khái niệm cơ bản này.


Tài liệu tham khảo: