feat:增加代办事项状态管理
This commit is contained in:
@@ -1,43 +1,80 @@
|
||||
import 'package:flisp_app/store/todo_dialog_store.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:flisp_app/models/todo.dart';
|
||||
import 'package:flisp_app/utils/date_utils.dart';
|
||||
import 'package:flisp_app/widgets/common.dart';
|
||||
|
||||
class TodoDialogContent extends StatefulWidget {
|
||||
class TodoDialog extends StatefulWidget {
|
||||
final bool isEditing;
|
||||
final TextEditingController titleController;
|
||||
final TextEditingController descriptionController;
|
||||
final DateTime? selectedDueDate;
|
||||
final TodoPriority selectedPriority;
|
||||
final String? selectedCategory;
|
||||
final Function(DateTime?) onDueDateChanged;
|
||||
final Function(TodoPriority) onPriorityChanged;
|
||||
final Function(String?) onCategoryChanged;
|
||||
final VoidCallback onClearDueDate;
|
||||
final VoidCallback onSelectDueDate;
|
||||
final TodoItem? initialTodo;
|
||||
|
||||
const TodoDialogContent({
|
||||
const TodoDialog({
|
||||
super.key,
|
||||
required this.isEditing,
|
||||
required this.titleController,
|
||||
required this.descriptionController,
|
||||
required this.selectedDueDate,
|
||||
required this.selectedPriority,
|
||||
required this.selectedCategory,
|
||||
required this.onDueDateChanged,
|
||||
required this.onPriorityChanged,
|
||||
required this.onCategoryChanged,
|
||||
required this.onClearDueDate,
|
||||
required this.onSelectDueDate,
|
||||
this.initialTodo,
|
||||
});
|
||||
|
||||
@override
|
||||
State<TodoDialogContent> createState() => _TodoDialogContentState();
|
||||
State<TodoDialog> createState() => _TodoDialogState();
|
||||
}
|
||||
|
||||
class _TodoDialogContentState extends State<TodoDialogContent> {
|
||||
class _TodoDialogState extends State<TodoDialog> {
|
||||
late TextEditingController _titleController;
|
||||
late TextEditingController _descriptionController;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
_titleController = TextEditingController();
|
||||
_descriptionController = TextEditingController();
|
||||
|
||||
// 如果是编辑模式,初始化数据
|
||||
if (widget.isEditing && widget.initialTodo != null) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
final store = Provider.of<TodoDialogStore>(context, listen: false);
|
||||
store.initEditData(widget.initialTodo!);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_titleController.dispose();
|
||||
_descriptionController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _selectDueDate(BuildContext context) async {
|
||||
final DateTime? picked = await showDatePicker(
|
||||
context: context,
|
||||
initialDate: DateTime.now(),
|
||||
firstDate: DateTime.now(),
|
||||
lastDate: DateTime(2100),
|
||||
);
|
||||
|
||||
if (picked != null) {
|
||||
final store = Provider.of<TodoDialogStore>(context, listen: false);
|
||||
store.dueDate = picked;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final store = Provider.of<TodoDialogStore>(context);
|
||||
|
||||
// 同步控制器文本
|
||||
if (_titleController.text != store.title) {
|
||||
_titleController.text = store.title;
|
||||
_titleController.selection = TextSelection.collapsed(offset: store.title.length);
|
||||
}
|
||||
|
||||
if (_descriptionController.text != store.description) {
|
||||
_descriptionController.text = store.description;
|
||||
_descriptionController.selection = TextSelection.collapsed(offset: store.description.length);
|
||||
}
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
@@ -62,16 +99,15 @@ class _TodoDialogContentState extends State<TodoDialogContent> {
|
||||
|
||||
// 标题输入框
|
||||
TextField(
|
||||
controller: widget.titleController,
|
||||
controller: _titleController,
|
||||
onChanged: (value) => store.title = value,
|
||||
decoration: InputDecoration(
|
||||
labelText: '标题',
|
||||
hintText: '请输入待办事项标题...',
|
||||
counterText: '',
|
||||
suffixText: '${widget.titleController.text.length}/20',
|
||||
suffixText: '${store.title.length}/20',
|
||||
suffixStyle: TextStyle(
|
||||
color: widget.titleController.text.length > 20
|
||||
? Colors.red
|
||||
: Colors.grey,
|
||||
color: store.title.length > 20 ? Colors.red : Colors.grey,
|
||||
),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
@@ -88,32 +124,25 @@ class _TodoDialogContentState extends State<TodoDialogContent> {
|
||||
filled: true,
|
||||
fillColor: Colors.white,
|
||||
prefixIcon: Icon(Icons.title, color: Colors.black87),
|
||||
contentPadding: EdgeInsets.symmetric(
|
||||
horizontal: 16,
|
||||
vertical: 14,
|
||||
),
|
||||
contentPadding: EdgeInsets.symmetric(horizontal: 16, vertical: 14),
|
||||
),
|
||||
style: TextStyle(fontSize: 16),
|
||||
maxLength: 20,
|
||||
onChanged: (value) {
|
||||
if (mounted) setState(() {});
|
||||
},
|
||||
),
|
||||
|
||||
SizedBox(height: 12),
|
||||
|
||||
// 描述输入框
|
||||
TextField(
|
||||
controller: widget.descriptionController,
|
||||
controller: _descriptionController,
|
||||
onChanged: (value) => store.description = value,
|
||||
decoration: InputDecoration(
|
||||
labelText: '内容',
|
||||
hintText: '请输入待办事项内容...',
|
||||
counterText: '',
|
||||
suffixText: '${widget.titleController.text.length}/50',
|
||||
suffixText: '${store.description.length}/50',
|
||||
suffixStyle: TextStyle(
|
||||
color: widget.titleController.text.length > 20
|
||||
? Colors.red
|
||||
: Colors.grey,
|
||||
color: store.description.length > 50 ? Colors.red : Colors.grey,
|
||||
),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
@@ -130,10 +159,7 @@ class _TodoDialogContentState extends State<TodoDialogContent> {
|
||||
filled: true,
|
||||
fillColor: Colors.white,
|
||||
prefixIcon: Icon(Icons.description, color: Colors.black87),
|
||||
contentPadding: EdgeInsets.symmetric(
|
||||
horizontal: 16,
|
||||
vertical: 14,
|
||||
),
|
||||
contentPadding: EdgeInsets.symmetric(horizontal: 16, vertical: 14),
|
||||
),
|
||||
style: TextStyle(fontSize: 16),
|
||||
maxLines: 2,
|
||||
@@ -148,22 +174,22 @@ class _TodoDialogContentState extends State<TodoDialogContent> {
|
||||
child: ListTile(
|
||||
leading: Icon(Icons.calendar_today, color: Colors.blue),
|
||||
title: Text(
|
||||
widget.selectedDueDate == null
|
||||
store.dueDate == null
|
||||
? '选择截止日期'
|
||||
: '截止: ${formatDate(widget.selectedDueDate!)}',
|
||||
: '截止: ${formatDate(store.dueDate!)}',
|
||||
style: TextStyle(
|
||||
color: widget.selectedDueDate == null
|
||||
? Colors.grey
|
||||
: Colors.black87,
|
||||
color: store.dueDate == null ? Colors.grey : Colors.black87,
|
||||
),
|
||||
),
|
||||
trailing: widget.selectedDueDate != null
|
||||
trailing: store.dueDate != null
|
||||
? IconButton(
|
||||
icon: Icon(Icons.clear, size: 18),
|
||||
onPressed: widget.onClearDueDate,
|
||||
onPressed: () {
|
||||
store.dueDate = null;
|
||||
},
|
||||
)
|
||||
: null,
|
||||
onTap: widget.onSelectDueDate,
|
||||
onTap: () => _selectDueDate(context),
|
||||
),
|
||||
),
|
||||
SizedBox(height: 12),
|
||||
@@ -175,12 +201,12 @@ class _TodoDialogContentState extends State<TodoDialogContent> {
|
||||
leading: Container(
|
||||
padding: EdgeInsets.all(6),
|
||||
decoration: BoxDecoration(
|
||||
color: widget.selectedPriority.color.withAlpha(10),
|
||||
color: store.priority.color.withAlpha(10),
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
child: Icon(
|
||||
Icons.flag,
|
||||
color: widget.selectedPriority.color,
|
||||
color: store.priority.color,
|
||||
size: 18,
|
||||
),
|
||||
),
|
||||
@@ -193,7 +219,7 @@ class _TodoDialogContentState extends State<TodoDialogContent> {
|
||||
),
|
||||
),
|
||||
trailing: DropdownButton<TodoPriority>(
|
||||
value: widget.selectedPriority,
|
||||
value: store.priority,
|
||||
underline: SizedBox(),
|
||||
icon: Icon(Icons.arrow_drop_down, color: Colors.grey.shade600),
|
||||
iconSize: 20,
|
||||
@@ -201,8 +227,7 @@ class _TodoDialogContentState extends State<TodoDialogContent> {
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
onChanged: (value) {
|
||||
if (value != null) {
|
||||
widget.onPriorityChanged(value);
|
||||
if (mounted) setState(() {});
|
||||
store.priority = value;
|
||||
}
|
||||
},
|
||||
items: TodoPriority.values.map((priority) {
|
||||
|
||||
Reference in New Issue
Block a user