- 创建 Item 模型类,包含 toMap 和 fromMap 方法 - 新增 ItemRepository 类,负责数据库操作 - 实现 ItemScopedModel,用于状态管理 - 更新 SQLiteHelper,使用新的 item 表结构- 删除旧的 SQLiteOperation 文件
84 lines
2.4 KiB
Dart
84 lines
2.4 KiB
Dart
import 'package:flutter/material.dart';
|
||
import 'package:item_tracker/screens/addItem/add_item_screen.dart';
|
||
|
||
import '../scoped_models/item_scoped_model.dart';
|
||
|
||
class ItemListScreen extends StatefulWidget {
|
||
@override
|
||
_ItemListScreenState createState() => _ItemListScreenState();
|
||
}
|
||
|
||
class _ItemListScreenState extends State<ItemListScreen> {
|
||
late Future<List<Map<String, dynamic>>> _itemsFuture;
|
||
|
||
@override
|
||
void initState() {
|
||
super.initState();
|
||
_itemsFuture = _loadItems(); // 初始化 Future
|
||
}
|
||
|
||
Future<List<Map<String, dynamic>>> _loadItems() async {
|
||
final itemScopedModel = ItemScopedModel.of(context);
|
||
return await itemScopedModel.loadItems();
|
||
}
|
||
|
||
Future<void> _refreshData() async {
|
||
setState(() {
|
||
_itemsFuture = _loadItems(); // 更新 Future,触发 FutureBuilder 重新加载数据
|
||
});
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return Scaffold(
|
||
appBar: AppBar(
|
||
title: Text('物品列表'),
|
||
),
|
||
body: RefreshIndicator(
|
||
child: FutureBuilder<List<Map<String, dynamic>>>(
|
||
future: _itemsFuture, // 绑定到可变的 Future 对象
|
||
builder: (context, snapshot) {
|
||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||
return Center(child: CircularProgressIndicator());
|
||
} else if (snapshot.hasError) {
|
||
return Center(child: Text('Error: ${snapshot.error}'));
|
||
} else {
|
||
final items = snapshot.data ?? [];
|
||
return ListView.builder(
|
||
itemCount: items.length,
|
||
itemBuilder: (context, index) {
|
||
final item = items[index];
|
||
return ListTile(
|
||
title: Text(item['name']),
|
||
subtitle: Text(item['context']),
|
||
);
|
||
},
|
||
);
|
||
}
|
||
},
|
||
),
|
||
onRefresh: _refreshData,
|
||
),
|
||
floatingActionButton: FloatingActionButton(
|
||
onPressed: () async {
|
||
final shouldRefresh = await Navigator.push(
|
||
context,
|
||
MaterialPageRoute(builder: (context) => AddItemScreen()),
|
||
);
|
||
|
||
print('shouldRefresh${shouldRefresh}');
|
||
|
||
if (shouldRefresh == true) {
|
||
print('刷新中');
|
||
_refreshData();
|
||
}
|
||
},
|
||
child: Icon(
|
||
Icons.add,
|
||
color: Colors.white,
|
||
),
|
||
),
|
||
);
|
||
}
|
||
}
|