146 lines
4.5 KiB
Dart
146 lines
4.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:get_it_mixin/get_it_mixin.dart';
|
|
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:badges/badges.dart' as badges;
|
|
import 'package:hive_flutter/hive_flutter.dart';
|
|
import 'package:together_mobile/database/box_type.dart';
|
|
import 'package:together_mobile/database/hive_database.dart';
|
|
import 'package:together_mobile/models/apply_list_model.dart';
|
|
import 'package:together_mobile/models/init_get_it.dart';
|
|
import 'package:together_mobile/models/user_model.dart';
|
|
import 'package:together_mobile/utils/app_dir.dart';
|
|
|
|
class HomeScreenWithNavBar extends StatefulWidget
|
|
with GetItStatefulWidgetMixin {
|
|
HomeScreenWithNavBar({
|
|
super.key,
|
|
required this.initIndex,
|
|
required this.child,
|
|
this.listController,
|
|
});
|
|
|
|
final int initIndex;
|
|
final Widget child;
|
|
// used to get back to top
|
|
final ScrollController? listController;
|
|
|
|
@override
|
|
State<HomeScreenWithNavBar> createState() => _HomeScreenWithNavBarState();
|
|
}
|
|
|
|
class _HomeScreenWithNavBarState extends State<HomeScreenWithNavBar>
|
|
with GetItStateMixin {
|
|
Future<bool> _openBox() async {
|
|
if (!getIt.get<UserProfile>().isInitialised) {
|
|
List<int> encryptionKeyUint8List = await getEncryptKey();
|
|
|
|
await Hive.initFlutter(await getBoxDir());
|
|
|
|
Box<ChatSetting> chatSettingBox =
|
|
await Hive.openBox<ChatSetting>('chat_setting');
|
|
|
|
final openedChats =
|
|
chatSettingBox.values.where((element) => element.isOpen);
|
|
|
|
for (var chatBox in openedChats) {
|
|
await Hive.openBox<MessageT>(
|
|
'message_${chatBox.contactId}',
|
|
encryptionCipher: HiveAesCipher(encryptionKeyUint8List),
|
|
compactionStrategy: (entries, deletedEntries) => entries > 200,
|
|
);
|
|
}
|
|
}
|
|
return Future(() => true);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
int applyCount = watchOnly((ApplyList applyList) => applyList.count);
|
|
|
|
return Scaffold(
|
|
body: widget.child,
|
|
bottomNavigationBar: BottomNavigationBar(
|
|
backgroundColor:
|
|
Theme.of(context).bottomNavigationBarTheme.backgroundColor,
|
|
items: [
|
|
BottomNavigationBarItem(
|
|
icon: FutureBuilder(
|
|
future: _openBox(),
|
|
builder: (context, snapshot) {
|
|
if (snapshot.hasData) {
|
|
return ValueListenableBuilder(
|
|
valueListenable:
|
|
Hive.box<ChatSetting>('chat_setting').listenable(),
|
|
builder: (context, chatSettingBox, _) {
|
|
final values = chatSettingBox.values;
|
|
int unreadCount = 0;
|
|
for (var value in values) {
|
|
unreadCount += value.unreadCount;
|
|
}
|
|
return badges.Badge(
|
|
badgeContent: Text(
|
|
unreadCount > 99 ? '$unreadCount+' : '$unreadCount',
|
|
style: TextStyle(
|
|
fontSize: 11,
|
|
color: Theme.of(context).colorScheme.inversePrimary,
|
|
),
|
|
),
|
|
showBadge: unreadCount > 0,
|
|
child: const Icon(Icons.message),
|
|
);
|
|
},
|
|
);
|
|
} else {
|
|
return const Icon(Icons.message);
|
|
}
|
|
},
|
|
),
|
|
label: '消息',
|
|
),
|
|
BottomNavigationBarItem(
|
|
icon: badges.Badge(
|
|
badgeContent: Text(
|
|
applyCount > 99 ? '$applyCount+' : '$applyCount',
|
|
style: TextStyle(
|
|
fontSize: 11,
|
|
color: Theme.of(context).colorScheme.inversePrimary,
|
|
),
|
|
),
|
|
showBadge: applyCount > 0,
|
|
child: const Icon(Icons.contacts),
|
|
),
|
|
label: '通讯录',
|
|
),
|
|
const BottomNavigationBarItem(
|
|
icon: Icon(Icons.star),
|
|
label: '更多',
|
|
),
|
|
],
|
|
onTap: (int index) => _onItemTapped(index, context),
|
|
currentIndex: widget.initIndex,
|
|
),
|
|
);
|
|
}
|
|
|
|
void _onItemTapped(int index, BuildContext context) {
|
|
// setState(() {
|
|
// _selectedIndex = index;
|
|
// });
|
|
|
|
switch (index) {
|
|
case 0:
|
|
GoRouter.of(context).go('/chat');
|
|
break;
|
|
|
|
case 1:
|
|
GoRouter.of(context).go('/contact');
|
|
break;
|
|
|
|
case 2:
|
|
GoRouter.of(context).go('/more');
|
|
break;
|
|
}
|
|
}
|
|
}
|