flutter getx app todo with select category model
- User
flutter getx app todo with select category model
- Assistant
flutter getx app todo with select category model
Creating a Flutter ToDo app using the GetX package involves managing state and dependencies efficiently. Here's a basic outline of how you might structure a ToDo app where tasks can be categorized. Below, I'll walk you through setting up the category model and basic functionalities.
Initialize Flutter Project:
flutter create todo_app
cd todo_app
Add Dependencies in pubspec.yaml:
dependencies:
flutter:
sdk: flutter
get: ^4.3.8 # Please check for the latest version
cupertino_icons: ^1.0.3
Import GetX: In your Dart files where you want to use GetX features:
import 'package:get/get.dart';
To-Do Task Model:
class Task {
String id;
String title;
String description;
bool isDone;
String categoryId;
Task({required this.id, required this.title, this.description = '', this.isDone = false, required this.categoryId});
}
Category Model:
class Category {
String id;
String name;
Category({required this.id, required this.name});
}
Create a controller to manage the state of tasks and categories.
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'models.dart'; // Import your Task and Category models
class TodoController extends GetxController {
var tasks = <Task>[].obs;
var categories = <Category>[].obs;
@override
void onInit() {
super.onInit();
// Initialize with some sample categories
categories.addAll([
Category(id: '1', name: 'Work'),
Category(id: '2', name: 'Personal'),
]);
}
void addTask(Task task) {
tasks.add(task);
}
void updateTask(String id, {String? title, String? description, bool? isDone}) {
var index = tasks.indexWhere((task) => task.id == id);
if (index != -1) {
tasks[index] = Task(
id: tasks[index].id,
title: title ?? tasks[index].title,
description: description ?? tasks[index].description,
isDone: isDone ?? tasks[index].isDone,
categoryId: tasks[index].categoryId,
);
tasks.refresh(); // Notify listeners for changes
}
}
void toggleTaskStatus(String id) {
var task = tasks.firstWhere((task) => task.id == id);
task.isDone = !task.isDone;
tasks.refresh();
}
void addCategory(Category category) {
categories.add(category);
}
}
Create a simple UI to display and add tasks.
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'todo_controller.dart'; // Import controller
void main() {
runApp(TodoApp());
}
class TodoApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
home: TodoHome(),
);
}
}
class TodoHome extends StatelessWidget {
final TodoController controller = Get.put(TodoController());
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('ToDo App'),
),
body: Column(
children: [
Expanded(
child: Obx(() {
return ListView.builder(
itemCount: controller.tasks.length,
itemBuilder: (context, index) {
final task = controller.tasks[index];
return ListTile(
title: Text(task.title),
subtitle: Text(
controller.categories.firstWhere((c) => c.id == task.categoryId, orElse: () => Category(id: '', name: '')).name,
),
trailing: IconButton(
icon: Icon(
task.isDone ? Icons.check_box : Icons.check_box_outline_blank,
),
onPressed: () => controller.toggleTaskStatus(task.id),
),
);
},
);
}),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
onPressed: () => Get.to(AddTaskPage()),
child: Text('Add Task'),
),
),
],
),
);
}
}
class AddTaskPage extends StatelessWidget {
final TodoController controller = Get.find();
final TextEditingController titleController = TextEditingController();
final RxString selectedCategoryId = ''.obs;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Add Task'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
TextField(
controller: titleController,
decoration: InputDecoration(labelText: 'Task Title'),
),
Obx(() {
return DropdownButton<String>(
value: selectedCategoryId.value.isEmpty ? null : selectedCategoryId.value,
onChanged: (val) {
selectedCategoryId.value = val!;
},
items: controller.categories.map((category) {
return DropdownMenuItem<String>(
value: category.id,
child: Text(category.name),
);
}).toList(),
hint: Text('Select Category'),
);
}),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
if (titleController.text.isNotEmpty && selectedCategoryId.value.isNotEmpty) {
controller.addTask(Task(
id: DateTime.now().toString(),
title: titleController.text,
categoryId: selectedCategoryId.value,
));
Get.back();
} else {
Get.snackbar('Error', 'Please fill all fields');
}
},
child: Text('Add'),
),
],
),
),
);
}
}
You can enhance this app by adding:
This is a starting point to help you understand how to structure a Flutter application with GetX for state management. Enjoy building your ToDo app!
Not yet included in a published release.