114 lines
3.4 KiB
Dart
114 lines
3.4 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:liquid_glass_widgets/widgets/shared/glass_page.dart';
|
|
import 'package:liquid_glass_widgets/widgets/surfaces/glass_bottom_bar.dart';
|
|
import 'package:liquid_glass_widgets/widgets/surfaces/glass_scaffold.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:sweet_chat_app/config/AppConfig.dart';
|
|
import 'package:sweet_chat_app/models/im_message.dart';
|
|
import 'package:sweet_chat_app/pages/contact_page.dart';
|
|
import 'package:sweet_chat_app/pages/explore_page.dart';
|
|
import 'package:sweet_chat_app/pages/message_page.dart';
|
|
import 'package:sweet_chat_app/providers/app_provider.dart';
|
|
import 'package:sweet_chat_app/services/notify_service.dart';
|
|
import 'package:sweet_chat_app/services/webSocket_service.dart';
|
|
import 'package:sweet_chat_app/utils/sp_utils.dart';
|
|
|
|
|
|
class HomePage extends StatefulWidget {
|
|
const HomePage({super.key});
|
|
|
|
@override
|
|
State<HomePage> createState() => _HomePageState();
|
|
}
|
|
|
|
class _HomePageState extends State<HomePage> {
|
|
late StreamSubscription<String> _msgSub;
|
|
final NotifyService notifyService = NotifyService();
|
|
int _index = 0;
|
|
late int userId;
|
|
|
|
final _pages = [
|
|
const MessagePage(),
|
|
const ContactsPage(),
|
|
const ExplorePage(),
|
|
const Center(child: Text('⚙ 我的', style: TextStyle(fontSize: 26))),
|
|
];
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
initWebsocket();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_msgSub.cancel();
|
|
super.dispose();
|
|
}
|
|
|
|
void initWebsocket() {
|
|
userId = SpUtil.getInt('userId') ?? 0;
|
|
WebSocketService().connect('${AppConfig.wsUrl}?userId=$userId');
|
|
WebSocketService().send(ImMessage.online(userId).toJsonString());
|
|
|
|
_msgSub = WebSocketService().messages.listen((websocketMessage) {
|
|
_handleMessage(websocketMessage);
|
|
});
|
|
}
|
|
|
|
void _handleMessage(String wsMsg) {
|
|
if (!mounted) return;
|
|
|
|
final ImMessage imMsg = ImMessage.fromJsonString(wsMsg);
|
|
|
|
// 非自己的消息
|
|
if (imMsg.from != userId.toString()) {
|
|
final appProvider = Provider.of<AppProvider>(context, listen: false);
|
|
// 如果已经在聊天中 则不用提醒
|
|
if (appProvider.routerName == 'chat') {
|
|
return;
|
|
}
|
|
|
|
notifyService.showInstantNotification(title: imMsg.title, body: imMsg.content);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return GlassScaffold(
|
|
statusBarStyle: GlassStatusBarStyle.auto,
|
|
bottomBar: GlassBottomBar(
|
|
selectedIndex: _index,
|
|
onTabSelected: (i) => setState(() => _index = i),
|
|
selectedIconColor: Colors.purple.shade200,
|
|
unselectedIconColor: Colors.white,
|
|
tabs: const [
|
|
GlassBottomBarTab(
|
|
icon: Icon(Icons.chat_bubble_outline),
|
|
activeIcon: Icon(Icons.chat_bubble),
|
|
label: '消息',
|
|
),
|
|
GlassBottomBarTab(
|
|
icon: Icon(Icons.contacts_outlined),
|
|
activeIcon: Icon(Icons.contacts),
|
|
label: '通讯录',
|
|
),
|
|
GlassBottomBarTab(
|
|
icon: Icon(Icons.explore_outlined),
|
|
activeIcon: Icon(Icons.explore),
|
|
label: '发现',
|
|
),
|
|
GlassBottomBarTab(
|
|
icon: Icon(Icons.person_outline),
|
|
activeIcon: Icon(Icons.person),
|
|
label: '我的',
|
|
),
|
|
],
|
|
),
|
|
body: _pages[_index],
|
|
);
|
|
}
|
|
}
|