- 在 DetailItemScreen 中添加删除物品的按钮和确认对话框 - 在 ItemProvider 中实现删除物品的方法 - 在 ItemRepository 中实现删除物品的数据库操作 - 在 ItemModel 中添加 status 字段,用于标记物品状态
73 lines
2.6 KiB
Dart
73 lines
2.6 KiB
Dart
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;
|
|
|
|
DetailItemScreen({required this.item});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text('${item.name}'),
|
|
),
|
|
body: Padding(
|
|
padding: EdgeInsets.all(16.0),
|
|
child:
|
|
Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
|
|
Text('名称: ${item.toMap().toString()}'),
|
|
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,
|
|
),
|
|
),
|
|
])),
|
|
);
|
|
}
|
|
}
|