Quick access buttons

This commit is contained in:
Yegor Vialov 2020-05-18 22:06:10 +00:00
parent 302451e118
commit da4264a409

View File

@ -450,8 +450,10 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>(); final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
Widget _buildScaffoldBody(bool empty) { Widget _buildScaffoldBody(bool empty) {
List<PopupMenuItem<String>> serviceMenuItems = []; List<Entity> activePlayers = [];
List<PopupMenuItem<String>> mediaMenuItems = []; List<Entity> activeLights = [];
Color mediaMenuIconColor;
Color lightMenuIconColor;
int currentViewCount = HomeAssistant().ui?.views?.length ?? 0; int currentViewCount = HomeAssistant().ui?.views?.length ?? 0;
if (_previousViewCount != currentViewCount) { if (_previousViewCount != currentViewCount) {
@ -460,72 +462,22 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
_previousViewCount = currentViewCount; _previousViewCount = currentViewCount;
} }
serviceMenuItems.add(PopupMenuItem<String>( _showLoginButton = !AppSettings().isAuthenticated;
child: new Text("Reload"),
value: "reload",
));
if (AppSettings().isAuthenticated) {
_showLoginButton = false;
serviceMenuItems.add(
PopupMenuItem<String>(
child: new Text("Logout"),
value: "logout",
));
}
Widget mediaMenuIcon;
int playersCount = 0;
if (!empty && !HomeAssistant().entities.isEmpty) { if (!empty && !HomeAssistant().entities.isEmpty) {
List<Entity> activePlayers = HomeAssistant().entities.getByDomains(includeDomains: ["media_player"], stateFiler: [EntityState.paused, EntityState.playing, EntityState.idle]); activePlayers = HomeAssistant().entities.getByDomains(includeDomains: ["media_player"], stateFiler: [EntityState.paused, EntityState.playing, EntityState.idle]);
playersCount = activePlayers.length; activeLights = HomeAssistant().entities.getByDomains(includeDomains: ["light"], stateFiler: [EntityState.on]);
mediaMenuItems.addAll(
activePlayers.map((entity) => PopupMenuItem<String>(
child: Text(
"${entity.displayName}",
style: Theme.of(context).textTheme.body1.copyWith(
color: HAClientTheme().getColorByEntityState(entity.state, context)
)
),
value: "${entity.entityId}",
)).toList()
);
} }
mediaMenuItems.addAll([
PopupMenuItem<String>( if (activePlayers.isNotEmpty) {
child: new Text("Play media..."), mediaMenuIconColor = Theme.of(context).accentColor;
value: "play_media",
)
]);
if (playersCount > 0) {
mediaMenuIcon = Stack(
overflow: Overflow.visible,
children: <Widget>[
Icon(MaterialDesignIcons.getIconDataFromIconName(
"mdi:television"), color: Colors.white,),
Positioned(
bottom: -4,
right: -4,
child: Container(
height: 16,
width: 16,
decoration: new BoxDecoration(
color: Colors.orange,
shape: BoxShape.circle,
),
child: Center(
child: Text(
"$playersCount",
style: Theme.of(context).textTheme.caption.copyWith(
color: Colors.white
)
),
),
),
)
],
);
} else { } else {
mediaMenuIcon = Icon(MaterialDesignIcons.getIconDataFromIconName( mediaMenuIconColor = Theme.of(context).primaryIconTheme.color;
"mdi:television"), color: Colors.white,); }
if (activeLights.isNotEmpty) {
lightMenuIconColor = Theme.of(context).accentColor;
} else {
lightMenuIconColor = Theme.of(context).primaryIconTheme.color;
} }
Widget mainScrollBody; Widget mainScrollBody;
if (empty) { if (empty) {
@ -580,43 +532,30 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
primary: true, primary: true,
title: Text(HomeAssistant().locationName ?? ""), title: Text(HomeAssistant().locationName ?? ""),
actions: <Widget>[ actions: <Widget>[
IconButton( PopupMenuButton(
icon: mediaMenuIcon, child: Padding(
onPressed: () { padding: EdgeInsets.symmetric(horizontal: 15),
showMenu( child: Icon(MaterialDesignIcons.getIconDataFromIconName(
position: RelativeRect.fromLTRB(MediaQuery.of(context).size.width, MediaQuery.of(context).padding.top + Sizes.iconSize + 20, 50, 0), "mdi:dots-vertical"), color: Theme.of(context).primaryIconTheme.color)
context: context, ),
items: mediaMenuItems itemBuilder: (BuildContext context) {
).then((String val) { List<PopupMenuEntry<String>> result = [
if (val == "play_media") { PopupMenuItem<String>(
Navigator.pushNamed(context, "/play-media", arguments: {"url": ""}); child: new Text("Reload"),
} else if (val != null) { value: "reload",
_showEntityPage(val); )
} ];
}); if (AppSettings().isAuthenticated) {
result.addAll([
PopupMenuDivider(),
PopupMenuItem<String>(
child: new Text("Logout"),
value: "logout",
)]);
} }
return result;
},
), ),
IconButton(
icon: Icon(MaterialDesignIcons.getIconDataFromIconName(
"mdi:dots-vertical"), color: Colors.white,),
onPressed: () {
showMenu(
position: RelativeRect.fromLTRB(MediaQuery.of(context).size.width, MediaQuery.of(context).padding.top + Sizes.iconSize + 20, 0.0, 0.0),
context: context,
items: serviceMenuItems
).then((String val) {
HomeAssistant().currentDashboardPath = HomeAssistant.DEFAULT_DASHBOARD;
if (val == "reload") {
_quickLoad();
} else if (val == "logout") {
HomeAssistant().logout().then((_) {
_quickLoad();
});
}
});
}
)
], ],
leading: IconButton( leading: IconButton(
icon: Icon(Icons.menu), icon: Icon(Icons.menu),
@ -631,19 +570,87 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
child: Row( child: Row(
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: <Widget>[ children: <Widget>[
IconButton( PopupMenuButton<String>(
icon: Icon(Icons.add), child: Padding(
color: Colors.white, padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
onPressed: () { child: Icon(
Logger.d('First'); MaterialDesignIcons.getIconDataFromIconName("mdi:television"),
color: mediaMenuIconColor,
size: 20,
)
),
onSelected: (String val) {
if (val == "play_media") {
Navigator.pushNamed(context, "/play-media", arguments: {"url": ""});
} else if (val != null) {
_showEntityPage(val);
}
}, },
itemBuilder: (BuildContext context) {
List<PopupMenuEntry<String>> result = [
PopupMenuDivider(),
PopupMenuItem<String>(
child: new Text("Play media..."),
value: "play_media",
)
];
if (activePlayers.isNotEmpty) {
result.insertAll(0,
activePlayers.map((entity) => PopupMenuItem<String>(
child: Text(
"${entity.displayName}",
style: Theme.of(context).textTheme.body1.copyWith(
color: HAClientTheme().getColorByEntityState(entity.state, context)
)
),
value: "${entity.entityId}",
)).toList()
);
} else {
result.insert(0, PopupMenuItem<String>(
child: new Text("No active players"),
value: "_",
enabled: false,
));
}
return result;
}
), ),
IconButton( PopupMenuButton<String>(
icon: Icon(Icons.add), child: Padding(
color: Colors.white, padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
onPressed: () { child: Icon(
Logger.d('First'); MaterialDesignIcons.getIconDataFromIconName("mdi:lightbulb-outline"),
color: lightMenuIconColor,
size: 20
)
),
onSelected: (String val) {
if (val == 'turn_off_all') {
ConnectionManager().callService(
service: 'turn_off',
domain: 'light',
entityId: 'all'
);
} else if (val == 'turn_on_all') {
ConnectionManager().callService(
service: 'turn_on',
domain: 'light',
entityId: 'all'
);
}
}, },
itemBuilder: (BuildContext context) => <PopupMenuEntry<String>>[
PopupMenuItem<String>(
child: new Text("Turn on all lights"),
value: "turn_on_all",
),
PopupMenuItem<String>(
child: new Text("Turn off all ligts"),
value: "turn_off_all",
enabled: activeLights.isNotEmpty,
)
],
) )
], ],
) ), ) ),