flutter-demo/lib/screens/item_list_screen.dart
LingandRX 90c1d5238b feat(database): 实现物品添加和列表展示功能
- 新增数据库相关代码,包括数据库帮助类和操作函数
- 重构添加物品页面,增加表单验证和数据提交逻辑
- 新增物品列表页面,实现数据加载和展示功能
- 更新项目配置,添加必要的依赖库
2025-04-24 21:26:06 +08:00

78 lines
2.2 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:flutter/material.dart';
import 'package:item_tracker/screens/addItem/add_item_screen.dart';
import '../database/sqlite_operation.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 = getAllItems(); // 初始化 Future
}
Future<void> _refreshData() async {
setState(() {
_itemsFuture = getAllItems(); // 更新 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,
),
),
);
}
}