forked from Nexus/nexus
make highlights work better for Notifications page
This commit is contained in:
parent
ea687471bc
commit
e21c9e8c22
2 changed files with 43 additions and 4 deletions
|
|
@ -34,7 +34,7 @@ class NotificationController
|
||||||
Navigator.of(context).push(
|
Navigator.of(context).push(
|
||||||
MaterialPageRoute(
|
MaterialPageRoute(
|
||||||
builder: (_) => NotificationsPage(
|
builder: (_) => NotificationsPage(
|
||||||
eventId: eventId,
|
highlightedEventId: eventId,
|
||||||
defaultToAllNotifications: true,
|
defaultToAllNotifications: true,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
import "dart:async";
|
||||||
|
|
||||||
import "package:m3e_buttons/m3e_buttons.dart";
|
import "package:m3e_buttons/m3e_buttons.dart";
|
||||||
import "package:material_ui/material_ui.dart";
|
import "package:material_ui/material_ui.dart";
|
||||||
import "package:flutter_hooks/flutter_hooks.dart";
|
import "package:flutter_hooks/flutter_hooks.dart";
|
||||||
|
|
@ -12,7 +14,7 @@ import "package:nexus/widgets/renderers/event.dart";
|
||||||
import "package:super_sliver_list/super_sliver_list.dart";
|
import "package:super_sliver_list/super_sliver_list.dart";
|
||||||
|
|
||||||
class const NotificationsPage({
|
class const NotificationsPage({
|
||||||
final String? eventId,
|
final String? highlightedEventId,
|
||||||
final bool defaultToAllNotifications = false,
|
final bool defaultToAllNotifications = false,
|
||||||
super.key,
|
super.key,
|
||||||
}) extends HookConsumerWidget {
|
}) extends HookConsumerWidget {
|
||||||
|
|
@ -30,6 +32,10 @@ class const NotificationsPage({
|
||||||
};
|
};
|
||||||
final unreadTypeIndex = useState(defaultToAllNotifications ? 1 : 0);
|
final unreadTypeIndex = useState(defaultToAllNotifications ? 1 : 0);
|
||||||
|
|
||||||
|
final highlightedId = useState(highlightedEventId);
|
||||||
|
final listController = useRef(ListController());
|
||||||
|
final scrollController = useScrollController();
|
||||||
|
|
||||||
final provider = NotificationsController.provider((
|
final provider = NotificationsController.provider((
|
||||||
options.values.toList()[unreadTypeIndex.value],
|
options.values.toList()[unreadTypeIndex.value],
|
||||||
null,
|
null,
|
||||||
|
|
@ -37,7 +43,38 @@ class const NotificationsPage({
|
||||||
final notifications = ref.watch(provider);
|
final notifications = ref.watch(provider);
|
||||||
final notifier = ref.watch(provider.notifier);
|
final notifier = ref.watch(provider.notifier);
|
||||||
|
|
||||||
final scrollController = useScrollController();
|
useEffect(() {
|
||||||
|
if (highlightedId.value == null) return null;
|
||||||
|
Timer? timer;
|
||||||
|
|
||||||
|
void listener() =>
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
||||||
|
if (!listController.value.isAttached) return;
|
||||||
|
|
||||||
|
final notifications = await ref.watch(provider.future);
|
||||||
|
|
||||||
|
final index = notifications.indexWhere(
|
||||||
|
(element) => element.eventId == highlightedId.value!,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (index == -1) return;
|
||||||
|
|
||||||
|
listController.value.animateToItem(
|
||||||
|
index: index,
|
||||||
|
scrollController: scrollController,
|
||||||
|
alignment: 0.5,
|
||||||
|
duration: (_) => .new(milliseconds: 700),
|
||||||
|
curve: (_) => Curves.easeInOut,
|
||||||
|
);
|
||||||
|
timer = Timer(.new(seconds: 1), () {
|
||||||
|
highlightedId.value = null;
|
||||||
|
});
|
||||||
|
listController.value.removeListener(listener);
|
||||||
|
});
|
||||||
|
|
||||||
|
listController.value.addListener(listener);
|
||||||
|
return timer?.cancel;
|
||||||
|
}, []);
|
||||||
|
|
||||||
useEffect(() {
|
useEffect(() {
|
||||||
Future<void> listener() async {
|
Future<void> listener() async {
|
||||||
|
|
@ -72,6 +109,7 @@ class const NotificationsPage({
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
: SuperListView.builder(
|
: SuperListView.builder(
|
||||||
|
listController: listController.value,
|
||||||
controller: scrollController,
|
controller: scrollController,
|
||||||
itemCount: value.length,
|
itemCount: value.length,
|
||||||
padding: const EdgeInsets.symmetric(
|
padding: const EdgeInsets.symmetric(
|
||||||
|
|
@ -81,7 +119,8 @@ class const NotificationsPage({
|
||||||
reverse: true,
|
reverse: true,
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
final event = value[index];
|
final event = value[index];
|
||||||
final isHighlighted = event.eventId == eventId;
|
final isHighlighted =
|
||||||
|
event.eventId == highlightedId.value;
|
||||||
|
|
||||||
return Padding(
|
return Padding(
|
||||||
padding: .only(top: 8),
|
padding: .only(top: 8),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue