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,
|
description TEXT,
|
||||||
purchase_date TEXT,
|
purchase_date TEXT,
|
||||||
is_in_use TEXT DEFAULT 'no',
|
is_in_use TEXT DEFAULT 'no',
|
||||||
|
status TEXT DEFAULT 'normal',
|
||||||
created_at TEXT DEFAULT (datetime('now')),
|
created_at TEXT DEFAULT (datetime('now')),
|
||||||
updated_at TEXT DEFAULT (datetime('now'))
|
updated_at TEXT DEFAULT (datetime('now'))
|
||||||
);
|
);
|
||||||
|
|||||||
@ -12,7 +12,11 @@ class Item {
|
|||||||
final DateTime? purchaseDate;
|
final DateTime? purchaseDate;
|
||||||
// 是否使用
|
// 是否使用
|
||||||
final String? isInUse;
|
final String? isInUse;
|
||||||
|
// 数据状态 -normal -deleted
|
||||||
|
final String? status;
|
||||||
|
// 创建时间
|
||||||
final DateTime? createdAt;
|
final DateTime? createdAt;
|
||||||
|
// 更新时间
|
||||||
final DateTime? updatedAt;
|
final DateTime? updatedAt;
|
||||||
|
|
||||||
Item({
|
Item({
|
||||||
@ -23,6 +27,7 @@ class Item {
|
|||||||
this.description,
|
this.description,
|
||||||
this.purchaseDate,
|
this.purchaseDate,
|
||||||
this.isInUse = 'no',
|
this.isInUse = 'no',
|
||||||
|
this.status = 'normal',
|
||||||
this.createdAt,
|
this.createdAt,
|
||||||
this.updatedAt,
|
this.updatedAt,
|
||||||
});
|
});
|
||||||
@ -35,6 +40,7 @@ class Item {
|
|||||||
'description': description,
|
'description': description,
|
||||||
'purchase_date': purchaseDate,
|
'purchase_date': purchaseDate,
|
||||||
'is_in_use': isInUse,
|
'is_in_use': isInUse,
|
||||||
|
'status': status,
|
||||||
'created_at': createdAt,
|
'created_at': createdAt,
|
||||||
'updated_at': updatedAt,
|
'updated_at': updatedAt,
|
||||||
};
|
};
|
||||||
@ -49,6 +55,7 @@ class Item {
|
|||||||
description: map['description'],
|
description: map['description'],
|
||||||
purchaseDate: map['purchase_date'],
|
purchaseDate: map['purchase_date'],
|
||||||
isInUse: map['is_in_use'],
|
isInUse: map['is_in_use'],
|
||||||
|
status: map['status'],
|
||||||
createdAt: map['created_at'],
|
createdAt: map['created_at'],
|
||||||
updatedAt: map['updated_at'],
|
updatedAt: map['updated_at'],
|
||||||
);
|
);
|
||||||
|
|||||||
@ -21,4 +21,14 @@ class ItemProvider extends ChangeNotifier {
|
|||||||
await repository.insertItem(item);
|
await repository.insertItem(item);
|
||||||
await loadItems(); // 插入后刷新
|
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});
|
ItemRepository({required this.dbHelper});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 插入一条数据
|
||||||
|
*/
|
||||||
Future<int> insertItem(Item item) async {
|
Future<int> insertItem(Item item) async {
|
||||||
final db = await dbHelper.database;
|
final db = await dbHelper.database;
|
||||||
return await db.insert('items', item.toMap());
|
return await db.insert('items', item.toMap());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询所有数据
|
||||||
|
*/
|
||||||
Future<List<Map<String, dynamic>>> getAllItems() async {
|
Future<List<Map<String, dynamic>>> getAllItems() async {
|
||||||
final db = await dbHelper.database;
|
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)
|
Provider.of<ItemProvider>(context, listen: false)
|
||||||
.addItem(newItem);
|
.addItem(newItem);
|
||||||
|
|
||||||
|
// 弹窗提示添加成功
|
||||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
||||||
content: Text('提交成功'),
|
content: Text('提交成功'),
|
||||||
duration: Duration(seconds: 1),
|
duration: Duration(seconds: 1),
|
||||||
|
|||||||
@ -1,5 +1,9 @@
|
|||||||
|
import 'package:flutter/cupertino.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:item_tracker/models/item_model.dart';
|
import 'package:item_tracker/models/item_model.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
|
import '../../provider/item_provider.dart';
|
||||||
|
|
||||||
class DetailItemScreen extends StatelessWidget {
|
class DetailItemScreen extends StatelessWidget {
|
||||||
final Item item;
|
final Item item;
|
||||||
@ -13,15 +17,56 @@ class DetailItemScreen extends StatelessWidget {
|
|||||||
title: Text('${item.name}'),
|
title: Text('${item.name}'),
|
||||||
),
|
),
|
||||||
body: Padding(
|
body: Padding(
|
||||||
padding: EdgeInsets.all(16.0),
|
padding: EdgeInsets.all(16.0),
|
||||||
child: Column(
|
child:
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
|
||||||
children: [
|
|
||||||
Text('名称: ${item.toMap().toString()}'),
|
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