qhd-prevention-flutter/lib/pages/home/work/laws_regulations_two_page.dart

323 lines
8.8 KiB
Dart
Raw Normal View History

2025-08-20 09:56:31 +08:00
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:path_provider/path_provider.dart';
import 'package:intl/intl.dart';
import 'package:dio/dio.dart';
import 'package:qhd_prevention/pages/home/work/read_file_page.dart';
import 'package:qhd_prevention/pages/home/work/risk_list_picker.dart';
import 'package:qhd_prevention/pages/http/ApiService.dart';
import 'package:qhd_prevention/pages/my_appbar.dart';
import '../../../customWidget/search_bar_widget.dart';
import '../../../tools/tools.dart';
import 'laws_list_picker.dart';
import 'package:url_launcher/url_launcher.dart';
class LawsRegulationsTwoPage extends StatefulWidget {
const LawsRegulationsTwoPage(this.id, {super.key});
final String id;
@override
State<LawsRegulationsTwoPage> createState() => _LawsRegulationsTwoPage();
}
class _LawsRegulationsTwoPage extends State<LawsRegulationsTwoPage> {
final TextEditingController _searchController = TextEditingController();
List<dynamic> _fileList = [];
@override
void initState() {
super.initState();
_getLowListByParentId();
}
Future<void> _getLowListByParentId() async {
try {
_fileList.clear();
final result = await ApiService.getLowListByParentId(widget.id);
if (result['result'] == 'success') {
final List<dynamic> newList = result['varList'] ?? [];
setState(() {
_fileList.addAll(newList);
});
} else {
_showMessage('加载数据失败');
}
} catch (e) {
// 出错时可以 Toast 或者在页面上显示错误状态
print('加载数据失败:$e');
}
}
Future<void> _downloadFile(String filePath) async {
//下载
try {
final Uri url = Uri.parse(ApiService.baseImgPath + filePath);
if (!await launchUrl(url,mode:LaunchMode.externalApplication)) {
throw Exception('Could not launch $url');
}
} catch (e) {
print(e.toString());
}
}
Future<void> _showFile(dynamic file) async {
//预览
try {
final url =ApiService.baseImgPath + file["FILEPATH"];
pushPage(
ReadFilePage(
fileUrl: url,
),
context,
);
} catch (e) {
print(e.toString());
}
}
String _formatDate(String dateString) {
try {
final date = DateTime.parse(dateString);
return DateFormat('yyyy-MM-dd HH:mm:ss').format(date);
} catch (e) {
return dateString;
}
}
String _getFileType(String extension) {
switch (extension) {
case '.docx':
case '.doc':
return '文件属性:文档';
case '.xls':
case '.xlsx':
return '文件属性:表格';
case '.ppt':
case '.pptx':
return '文件属性:幻灯片';
case '.pdf':
return '文件属性PDF';
default:
return '文件属性:';
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: MyAppbar(title: "法律法规"),
body: Column(
children: [
// 搜索栏
// Container(
// color: Colors.white,
// child:Padding(
// padding: EdgeInsets.all(10),
// child: SearchBarWidget(
// controller: _searchController,
// onSearch: (keyboard) {
// // 输入请求接口
// _getLowListByParentId();
// },
// ),
// ),
// ),
Expanded(child: _buildContent()),
],
),
);
}
Widget _buildContent() {
if (_fileList.isNotEmpty) {
return ListView.builder(
itemCount: _fileList.length,
itemBuilder:
(context, index) => FileCard(
file: _fileList[index],
onDownload: _downloadFile,
onPreview: _showFile,
formatDate: _formatDate,
getFileType: _getFileType,
),
);
} else {
return const Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.folder_open, size: 64, color: Colors.grey),
SizedBox(height: 16),
Text('暂无数据', style: TextStyle(fontSize: 18, color: Colors.grey)),
],
),
);
}
}
void _showMessage(String msg) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(msg)));
}
}
class CustomCollapse extends StatefulWidget {
final dynamic item;
final Function(String) onTap;
const CustomCollapse({super.key, required this.item, required this.onTap});
@override
State<CustomCollapse> createState() => _CustomCollapseState();
}
class _CustomCollapseState extends State<CustomCollapse> {
bool _isExpanded = false;
@override
Widget build(BuildContext context) {
final hasChildren =
widget.item['children'] != null &&
(widget.item['children'] as List).isNotEmpty;
return Card(
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: Column(
children: [
ListTile(
title: Text(
widget.item['name'] ?? '未命名',
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
),
trailing:
hasChildren
? Icon(
_isExpanded ? Icons.expand_less : Icons.expand_more,
color: Colors.blue,
)
: null,
onTap:
hasChildren
? () => setState(() => _isExpanded = !_isExpanded)
: () => widget.onTap(widget.item['id'] ?? ''),
),
if (_isExpanded && hasChildren)
...(widget.item['children'] as List).map<Widget>((child) {
return Padding(
padding: const EdgeInsets.only(left: 16.0),
child: ListTile(
title: Text(child['name']),
leading: const Icon(Icons.description, color: Colors.blue),
onTap: () => widget.onTap(child['id'] ?? ''),
),
);
}).toList(),
],
),
);
}
}
class FileCard extends StatelessWidget {
final dynamic file;
final Function(String) onDownload;
final Function(dynamic) onPreview;
final String Function(String) formatDate;
final String Function(String) getFileType;
const FileCard({
super.key,
required this.file,
required this.onDownload,
required this.onPreview,
required this.formatDate,
required this.getFileType,
});
@override
Widget build(BuildContext context) {
return Card(
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
file['NAME'] ?? '未命名文件',
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
const SizedBox(height: 10),
Text(
getFileType(file['extension_name'] ?? ''),
style: TextStyle(color: Colors.grey[600]),
),
const SizedBox(height: 5),
Text(
'上传日期:${formatDate(file['CTIME'] ?? '')}',
style: TextStyle(color: Colors.grey[600]),
),
const SizedBox(height: 15),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
OutlinedButton.icon(
icon: const Icon(Icons.download, size: 18),
label: const Text('下载'),
onPressed: () => onDownload(file['FILEPATH'] ?? ''),
style: OutlinedButton.styleFrom(
foregroundColor: Colors.blue,
side: const BorderSide(color: Colors.blue),
),
),
const SizedBox(width: 10),
ElevatedButton.icon(
icon: const Icon(Icons.visibility, size: 18),
label: const Text('预览'),
onPressed: () => onPreview(file),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.cyan,
foregroundColor: Colors.white,
),
),
],
),
],
),
),
);
}
}
//详情页
class StudyDetailPage extends StatelessWidget {
final String parentId;
const StudyDetailPage({super.key, required this.parentId});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('详情页面')),
body: Center(child: Text('父级ID: $parentId')),
);
}
}