2019-07-01 09:51:23 +00:00
|
|
|
import 'package:comunic/utils/intl_utils.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
/// Navigation bar widget
|
|
|
|
///
|
|
|
|
/// @author Pierre HUBERT
|
|
|
|
|
|
|
|
typedef OnSelectMenuAction = void Function(BarCallbackActions);
|
|
|
|
|
|
|
|
/// Callback actions
|
|
|
|
enum BarCallbackActions {
|
|
|
|
OPEN_CONVERSATIONS,
|
|
|
|
OPEN_NEWEST_POSTS,
|
|
|
|
OPEN_FRIENDS,
|
2019-07-01 10:06:14 +00:00
|
|
|
OPEN_MY_PAGE,
|
2019-11-01 08:59:22 +00:00
|
|
|
OPEN_APP_SETTINGS,
|
2019-07-01 09:51:23 +00:00
|
|
|
NONE,
|
|
|
|
ACTION_LOGOUT
|
|
|
|
}
|
|
|
|
|
|
|
|
const _PrimaryColor = Colors.blue;
|
|
|
|
const _SecondaryColor = Colors.white;
|
|
|
|
|
|
|
|
/// Menu item information
|
|
|
|
class _MenuItem {
|
|
|
|
final String label;
|
|
|
|
final Widget icon;
|
|
|
|
final BarCallbackActions action;
|
|
|
|
final bool isMenu;
|
|
|
|
|
2019-11-01 08:59:22 +00:00
|
|
|
const _MenuItem({
|
|
|
|
@required this.label,
|
|
|
|
@required this.icon,
|
|
|
|
@required this.action,
|
|
|
|
this.isMenu = false,
|
|
|
|
}) : assert(label != null),
|
2019-07-01 09:51:23 +00:00
|
|
|
assert(icon != null || isMenu),
|
|
|
|
assert(action != null),
|
|
|
|
assert(isMenu != null);
|
|
|
|
}
|
|
|
|
|
2019-07-01 10:06:14 +00:00
|
|
|
/// Item of action menu
|
|
|
|
class _ActionMenuItem {
|
|
|
|
final String label;
|
|
|
|
final BarCallbackActions action;
|
|
|
|
|
|
|
|
const _ActionMenuItem({@required this.label, @required this.action})
|
|
|
|
: assert(label != null),
|
|
|
|
assert(action != null);
|
|
|
|
}
|
|
|
|
|
2019-07-01 09:51:23 +00:00
|
|
|
/// List of menu items to show
|
|
|
|
final _menuItems = <_MenuItem>[
|
|
|
|
_MenuItem(
|
|
|
|
label: tr("Conversations"),
|
|
|
|
icon: Icon(Icons.comment),
|
|
|
|
action: BarCallbackActions.OPEN_CONVERSATIONS),
|
|
|
|
_MenuItem(
|
|
|
|
label: tr("Newest"),
|
|
|
|
icon: Icon(Icons.refresh),
|
|
|
|
action: BarCallbackActions.OPEN_NEWEST_POSTS),
|
|
|
|
_MenuItem(
|
|
|
|
label: tr("Friends"),
|
|
|
|
icon: Icon(Icons.group),
|
|
|
|
action: BarCallbackActions.OPEN_FRIENDS),
|
|
|
|
_MenuItem(
|
|
|
|
label: tr("Menu"),
|
|
|
|
icon: Icon(Icons.more_vert),
|
|
|
|
isMenu: true,
|
|
|
|
action: BarCallbackActions.NONE)
|
|
|
|
];
|
|
|
|
|
2019-07-01 10:06:14 +00:00
|
|
|
/// List of menu actions items
|
|
|
|
final _menuActionsItem = <_ActionMenuItem>[
|
|
|
|
_ActionMenuItem(
|
|
|
|
label: tr("My Page"), action: BarCallbackActions.OPEN_MY_PAGE),
|
2019-11-01 08:59:22 +00:00
|
|
|
_ActionMenuItem(
|
|
|
|
label: tr("App settings"), action: BarCallbackActions.OPEN_APP_SETTINGS),
|
2019-07-01 10:06:14 +00:00
|
|
|
_ActionMenuItem(
|
|
|
|
label: tr("Sign out"), action: BarCallbackActions.ACTION_LOGOUT),
|
|
|
|
];
|
|
|
|
|
2019-07-01 09:51:23 +00:00
|
|
|
/// Public widget
|
|
|
|
class ComunicAppBar extends StatelessWidget implements PreferredSizeWidget {
|
|
|
|
final OnSelectMenuAction onTap;
|
|
|
|
final BarCallbackActions selectedAction;
|
|
|
|
|
|
|
|
const ComunicAppBar(
|
|
|
|
{Key key, @required this.onTap, @required this.selectedAction})
|
|
|
|
: assert(onTap != null),
|
|
|
|
super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Material(
|
|
|
|
color: Colors.blue,
|
|
|
|
child: Row(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
|
|
children: List.generate(
|
|
|
|
_menuItems.length,
|
|
|
|
(i) => _MenuItemWidget(
|
2019-11-01 08:59:22 +00:00
|
|
|
item: _menuItems[i],
|
|
|
|
onTap: onTap,
|
|
|
|
isSelected: _menuItems[i].action == selectedAction,
|
|
|
|
),
|
2019-07-01 09:51:23 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Size get preferredSize => Size.fromHeight(35);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The [Widget] part of a menu item
|
|
|
|
class _MenuItemWidget extends StatelessWidget {
|
|
|
|
final _MenuItem item;
|
|
|
|
final OnSelectMenuAction onTap;
|
|
|
|
final bool isSelected;
|
|
|
|
|
|
|
|
const _MenuItemWidget(
|
|
|
|
{Key key,
|
|
|
|
@required this.item,
|
|
|
|
@required this.onTap,
|
|
|
|
@required this.isSelected})
|
|
|
|
: assert(item != null),
|
|
|
|
assert(onTap != null),
|
|
|
|
assert(isSelected != null),
|
|
|
|
super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
assert(debugCheckHasMaterial(context));
|
|
|
|
|
|
|
|
return Expanded(
|
|
|
|
child: Material(
|
|
|
|
color: isSelected ? _SecondaryColor : _PrimaryColor,
|
|
|
|
child: !item.isMenu
|
|
|
|
? InkWell(
|
|
|
|
child: _buildIconContainer(),
|
|
|
|
onTap: () => onTap(item.action),
|
|
|
|
)
|
|
|
|
: _buildContextMenuPopupButton(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildIconContainer() {
|
|
|
|
return Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: <Widget>[
|
|
|
|
IconTheme(
|
|
|
|
data: IconThemeData(
|
|
|
|
color: isSelected ? _PrimaryColor : _SecondaryColor),
|
|
|
|
child: item.icon,
|
|
|
|
)
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build context menu
|
|
|
|
Widget _buildContextMenuPopupButton() {
|
|
|
|
return PopupMenuButton<BarCallbackActions>(
|
|
|
|
child: _buildIconContainer(),
|
2019-07-01 10:06:14 +00:00
|
|
|
itemBuilder: (i) => _menuActionsItem
|
|
|
|
.map((f) => PopupMenuItem(
|
|
|
|
child: Text(f.label),
|
|
|
|
value: f.action,
|
|
|
|
))
|
|
|
|
.toList(),
|
2019-07-01 09:51:23 +00:00
|
|
|
onSelected: onTap,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|