mirror of
https://gitlab.com/comunic/comunicmobile
synced 2025-06-19 16:25:17 +00:00
Created custom appbar
This commit is contained in:
152
lib/ui/widgets/navbar_widget.dart
Normal file
152
lib/ui/widgets/navbar_widget.dart
Normal file
@ -0,0 +1,152 @@
|
||||
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,
|
||||
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;
|
||||
|
||||
const _MenuItem(
|
||||
{@required this.label,
|
||||
@required this.icon,
|
||||
@required this.action,
|
||||
this.isMenu = false})
|
||||
: assert(label != null),
|
||||
assert(icon != null || isMenu),
|
||||
assert(action != null),
|
||||
assert(isMenu != null);
|
||||
}
|
||||
|
||||
/// 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)
|
||||
];
|
||||
|
||||
/// 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(
|
||||
item: _menuItems[i],
|
||||
onTap: onTap,
|
||||
isSelected: _menuItems[i].action == selectedAction,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@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(),
|
||||
itemBuilder: (i) => [
|
||||
PopupMenuItem(
|
||||
child: Text(tr("Logout")),
|
||||
value: BarCallbackActions.ACTION_LOGOUT,
|
||||
)
|
||||
],
|
||||
onSelected: onTap,
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user