OpenContacts/lib/widgets/friends/friends_list.dart

91 lines
3.2 KiB
Dart
Raw Permalink Normal View History

import 'package:contacts_plus_plus/clients/messaging_client.dart';
import 'package:contacts_plus_plus/widgets/default_error_widget.dart';
import 'package:contacts_plus_plus/widgets/friends/expanding_input_fab.dart';
import 'package:contacts_plus_plus/widgets/friends/friend_list_tile.dart';
2023-04-29 13:18:46 -04:00
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
2023-04-29 13:18:46 -04:00
2023-05-04 12:16:19 -04:00
class FriendsList extends StatefulWidget {
const FriendsList({super.key});
2023-04-29 13:18:46 -04:00
@override
State<FriendsList> createState() => _FriendsListState();
2023-04-29 13:18:46 -04:00
}
2023-06-03 11:17:54 -04:00
class _FriendsListState extends State<FriendsList> with AutomaticKeepAliveClientMixin {
2023-05-03 12:43:06 -04:00
String _searchFilter = "";
2023-04-30 09:43:59 -04:00
2023-04-29 13:18:46 -04:00
@override
Widget build(BuildContext context) {
2023-06-03 11:17:54 -04:00
super.build(context);
return ChangeNotifierProvider.value(
value: Provider.of<MessagingClient>(context, listen: false),
child: Stack(
alignment: Alignment.topCenter,
children: [
Consumer<MessagingClient>(builder: (context, mClient, _) {
if (mClient.initStatus == null) {
return const LinearProgressIndicator();
} else if (mClient.initStatus!.isNotEmpty) {
return Column(
children: [
Expanded(
child: DefaultErrorWidget(
message: mClient.initStatus,
onRetry: () async {
mClient.resetInitStatus();
mClient.refreshFriendsListWithErrorHandler();
},
),
2023-05-05 10:39:40 -04:00
),
2023-06-03 11:17:54 -04:00
],
);
} else {
var friends = List.from(mClient.cachedFriends); // Explicit copy.
if (_searchFilter.isNotEmpty) {
friends = friends
.where((element) => element.username.toLowerCase().contains(_searchFilter.toLowerCase()))
.toList();
friends.sort((a, b) => a.username.length.compareTo(b.username.length));
}
2023-06-03 11:17:54 -04:00
return ListView.builder(
physics: const BouncingScrollPhysics(decelerationRate: ScrollDecelerationRate.fast),
itemCount: friends.length,
itemBuilder: (context, index) {
final friend = friends[index];
final unreads = mClient.getUnreadsForFriend(friend);
return FriendListTile(
friend: friend,
unreads: unreads.length,
2023-05-30 09:09:38 -04:00
);
},
2023-06-03 11:17:54 -04:00
);
}
2023-06-03 11:17:54 -04:00
}),
Align(
alignment: Alignment.bottomCenter,
child: ExpandingInputFab(
onInputChanged: (String text) {
2023-04-30 09:43:59 -04:00
setState(() {
2023-06-03 11:17:54 -04:00
_searchFilter = text;
2023-04-30 09:43:59 -04:00
});
2023-06-03 11:17:54 -04:00
},
onExpansionChanged: (expanded) {
if (!expanded) {
setState(() {
_searchFilter = "";
});
}
},
),
2023-04-30 09:43:59 -04:00
),
2023-06-03 11:17:54 -04:00
],
),
2023-04-29 13:18:46 -04:00
);
}
2023-06-03 11:17:54 -04:00
@override
bool get wantKeepAlive => true;
2023-05-30 09:09:38 -04:00
}