Commit 56619d0f authored by 张宏's avatar 张宏

3

parent 7c60f91c
...@@ -4,6 +4,8 @@ import 'inspection_history_state.dart'; ...@@ -4,6 +4,8 @@ import 'inspection_history_state.dart';
import '../../../../services/inspection_history_service.dart'; import '../../../../services/inspection_history_service.dart';
class InspectionHistoryCubit extends Cubit<InspectionHistoryState> { class InspectionHistoryCubit extends Cubit<InspectionHistoryState> {
static const int pageSize = 10;
final InspectionHistoryService _service; final InspectionHistoryService _service;
final int _deviceIdInt; final int _deviceIdInt;
...@@ -12,27 +14,27 @@ class InspectionHistoryCubit extends Cubit<InspectionHistoryState> { ...@@ -12,27 +14,27 @@ class InspectionHistoryCubit extends Cubit<InspectionHistoryState> {
required InspectionHistoryService service, required InspectionHistoryService service,
}) : _service = service, }) : _service = service,
_deviceIdInt = int.tryParse(deviceId) ?? 0, _deviceIdInt = int.tryParse(deviceId) ?? 0,
super(InspectionHistoryState( super(InspectionHistoryState(deviceId: deviceId));
deviceId: deviceId,
totalCount: 0,
passCount: 0,
warningCount: 0,
failCount: 0,
records: [],
)) {
loadHistoryData();
}
Future<void> loadHistoryData() async { /// 供 PagingController.fetchPage 回调使用,每次加载新的一页
emit(state.copyWith(isLoading: true, error: null)); Future<List<InspectionRecord>> fetchPage(int pageKey) async {
try {
final result = await _service.getList( final result = await _service.getList(
pageSize: 10, pageSize: pageSize,
pageNum: 1, pageNum: pageKey,
deviceId: _deviceIdInt, deviceId: _deviceIdInt,
); );
final records = result.rows.map((row) { // 首页时更新统计数
if (pageKey == 1) {
emit(state.copyWith(
totalCount: result.tabCount.total,
passCount: result.tabCount.passCount,
warningCount: result.tabCount.warnCount,
failCount: result.tabCount.failCount,
));
}
return result.rows.map((row) {
return InspectionRecord( return InspectionRecord(
deviceName: result.deviceInfo.deviceName, deviceName: result.deviceInfo.deviceName,
inspectorName: row.inspectorName, inspectorName: row.inspectorName,
...@@ -48,17 +50,5 @@ class InspectionHistoryCubit extends Cubit<InspectionHistoryState> { ...@@ -48,17 +50,5 @@ class InspectionHistoryCubit extends Cubit<InspectionHistoryState> {
icon: Icons.devices, icon: Icons.devices,
); );
}).toList(); }).toList();
emit(state.copyWith(
isLoading: false,
totalCount: result.tabCount.total,
passCount: result.tabCount.passCount,
warningCount: result.tabCount.warnCount,
failCount: result.tabCount.failCount,
records: records,
));
} catch (e) {
emit(state.copyWith(isLoading: false, error: e.toString()));
}
} }
} }
\ No newline at end of file
...@@ -7,19 +7,13 @@ class InspectionHistoryState extends Equatable { ...@@ -7,19 +7,13 @@ class InspectionHistoryState extends Equatable {
final int passCount; final int passCount;
final int warningCount; final int warningCount;
final int failCount; final int failCount;
final List<InspectionRecord> records;
final bool isLoading;
final String? error;
const InspectionHistoryState({ const InspectionHistoryState({
required this.deviceId, required this.deviceId,
required this.totalCount, this.totalCount = 0,
required this.passCount, this.passCount = 0,
required this.warningCount, this.warningCount = 0,
required this.failCount, this.failCount = 0,
required this.records,
this.isLoading = false,
this.error,
}); });
InspectionHistoryState copyWith({ InspectionHistoryState copyWith({
...@@ -28,9 +22,6 @@ class InspectionHistoryState extends Equatable { ...@@ -28,9 +22,6 @@ class InspectionHistoryState extends Equatable {
int? passCount, int? passCount,
int? warningCount, int? warningCount,
int? failCount, int? failCount,
List<InspectionRecord>? records,
bool? isLoading,
String? error,
}) { }) {
return InspectionHistoryState( return InspectionHistoryState(
deviceId: deviceId ?? this.deviceId, deviceId: deviceId ?? this.deviceId,
...@@ -38,9 +29,6 @@ class InspectionHistoryState extends Equatable { ...@@ -38,9 +29,6 @@ class InspectionHistoryState extends Equatable {
passCount: passCount ?? this.passCount, passCount: passCount ?? this.passCount,
warningCount: warningCount ?? this.warningCount, warningCount: warningCount ?? this.warningCount,
failCount: failCount ?? this.failCount, failCount: failCount ?? this.failCount,
records: records ?? this.records,
isLoading: isLoading ?? this.isLoading,
error: error,
); );
} }
...@@ -51,9 +39,6 @@ class InspectionHistoryState extends Equatable { ...@@ -51,9 +39,6 @@ class InspectionHistoryState extends Equatable {
passCount, passCount,
warningCount, warningCount,
failCount, failCount,
records,
isLoading,
error,
]; ];
} }
......
...@@ -2,12 +2,13 @@ import 'package:auto_route/auto_route.dart'; ...@@ -2,12 +2,13 @@ import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:smart_hotel_app/repositories/inspection_history_repository.dart'; import 'package:smart_hotel_app/repositories/inspection_history_repository.dart';
import 'package:smart_hotel_app/services/inspection_history_service.dart'; import 'package:smart_hotel_app/services/inspection_history_service.dart';
import 'cubit/inspection_history_cubit.dart'; import 'cubit/inspection_history_cubit.dart';
import 'cubit/inspection_history_state.dart'; import 'cubit/inspection_history_state.dart';
import 'widget/statistics_card.dart'; import 'widget/statistics_card.dart';
import 'widget/inspection_record_list.dart'; import 'widget/inspection_record_item.dart';
@RoutePage() @RoutePage()
class InspectionHistoryView extends StatelessWidget { class InspectionHistoryView extends StatelessWidget {
...@@ -24,7 +25,52 @@ class InspectionHistoryView extends StatelessWidget { ...@@ -24,7 +25,52 @@ class InspectionHistoryView extends StatelessWidget {
repository: InspectionHistoryRepository(), repository: InspectionHistoryRepository(),
), ),
), ),
child: Scaffold( child: const _InspectionHistoryBody(),
);
}
}
class _InspectionHistoryBody extends StatefulWidget {
const _InspectionHistoryBody();
@override
State<_InspectionHistoryBody> createState() => _InspectionHistoryBodyState();
}
class _InspectionHistoryBodyState extends State<_InspectionHistoryBody> {
late final PagingController<int, InspectionRecord> _pagingController;
@override
void initState() {
super.initState();
_pagingController = PagingController<int, InspectionRecord>(
getNextPageKey: (state) {
if (state.keys == null || state.keys!.isEmpty) return 1;
final total =
context.read<InspectionHistoryCubit>().state.totalCount;
final nextKey = state.nextIntPageKey;
if (nextKey == null) return null;
if ((nextKey - 1) * InspectionHistoryCubit.pageSize >= total) {
return null;
}
return nextKey;
},
fetchPage: (pageKey) =>
context.read<InspectionHistoryCubit>().fetchPage(pageKey),
);
}
@override
void dispose() {
_pagingController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return BlocBuilder<InspectionHistoryCubit, InspectionHistoryState>(
builder: (context, state) {
return Scaffold(
backgroundColor: const Color.fromRGBO(242, 243, 245, 1), backgroundColor: const Color.fromRGBO(242, 243, 245, 1),
appBar: AppBar( appBar: AppBar(
backgroundColor: Colors.white, backgroundColor: Colors.white,
...@@ -46,29 +92,132 @@ class InspectionHistoryView extends StatelessWidget { ...@@ -46,29 +92,132 @@ class InspectionHistoryView extends StatelessWidget {
), ),
centerTitle: true, centerTitle: true,
), ),
body: SingleChildScrollView( body: PagingListener<int, InspectionRecord>(
padding: EdgeInsets.symmetric(horizontal: 28.w, vertical: 20.h), controller: _pagingController,
child: BlocBuilder<InspectionHistoryCubit, InspectionHistoryState>( builder: (context, pagingState, fetchNextPage) {
builder: (context, state) { return CustomScrollView(
return Column( slivers: [
children: [ SliverToBoxAdapter(
StatisticsCard( child: Padding(
padding: EdgeInsets.symmetric(
horizontal: 28.w, vertical: 20.h),
child: StatisticsCard(
totalCount: state.totalCount, totalCount: state.totalCount,
passCount: state.passCount, passCount: state.passCount,
warningCount: state.warningCount, warningCount: state.warningCount,
failCount: state.failCount, failCount: state.failCount,
), ),
SizedBox(height: 20.h),
InspectionRecordList(
records: state.records,
), ),
SizedBox(height: 30.h), ),
SliverToBoxAdapter(
child: Padding(
padding: EdgeInsets.only(
left: 28.w, right: 28.w, bottom: 16.h),
child: Row(
children: [
Text(
'巡检历史',
style: TextStyle(
fontSize: 28.sp,
fontWeight: FontWeight.bold,
color:
const Color.fromRGBO(10, 13, 20, 1.0),
),
),
Text(
' (${state.totalCount})',
style: TextStyle(
fontSize: 28.sp,
fontWeight: FontWeight.bold,
color:
const Color.fromRGBO(10, 13, 20, 1.0),
),
),
], ],
); ),
),
),
SliverPadding(
padding: EdgeInsets.symmetric(horizontal: 28.w),
sliver: PagedSliverList<int, InspectionRecord>(
state: pagingState,
fetchNextPage: fetchNextPage,
builderDelegate:
PagedChildBuilderDelegate<InspectionRecord>(
firstPageErrorIndicatorBuilder: (_) => Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
'加载失败: ${pagingState.error}',
style: TextStyle(
fontSize: 28.sp,
color: const Color.fromRGBO(
255, 100, 101, 1),
),
textAlign: TextAlign.center,
),
SizedBox(height: 24.h),
ElevatedButton(
onPressed: fetchNextPage,
child: Text('重新加载',
style:
TextStyle(fontSize: 28.sp)),
),
],
),
),
noItemsFoundIndicatorBuilder: (_) => Padding(
padding: EdgeInsets.only(top: 60.h),
child: Text(
'暂无数据',
style: TextStyle(
fontSize: 28.sp,
color: const Color.fromRGBO(
100, 116, 139, 1),
),
),
),
newPageErrorIndicatorBuilder: (_) => Center(
child: Padding(
padding:
EdgeInsets.symmetric(vertical: 20.h),
child: Column(
children: [
Text(
'加载失败: ${pagingState.error}',
style: TextStyle(
fontSize: 24.sp,
color: const Color.fromRGBO(
255, 100, 101, 1),
),
),
SizedBox(height: 12.h),
TextButton(
onPressed: fetchNextPage,
child: Text('重试',
style:
TextStyle(fontSize: 24.sp)),
),
],
),
),
),
itemBuilder: (_, item, __) {
return InspectionRecordItem(record: item);
}, },
), ),
), ),
), ),
SliverToBoxAdapter(
child: SizedBox(height: 30.h),
),
],
);
},
),
);
},
); );
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment