150 lines
4.4 KiB
Dart
150 lines
4.4 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:environmental_protection/common/user_information.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
class ServicePage extends StatefulWidget {
|
|
const ServicePage({super.key});
|
|
|
|
@override
|
|
State<ServicePage> createState() => _ServicePageState();
|
|
}
|
|
|
|
class _ServicePageState extends State<ServicePage> with SingleTickerProviderStateMixin {
|
|
late final TabController _controller;
|
|
List<Map<String, String>> notices = [];
|
|
List<Map<String, String>> allNotices = [];
|
|
bool isLoading = true;
|
|
@override
|
|
void initState() {
|
|
_controller = TabController(length: 3, vsync: this);
|
|
_fetchNoticeData();
|
|
super.initState();
|
|
}
|
|
void _fetchNoticeData() async {
|
|
var list = await GlobalInformation.getInstance().requester.get(resolve("api/notice/list"));
|
|
var bodyList = jsonDecode(list.body);
|
|
setState((){
|
|
bodyList['rows'].forEach((e) {
|
|
allNotices.add({"title": e['noticeTitle'], "status": e['status'] == "0" ? "未读" : "已读", "time": e["updateTime"], "content": e["noticeContent"], "phone": e['phone'], "unit": e['unit']});
|
|
});
|
|
notices.addAll(allNotices);
|
|
isLoading = false;
|
|
});
|
|
}
|
|
void updateNotices(){
|
|
final e = _controller.index;
|
|
print(e);
|
|
setState(() {
|
|
notices.clear();
|
|
var targetType = "未读";
|
|
if (e == 0){
|
|
notices.addAll(allNotices);
|
|
return;
|
|
}else if (e == 2) {
|
|
targetType = "已读";
|
|
}
|
|
for (var e in allNotices) {
|
|
if (e['status'] == targetType){
|
|
notices.add(e);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text("社区公告"),
|
|
|
|
),
|
|
body: Column(
|
|
children: [
|
|
TabBar(
|
|
tabs: const [
|
|
Text("全部"),
|
|
Text("未读"),
|
|
Text("已读")
|
|
],
|
|
controller: _controller,
|
|
onTap: (e) {
|
|
updateNotices();
|
|
},
|
|
),
|
|
Expanded(
|
|
child: isLoading ? const Center(child: CircularProgressIndicator()) : ListView.builder(
|
|
itemCount: notices.length,
|
|
itemBuilder: (context, index) {
|
|
return _buildNoticeItem(index);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildNoticeItem(int index) {
|
|
return Card(
|
|
child: ListTile(
|
|
title: Text(notices[index]["title"]!),
|
|
subtitle: Text(notices[index]["content"]!, overflow: TextOverflow.ellipsis, maxLines: 3,),
|
|
trailing: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.end,
|
|
children: [
|
|
Text(notices[index]["status"]!, style: const TextStyle(color: Colors.green)),
|
|
Text(notices[index]["time"]!, style: const TextStyle(fontSize: 12)),
|
|
],
|
|
),
|
|
onTap: () {
|
|
final tmp_notice = notices[index];
|
|
if (notices[index]['status'] == "未读"){
|
|
setState(() {
|
|
notices[index]['status'] = "已读";
|
|
updateNotices();
|
|
});
|
|
}
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => NoticeDetailPage(notice: tmp_notice),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class NoticeDetailPage extends StatelessWidget {
|
|
final Map<String, String> notice;
|
|
|
|
const NoticeDetailPage({super.key, required this.notice});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text("公告详情"),
|
|
),
|
|
body: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: ListView(
|
|
children: [
|
|
Text(notice["title"]!, style: const TextStyle(fontSize: 24, fontWeight: FontWeight.bold)),
|
|
const SizedBox(height: 10),
|
|
Text("发布时间: ${notice["time"]}", style: const TextStyle(fontSize: 16)),
|
|
const SizedBox(height: 20),
|
|
const SizedBox(height: 20),
|
|
Text(notice["content"]!, style: const TextStyle(fontSize: 16)),
|
|
const SizedBox(height: 20),
|
|
Text("发布单位: ${notice['unit']}", style: const TextStyle(fontSize: 16)),
|
|
const SizedBox(height: 10),
|
|
Text("联系电话: ${notice['phone']}", style: const TextStyle(fontSize: 16)),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|