import 'package:comunic/helpers/events_helper.dart'; import 'package:comunic/helpers/notifications_helper.dart'; import 'package:comunic/models/count_unread_notifications.dart'; import 'package:comunic/ui/screens/notifications_screen.dart'; import 'package:comunic/ui/widgets/safe_state.dart'; import 'package:comunic/ui/widgets/tablet_mode/appbar_custom_dropdown_widget.dart'; import 'package:flutter/material.dart'; /// Comunic tablet AppBar widget /// /// @author Pierre Hubert class ComunicTabletAppBarWidget extends StatefulWidget implements PreferredSizeWidget { @override _ComunicTabletAppBarWidgetState createState() => _ComunicTabletAppBarWidgetState(); @override Size get preferredSize => Size.fromHeight(kToolbarHeight); } class _ComunicTabletAppBarWidgetState extends SafeState { final notificationsDropdownKey = GlobalKey(); final conversationsDropdownKey = GlobalKey(); var _unreadNotifications = CountUnreadNotifications(notifications: 0, conversations: 0); void _refreshCountUnread() async { try { final count = await NotificationsHelper().countUnread(); setState(() { _unreadNotifications = count; }); } catch (e, s) { print("Could not refresh the number of unread notifications: $e\n$s"); } } @override void initState() { super.initState(); _refreshCountUnread(); // Listen to notifications number update this.listenChangeState( (d) => _unreadNotifications.notifications = d.newNum); this.listenChangeState( (d) => _unreadNotifications.conversations = d.newNum); } @override Widget build(BuildContext context) { return AppBar( title: Text("Comunic"), actions: [ AppBarCustomDropDownWidget( key: notificationsDropdownKey, icon: Icon(Icons.notifications), notificationsBadge: _unreadNotifications.notifications, onBuildOverlay: (c) => Padding( padding: const EdgeInsets.all(8.0), child: NotificationsScreen(), ), ), AppBarCustomDropDownWidget( key: conversationsDropdownKey, icon: Icon(Icons.message), notificationsBadge: _unreadNotifications.conversations, onBuildOverlay: (c) => Center( child: RaisedButton( child: Text("Close"), onPressed: () => conversationsDropdownKey.currentState.toggleOverlay(), ), ), ), PopupMenuButton(itemBuilder: (c) => []), ], ); } }