diff --git a/lib/main.dart b/lib/main.dart index c247d4d..b27ebe0 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -14,7 +14,7 @@ main() async { 'username': 'WUvFG3gY', 'password': 'ZogPgBF6' })); - print(response.body); + // print(response.body); global.token = jsonDecode(response.body)['token']; runApp(const DigitalLife()); } diff --git a/lib/views/service.dart b/lib/views/service.dart index 886e4d2..d341c3e 100644 --- a/lib/views/service.dart +++ b/lib/views/service.dart @@ -1,24 +1,8 @@ +import 'dart:convert'; + +import 'package:environmental_protection/common/user_information.dart'; import 'package:flutter/material.dart'; -void main() { - runApp(const MyApp()); -} - -class MyApp extends StatelessWidget { - const MyApp({super.key}); - - @override - Widget build(BuildContext context) { - return MaterialApp( - title: 'Community Notice', - theme: ThemeData( - primarySwatch: Colors.blue, - ), - home: const ServicePage(), - ); - } -} - class ServicePage extends StatefulWidget { const ServicePage({super.key}); @@ -26,39 +10,64 @@ class ServicePage extends StatefulWidget { State createState() => _ServicePageState(); } -class _ServicePageState extends State { - List> notices = [ - { - "title": "消杀通知", - "status": "未读", - "content": "为了给大家提供一个舒适、卫生的生活环境...", - "time": "2020-12-05 11:02" - }, - { - "title": "缴费通知", - "status": "未读", - "content": "为了给大家提供一个舒适、卫生的生活环境...", - "time": "2020-11-21 09:45" - }, - { - "title": "物业通知", - "status": "已读", - "content": "为了给大家提供一个舒适、卫生的生活环境...", - "time": "2020-08-01 13:55" - }, - ]; - +class _ServicePageState extends State with SingleTickerProviderStateMixin { + late final TabController _controller; + List> notices = []; + List> 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; + }); + } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text("社区公告"), + ), body: Column( children: [ - _buildStatusMenu(), + TabBar( + tabs: const [ + Text("全部"), + Text("未读"), + Text("已读") + ], + controller: _controller, + onTap: (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); + } + } + }); + }, + ), Expanded( - child: ListView.builder( + child: isLoading ? const Center(child: CircularProgressIndicator()) : ListView.builder( itemCount: notices.length, itemBuilder: (context, index) { return _buildNoticeItem(index); @@ -70,25 +79,11 @@ class _ServicePageState extends State { ); } - Widget _buildStatusMenu() { - return const Padding( - padding: EdgeInsets.all(8.0), - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceEvenly, - children: [ - Text("全部", style: TextStyle(fontWeight: FontWeight.bold)), - Text("未读", style: TextStyle(color: Colors.grey)), - Text("已读", style: TextStyle(color: Colors.grey)), - ], - ), - ); - } - Widget _buildNoticeItem(int index) { return Card( child: ListTile( title: Text(notices[index]["title"]!), - subtitle: Text(notices[index]["content"]!), + subtitle: Text(notices[index]["content"]!, overflow: TextOverflow.ellipsis, maxLines: 3,), trailing: Column( crossAxisAlignment: CrossAxisAlignment.end, children: [ @@ -129,13 +124,12 @@ class NoticeDetailPage extends StatelessWidget { const SizedBox(height: 10), Text("发布时间: ${notice["time"]}", style: const TextStyle(fontSize: 16)), const SizedBox(height: 20), - const Placeholder(fallbackHeight: 150), const SizedBox(height: 20), Text(notice["content"]!, style: const TextStyle(fontSize: 16)), const SizedBox(height: 20), - const Text("发布单位: 红星海五期物业管理公司", style: TextStyle(fontSize: 16)), + Text("发布单位: ${notice['unit']}", style: const TextStyle(fontSize: 16)), const SizedBox(height: 10), - const Text("联系电话: 88888888", style: TextStyle(fontSize: 16)), + Text("联系电话: ${notice['phone']}", style: const TextStyle(fontSize: 16)), ], ), ),