feat(item): 添加物品删除功能并优化物品详情页面
- 在 DetailItemScreen 中添加删除物品的按钮和确认对话框 - 在 ItemProvider 中实现删除物品的方法 - 在 ItemRepository 中实现删除物品的数据库操作 - 在 ItemModel 中添加 status 字段,用于标记物品状态
This commit is contained in:
parent
4903d7d5a8
commit
083b4e506a
@ -7,6 +7,7 @@ CREATE TABLE items (
|
||||
description TEXT,
|
||||
purchase_date TEXT,
|
||||
is_in_use TEXT DEFAULT 'no',
|
||||
status TEXT DEFAULT 'normal',
|
||||
created_at TEXT DEFAULT (datetime('now')),
|
||||
updated_at TEXT DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
@ -12,7 +12,11 @@ class Item {
|
||||
final DateTime? purchaseDate;
|
||||
// 是否使用
|
||||
final String? isInUse;
|
||||
// 数据状态 -normal -deleted
|
||||
final String? status;
|
||||
// 创建时间
|
||||
final DateTime? createdAt;
|
||||
// 更新时间
|
||||
final DateTime? updatedAt;
|
||||
|
||||
Item({
|
||||
@ -23,6 +27,7 @@ class Item {
|
||||
this.description,
|
||||
this.purchaseDate,
|
||||
this.isInUse = 'no',
|
||||
this.status = 'normal',
|
||||
this.createdAt,
|
||||
this.updatedAt,
|
||||
});
|
||||
@ -35,6 +40,7 @@ class Item {
|
||||
'description': description,
|
||||
'purchase_date': purchaseDate,
|
||||
'is_in_use': isInUse,
|
||||
'status': status,
|
||||
'created_at': createdAt,
|
||||
'updated_at': updatedAt,
|
||||
};
|
||||
@ -49,6 +55,7 @@ class Item {
|
||||
description: map['description'],
|
||||
purchaseDate: map['purchase_date'],
|
||||
isInUse: map['is_in_use'],
|
||||
status: map['status'],
|
||||
createdAt: map['created_at'],
|
||||
updatedAt: map['updated_at'],
|
||||
);
|
||||
|
||||
@ -21,4 +21,14 @@ class ItemProvider extends ChangeNotifier {
|
||||
await repository.insertItem(item);
|
||||
await loadItems(); // 插入后刷新
|
||||
}
|
||||
|
||||
Future<void> updateItem(Item item) async {
|
||||
await repository.updateItem(item);
|
||||
await loadItems(); // 更新后刷新
|
||||
}
|
||||
|
||||
Future<void> deleteItem(Item item) async {
|
||||
await repository.deleteItem(item);
|
||||
await loadItems(); // 删除后刷新
|
||||
}
|
||||
}
|
||||
@ -7,13 +7,41 @@ class ItemRepository {
|
||||
|
||||
ItemRepository({required this.dbHelper});
|
||||
|
||||
/**
|
||||
* 插入一条数据
|
||||
*/
|
||||
Future<int> insertItem(Item item) async {
|
||||
final db = await dbHelper.database;
|
||||
return await db.insert('items', item.toMap());
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询所有数据
|
||||
*/
|
||||
Future<List<Map<String, dynamic>>> getAllItems() async {
|
||||
final db = await dbHelper.database;
|
||||
return await db.query('items');
|
||||
return await db.query('items', where: 'status = ?', whereArgs: ['normal']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新数据
|
||||
*/
|
||||
Future<int> updateItem(Item item) async {
|
||||
final db = await dbHelper.database;
|
||||
return await db.update(
|
||||
'items',
|
||||
item.toMap(),
|
||||
where: 'id = ?',
|
||||
whereArgs: [item.id],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除数据
|
||||
*/
|
||||
Future<int> deleteItem(Item item) async {
|
||||
final db = await dbHelper.database;
|
||||
return await db.update('items', {'status': 'delete'},
|
||||
where: 'id = ?', whereArgs: [item.id]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -207,6 +207,7 @@ class _FromTestRouteSate extends State<AddItemScreen> {
|
||||
Provider.of<ItemProvider>(context, listen: false)
|
||||
.addItem(newItem);
|
||||
|
||||
// 弹窗提示添加成功
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
||||
content: Text('提交成功'),
|
||||
duration: Duration(seconds: 1),
|
||||
|
||||
@ -1,5 +1,9 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:item_tracker/models/item_model.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../../provider/item_provider.dart';
|
||||
|
||||
class DetailItemScreen extends StatelessWidget {
|
||||
final Item item;
|
||||
@ -14,14 +18,55 @@ class DetailItemScreen extends StatelessWidget {
|
||||
),
|
||||
body: Padding(
|
||||
padding: EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
child:
|
||||
Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
|
||||
Text('名称: ${item.toMap().toString()}'),
|
||||
Text('描述: ${item.description}')
|
||||
]
|
||||
)
|
||||
)
|
||||
Text('描述: ${item.description}'),
|
||||
ElevatedButton(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Text("删除"),
|
||||
),
|
||||
onPressed: () {
|
||||
showCupertinoDialog(
|
||||
context: context,
|
||||
builder: (context) {
|
||||
return CupertinoAlertDialog(
|
||||
title: Text('提示'),
|
||||
content: Text('确定要删除吗?'),
|
||||
actions: [
|
||||
CupertinoDialogAction(
|
||||
child: Text('取消'),
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
},
|
||||
),
|
||||
CupertinoDialogAction(
|
||||
child: Text('确定'),
|
||||
onPressed: () {
|
||||
Provider.of<ItemProvider>(context,
|
||||
listen: false)
|
||||
.deleteItem(item);
|
||||
|
||||
// 弹窗提示删除成功
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
||||
content: Text('删除成功'),
|
||||
duration: Duration(seconds: 1),
|
||||
));
|
||||
Navigator.pop(context);
|
||||
Navigator.pop(context);
|
||||
})
|
||||
]);
|
||||
});
|
||||
},
|
||||
style: ElevatedButton.styleFrom(
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8.0),
|
||||
),
|
||||
elevation: 4.0,
|
||||
),
|
||||
),
|
||||
])),
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user