veilidchat/lib/notifications/views/notifications_widget.dart
2025-03-13 21:34:12 -04:00

94 lines
3.1 KiB
Dart

import 'package:awesome_extensions/awesome_extensions.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:toastification/toastification.dart';
import '../../theme/theme.dart';
import '../notifications.dart';
class NotificationsWidget extends StatelessWidget {
const NotificationsWidget({required Widget child, super.key})
: _child = child;
////////////////////////////////////////////////////////////////////////////
// Public API
@override
Widget build(BuildContext context) {
final notificationsCubit = context.read<NotificationsCubit>();
return BlocListener<NotificationsCubit, NotificationsState>(
bloc: notificationsCubit,
listener: (context, state) {
if (state.queue.isNotEmpty) {
final queue = notificationsCubit.popAll();
for (final notificationItem in queue) {
switch (notificationItem.type) {
case NotificationType.info:
_info(
context: context,
text: notificationItem.text,
title: notificationItem.title);
case NotificationType.error:
_error(
context: context,
text: notificationItem.text,
title: notificationItem.title);
}
}
}
},
child: _child);
}
////////////////////////////////////////////////////////////////////////////
// Private Implementation
void _toast(
{required BuildContext context,
required String text,
required ScaleToastTheme toastTheme,
String? title}) {
toastification.show(
context: context,
title: title != null
? Text(title)
.copyWith(style: toastTheme.titleTextStyle)
.paddingLTRB(0, 0, 0, 8)
: null,
description: Text(text).copyWith(style: toastTheme.descriptionTextStyle),
icon: toastTheme.icon,
primaryColor: toastTheme.primaryColor,
backgroundColor: toastTheme.backgroundColor,
foregroundColor: toastTheme.foregroundColor,
padding: toastTheme.padding,
borderRadius: toastTheme.borderRadius,
borderSide: toastTheme.borderSide,
autoCloseDuration: const Duration(seconds: 2),
animationDuration: const Duration(milliseconds: 500),
);
}
void _info(
{required BuildContext context, required String text, String? title}) {
final theme = Theme.of(context);
final toastTheme =
theme.extension<ScaleTheme>()!.toastTheme(ScaleToastKind.info);
_toast(context: context, text: text, toastTheme: toastTheme, title: title);
}
void _error(
{required BuildContext context, required String text, String? title}) {
final theme = Theme.of(context);
final toastTheme =
theme.extension<ScaleTheme>()!.toastTheme(ScaleToastKind.error);
_toast(context: context, text: text, toastTheme: toastTheme, title: title);
}
////////////////////////////////////////////////////////////////////////////
final Widget _child;
}