diff --git a/lib/models/contact_model.dart b/lib/models/contact_model.dart index 3c506dd..a10769a 100644 --- a/lib/models/contact_model.dart +++ b/lib/models/contact_model.dart @@ -25,11 +25,14 @@ class Contact extends ChangeNotifier { List friendGroups = []; String defaultGroup = ''; Map groupChats = {}; + int friendCount = 0; void init(Map json) { defaultGroup = json['defaultGroup']; + friendCount = 0; json['friends'].forEach((key, value) { + friendCount += 1; friends[key] = FriendSetting.fromJson(value); }); // json['groupChats'].forEach((key, value) { @@ -53,7 +56,8 @@ class Contact extends ChangeNotifier { } void addFriend(String friendId, Map friendSetting) { - friends.addAll({friendId: FriendSetting.fromJson(friendSetting)}); + friends[friendId] = FriendSetting.fromJson(friendSetting); + friendCount += 1; notifyListeners(); } @@ -67,6 +71,7 @@ class Contact extends ChangeNotifier { void removeFriend(String friendId) { friends.remove(friendId); + friendCount -= 1; notifyListeners(); } } diff --git a/lib/screens/contact/components/friend_group.dart b/lib/screens/contact/components/friend_group.dart index 94d3082..751da0a 100755 --- a/lib/screens/contact/components/friend_group.dart +++ b/lib/screens/contact/components/friend_group.dart @@ -1,12 +1,12 @@ import 'package:flutter/material.dart'; +import 'package:get_it_mixin/get_it_mixin.dart'; import 'package:together_mobile/models/contact_model.dart'; -import 'package:together_mobile/models/init_get_it.dart'; import 'friend_tile.dart'; -class FriendGroup extends StatelessWidget { - const FriendGroup({ +class FriendGroup extends StatelessWidget with GetItMixin { + FriendGroup({ super.key, required this.groupName, }); @@ -15,26 +15,21 @@ class FriendGroup extends StatelessWidget { @override Widget build(BuildContext context) { + final friends = watchOnly((Contact contact) => contact.friends); + return ExpansionTile( title: Text(groupName), trailing: Text( - getIt - .get() - .filterGroupFriends(groupName) - .length - .toString(), + get().filterGroupFriends(groupName).length.toString(), ), children: List.generate( - getIt.get().filterGroupFriends(groupName).length, + get().filterGroupFriends(groupName).length, (index) => FriendTile( key: ValueKey( - getIt.get().friends.keys.toList()[index], + friends.keys.toList()[index], ), - friendId: getIt - .get() - .filterGroupFriends(groupName) - .keys - .toList()[index], + friendId: + get().filterGroupFriends(groupName).keys.toList()[index], ), ), ); diff --git a/lib/screens/contact/contact_apply_screen/apply_list_screen.dart b/lib/screens/contact/contact_apply_screen/apply_list_screen.dart index 42517a8..90680b3 100755 --- a/lib/screens/contact/contact_apply_screen/apply_list_screen.dart +++ b/lib/screens/contact/contact_apply_screen/apply_list_screen.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:go_router/go_router.dart'; import 'package:together_mobile/models/apply_list_model.dart'; import 'package:together_mobile/models/init_get_it.dart'; @@ -53,6 +54,14 @@ class _ApplyListScreenState extends State { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( + leading: IconButton( + onPressed: () { + // Have to replace the default back button, + // because default back button use the old state + context.goNamed('Contact'); + }, + icon: const Icon(Icons.arrow_back), + ), centerTitle: true, title: const Text('申请列表'), ), diff --git a/lib/screens/contact/contact_apply_screen/components/apply_bottom_sheet.dart b/lib/screens/contact/contact_apply_screen/components/apply_bottom_sheet.dart index 071e2e2..13b9146 100755 --- a/lib/screens/contact/contact_apply_screen/components/apply_bottom_sheet.dart +++ b/lib/screens/contact/contact_apply_screen/components/apply_bottom_sheet.dart @@ -1,5 +1,4 @@ import 'dart:convert'; -import 'dart:io'; import 'package:cached_network_image/cached_network_image.dart'; import 'package:cherry_toast/cherry_toast.dart'; @@ -215,16 +214,6 @@ class _ApplyBottomSheetState extends State { context.pop(); if (res['code'] == 10600) { - // ignore: use_build_context_synchronously - CherryToast.success( - title: const Text( - '添加好友成功', - style: TextStyle( - color: kContentColorLight, - ), - ), - ).show(context); - final accountProfile = {}; accountProfile.addAll(getIt.get().toMap()); accountProfile.addAll(getIt.get().toMap()); @@ -243,6 +232,15 @@ class _ApplyBottomSheetState extends State { widget.apply.applicant!, widget.accountProfile, ); + // ignore: use_build_context_synchronously + CherryToast.success( + title: const Text( + '添加好友成功', + style: TextStyle( + color: kContentColorLight, + ), + ), + ).show(context); widget.refreshCallback(); } else { // ignore: use_build_context_synchronously diff --git a/lib/screens/contact/contact_screen.dart b/lib/screens/contact/contact_screen.dart index b6cb0ba..0fc0d85 100755 --- a/lib/screens/contact/contact_screen.dart +++ b/lib/screens/contact/contact_screen.dart @@ -30,16 +30,14 @@ class _ContactScreenState extends State with GetItStateMixin { @override Widget build(BuildContext context) { - Map friends = watchOnly( - (Contact contact) => contact.friends, - ); - List friendGroups = watchOnly( - (Contact contact) => contact.friendGroups, + // Seems like `watchOnly` cannot watch collecion type. + int friendCount = watchOnly( + (Contact contact) => contact.friendCount, ); + int applyCount = watchOnly( (ApplyList applyList) => applyList.count, ); - int friendCount = friends.length; // Create a localkey, use to generate the custom scroll view, // or there will be a error: "Duplicate GlobalKey detected in widget tree." @@ -169,12 +167,14 @@ class _ContactScreenState extends State with GetItStateMixin { delegate: SliverChildBuilderDelegate( (BuildContext context, int index) { if (_shows['friendGroups']!) { + String groupName = get().friendGroups[index]; return FriendGroup( - key: ValueKey(friendGroups[index]), - groupName: friendGroups[index], + key: ValueKey(groupName), + groupName: groupName, ); } else if (_shows['allFriends']!) { - String friendId = friends.keys.toList()[index]; + String friendId = + get().friends.keys.toList()[index]; // print(friends); return FriendTile( key: ValueKey( @@ -187,7 +187,7 @@ class _ContactScreenState extends State with GetItStateMixin { } }, childCount: _shows['friendGroups']! - ? friendGroups.length + ? get().friendGroups.length : _shows['allFriends']! ? friendCount : get().groupChats.length, diff --git a/lib/screens/contact/manage_group_screen/components/group_bottom_sheet.dart b/lib/screens/contact/manage_group_screen/components/group_bottom_sheet.dart index aa6786d..7d2beb8 100644 --- a/lib/screens/contact/manage_group_screen/components/group_bottom_sheet.dart +++ b/lib/screens/contact/manage_group_screen/components/group_bottom_sheet.dart @@ -4,8 +4,6 @@ import 'package:go_router/go_router.dart'; import 'package:cherry_toast/cherry_toast.dart'; import 'package:together_mobile/common/constants.dart'; -import 'package:together_mobile/models/contact_model.dart'; -import 'package:together_mobile/models/init_get_it.dart'; class EditGroupBottomSheet extends StatefulWidget { const EditGroupBottomSheet({ diff --git a/lib/screens/contact/manage_group_screen/manage_group_screen.dart b/lib/screens/contact/manage_group_screen/manage_group_screen.dart index 7892459..521d258 100755 --- a/lib/screens/contact/manage_group_screen/manage_group_screen.dart +++ b/lib/screens/contact/manage_group_screen/manage_group_screen.dart @@ -128,7 +128,7 @@ class _FriendGroupScreenState extends State { getIt.get().friendGroups.length - _deletedOriginGroups.length; setState(() { - _deletedOriginGroups.addAll(List.filled(sub - 1, '')); + _deletedOriginGroups.addAll(List.filled(sub, '')); }); Map res = await manageGroups( diff --git a/lib/screens/contact_add/search_new_screen.dart b/lib/screens/contact_add/search_new_screen.dart index 7ecf0f1..0bd4157 100755 --- a/lib/screens/contact_add/search_new_screen.dart +++ b/lib/screens/contact_add/search_new_screen.dart @@ -1,5 +1,3 @@ -import 'dart:io'; - import 'package:cherry_toast/cherry_toast.dart'; import 'package:flutter/material.dart'; @@ -10,8 +8,6 @@ import 'package:together_mobile/models/contact_model.dart'; import 'package:together_mobile/models/init_get_it.dart'; import 'package:together_mobile/models/user_model.dart'; import 'package:together_mobile/request/search_new.dart'; -import 'package:together_mobile/request/user_profile.dart'; -import 'package:together_mobile/utils/app_dir.dart'; class SearchNewScreen extends StatefulWidget { const SearchNewScreen({super.key}); diff --git a/lib/screens/friend_profile/components/row_floating_buttons.dart b/lib/screens/friend_profile/components/row_floating_buttons.dart index f43a30f..5ddae8f 100755 --- a/lib/screens/friend_profile/components/row_floating_buttons.dart +++ b/lib/screens/friend_profile/components/row_floating_buttons.dart @@ -73,13 +73,15 @@ class RowFloatingButtons extends StatelessWidget { ), ), FloatingActionButton( - onPressed: () { + onPressed: () async { try { Hive.box('message_$contactId'); } catch (e) { - Hive.openBox('message_$contactId'); + // have to await it + await Hive.openBox('message_$contactId'); } + // ignore: use_build_context_synchronously context.goNamed( 'Message', queryParameters: {'contactId': contactId}, diff --git a/lib/screens/more/profile_screen/change_profile_screens/change_avatar_screen.dart b/lib/screens/more/profile_screen/change_profile_screens/change_avatar_screen.dart index d7faa02..365d391 100755 --- a/lib/screens/more/profile_screen/change_profile_screens/change_avatar_screen.dart +++ b/lib/screens/more/profile_screen/change_profile_screens/change_avatar_screen.dart @@ -1,17 +1,15 @@ -import 'dart:io'; import 'dart:typed_data'; +import 'package:flutter/material.dart'; + import 'package:cached_network_image/cached_network_image.dart'; import 'package:cherry_toast/cherry_toast.dart'; -import 'package:flutter/material.dart'; import 'package:image_picker/image_picker.dart'; import 'package:image_editor_plus/image_editor_plus.dart'; import 'package:image_editor_plus/utils.dart'; - import 'package:together_mobile/common/constants.dart'; import 'package:together_mobile/request/server.dart'; -import 'package:together_mobile/utils/app_dir.dart'; import 'package:together_mobile/models/init_get_it.dart'; import 'package:together_mobile/models/user_model.dart'; import 'package:together_mobile/request/user_profile.dart'; diff --git a/lib/screens/more/profile_screen/main_profile.dart b/lib/screens/more/profile_screen/main_profile.dart index 09476a7..080781f 100755 --- a/lib/screens/more/profile_screen/main_profile.dart +++ b/lib/screens/more/profile_screen/main_profile.dart @@ -1,12 +1,10 @@ -import 'dart:io'; +import 'package:flutter/material.dart'; import 'package:cached_network_image/cached_network_image.dart'; -import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:together_mobile/common/constants.dart'; import 'package:together_mobile/models/init_get_it.dart'; - import 'package:together_mobile/models/user_model.dart'; import 'package:together_mobile/request/server.dart'; import 'components/profile_card.dart';