Notification stacks on the mobile

This commit is contained in:
2026-01-16 00:08:56 +08:00
parent 269c17d068
commit 8ad31dad58
2 changed files with 89 additions and 44 deletions

View File

@@ -12,6 +12,8 @@ class NotificationItemWidget extends HookConsumerWidget {
final NotificationItem item; final NotificationItem item;
final VoidCallback onDismiss; final VoidCallback onDismiss;
final bool isDesktop; final bool isDesktop;
final int index;
final int totalNotifications;
final Animation<double> progress; final Animation<double> progress;
const NotificationItemWidget({ const NotificationItemWidget({
@@ -19,6 +21,8 @@ class NotificationItemWidget extends HookConsumerWidget {
required this.item, required this.item,
required this.onDismiss, required this.onDismiss,
required this.isDesktop, required this.isDesktop,
required this.index,
required this.totalNotifications,
required this.progress, required this.progress,
}); });
@@ -48,7 +52,7 @@ class NotificationItemWidget extends HookConsumerWidget {
} }
: null, : null,
child: Card( child: Card(
elevation: 4, elevation: (index == 0 && !isDesktop && totalNotifications > 1) ? 8 : 4,
margin: EdgeInsets.zero, margin: EdgeInsets.zero,
color: Theme.of(context).colorScheme.surfaceContainerHigh, color: Theme.of(context).colorScheme.surfaceContainerHigh,
shape: const RoundedRectangleBorder( shape: const RoundedRectangleBorder(

View File

@@ -29,52 +29,96 @@ class NotificationOverlay extends HookConsumerWidget {
return const SizedBox.shrink(); return const SizedBox.shrink();
} }
final itemWidth = isDesktop ? 420.0 : MediaQuery.sizeOf(context).width - 40; final itemWidth = isDesktop ? 420.0 : MediaQuery.sizeOf(context).width;
if (isDesktop) {
return Positioned(
top: topOffset,
left: 0,
right: 0,
child: Material(
color: Colors.transparent,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisSize: MainAxisSize.min,
children: notifications.asMap().entries.map((entry) {
final index = entry.key;
final item = entry.value;
return AnimatedNotificationItem(
key: Key(item.id),
item: item,
isDesktop: true,
index: index,
totalNotifications: notifications.length,
onDismiss: () {
ref.read(notificationStateProvider.notifier).remove(item.id);
},
);
}).toList(),
),
).width(itemWidth).alignment(Alignment.topRight),
);
} else {
// Non-desktop: use Stack with overlapping
const double notificationHeight = 80.0;
const double overlap = 20.0;
final stackHeight =
notificationHeight + (notifications.length - 1) * overlap;
return Positioned( return Positioned(
top: topOffset, top: topOffset,
left: 0, left: 0,
right: 0, right: 0,
child: child: Material(
Material(
color: Colors.transparent, color: Colors.transparent,
child: Column( child: SizedBox(
mainAxisAlignment: MainAxisAlignment.start, height: stackHeight,
crossAxisAlignment: isDesktop child: Stack(
? CrossAxisAlignment.end alignment: Alignment.topCenter,
: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: notifications.asMap().entries.map((entry) { children: notifications.asMap().entries.map((entry) {
final index = entry.key;
final item = entry.value; final item = entry.value;
return AnimatedNotificationItem( return Positioned(
top: index * overlap,
left: 16,
right: 16,
child: AnimatedNotificationItem(
key: Key(item.id), key: Key(item.id),
item: item, item: item,
isDesktop: isDesktop, isDesktop: false,
index: index,
totalNotifications: notifications.length,
onDismiss: () { onDismiss: () {
ref ref
.read(notificationStateProvider.notifier) .read(notificationStateProvider.notifier)
.remove(item.id); .remove(item.id);
}, },
).clipRRect(all: 8),
); );
}).toList(), }).toList(),
), ),
) ),
.width(itemWidth) ).width(itemWidth).alignment(Alignment.topCenter),
.alignment(isDesktop ? Alignment.topRight : Alignment.topCenter),
); );
} }
}
} }
class AnimatedNotificationItem extends HookConsumerWidget { class AnimatedNotificationItem extends HookConsumerWidget {
final NotificationItem item; final NotificationItem item;
final VoidCallback onDismiss; final VoidCallback onDismiss;
final bool isDesktop; final bool isDesktop;
final int index;
final int totalNotifications;
const AnimatedNotificationItem({ const AnimatedNotificationItem({
super.key, super.key,
required this.item, required this.item,
required this.onDismiss, required this.onDismiss,
required this.isDesktop, required this.isDesktop,
required this.index,
required this.totalNotifications,
}); });
@override @override
@@ -124,18 +168,15 @@ class AnimatedNotificationItem extends HookConsumerWidget {
child: SizeTransition( child: SizeTransition(
sizeFactor: curvedAnimation, sizeFactor: curvedAnimation,
axis: Axis.vertical, axis: Axis.vertical,
child: Padding(
padding: isDesktop
? const EdgeInsets.only(bottom: 12, right: 16)
: const EdgeInsets.only(bottom: 12, left: 16, right: 16),
child: NotificationItemWidget( child: NotificationItemWidget(
item: item, item: item,
isDesktop: isDesktop, isDesktop: isDesktop,
index: index,
totalNotifications: totalNotifications,
onDismiss: () {}, onDismiss: () {},
progress: progressAnimation, progress: progressAnimation,
), ),
), ),
),
); );
} }
} }