WIP: bottom info bar as separate component
This commit is contained in:
		
							
								
								
									
										218
									
								
								lib/pages/widgets/bottom_info_bar.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										218
									
								
								lib/pages/widgets/bottom_info_bar.dart
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,218 @@ | ||||
| part of '../../main.dart'; | ||||
|  | ||||
| class BottomInfoBarController { | ||||
|  | ||||
|   Function show; | ||||
|   Function hide; | ||||
|  | ||||
|   String bottomBarText; | ||||
|   bool bottomBarProgress; | ||||
|   bool bottomBarErrorColor; | ||||
|   Timer _bottomBarTimer; | ||||
|   bool initialState = false; | ||||
|  | ||||
|   List<HAErrorAction> actions = []; | ||||
|  | ||||
|   void hideBottomBar() { | ||||
|     //_scaffoldKey?.currentState?.hideCurrentSnackBar(); | ||||
|     _bottomBarTimer?.cancel(); | ||||
|     if (hide == null) { | ||||
|       initialState = false; | ||||
|     } else { | ||||
|       hide(); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   void showInfoBottomBar({String message, bool progress: false, Duration duration}) { | ||||
|     _bottomBarTimer?.cancel(); | ||||
|     actions.clear(); | ||||
|     bottomBarErrorColor = false; | ||||
|     bottomBarText = message; | ||||
|     bottomBarProgress = progress; | ||||
|     if (show == null) { | ||||
|       initialState = true; | ||||
|     } else { | ||||
|       Logger.d("Calling  show() - ${bottomBarProgress}"); | ||||
|       show(); | ||||
|     } | ||||
|     if (duration != null) { | ||||
|       _bottomBarTimer = Timer(duration, () { | ||||
|         hideBottomBar(); | ||||
|       }); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   void showErrorBottomBar(HAError error) { | ||||
|     actions.clear(); | ||||
|     actions.addAll(error.actions); | ||||
|     bottomBarErrorColor = true; | ||||
|     bottomBarProgress = false; | ||||
|     bottomBarText = "${error.message}"; | ||||
|     if (show == null) { | ||||
|       initialState = true; | ||||
|     } else { | ||||
|       show(); | ||||
|     } | ||||
|   } | ||||
|  | ||||
| } | ||||
|  | ||||
| class BottomInfoBar extends StatefulWidget { | ||||
|  | ||||
|   final BottomInfoBarController controller; | ||||
|  | ||||
|   const BottomInfoBar({Key key, this.controller}) : super(key: key); | ||||
|    | ||||
|   @override | ||||
|   State<StatefulWidget> createState() { | ||||
|     return new _BottomInfoBarState(); | ||||
|   } | ||||
|  | ||||
| } | ||||
|  | ||||
| class _BottomInfoBarState extends State<BottomInfoBar> { | ||||
|  | ||||
|   bool _show; | ||||
|  | ||||
|   @override | ||||
|   void initState() { | ||||
|     _show = widget.controller.initialState; | ||||
|     widget.controller.show = () { | ||||
|       Logger.d('Set state in show() - ${widget.controller.bottomBarProgress}'); | ||||
|       setState(() { | ||||
|         _show = true; | ||||
|       }); | ||||
|     }; | ||||
|     widget.controller.hide = () { | ||||
|       setState(() { | ||||
|         _show = false; | ||||
|       }); | ||||
|     }; | ||||
|     super.initState(); | ||||
|   } | ||||
|  | ||||
|  | ||||
|   @override | ||||
|   Widget build(BuildContext context) { | ||||
|     if (!_show) { | ||||
|       return Container(width: 0, height: 0,); | ||||
|     } else { | ||||
|       Widget bottomBar; | ||||
|       List<Widget> bottomBarChildren = []; | ||||
|       Widget actionsWidget; | ||||
|       TextStyle textStyle = Theme.of(context).textTheme.button.copyWith( | ||||
|         decoration: TextDecoration.underline | ||||
|       ); | ||||
|       List<Widget> actions = []; | ||||
|       widget.controller.actions.forEach((HAErrorAction action) { | ||||
|         switch (action.type) { | ||||
|           case HAErrorActionType.FULL_RELOAD: { | ||||
|             actions.add(FlatButton( | ||||
|               child: Text("${action.title}", style: textStyle), | ||||
|               onPressed: () { | ||||
|                 Logger.d('FULL_RELOAD action'); | ||||
|                 EventBus().fire(FullReloadEvent()); | ||||
|               }, | ||||
|             )); | ||||
|             break; | ||||
|           } | ||||
|  | ||||
|           case HAErrorActionType.QUICK_RELOAD: { | ||||
|             actions.add(FlatButton( | ||||
|               child: Text("${action.title}", style: textStyle), | ||||
|               onPressed: () { | ||||
|                 EventBus().fire(ReloadUIEvent()); | ||||
|               }, | ||||
|             )); | ||||
|             break; | ||||
|           } | ||||
|  | ||||
|           case HAErrorActionType.RELOGIN: { | ||||
|             actions.add(FlatButton( | ||||
|               child: Text("${action.title}", style: textStyle), | ||||
|               onPressed: () { | ||||
|                 ConnectionManager().logout().then((_) => EventBus().fire(FullReloadEvent())); | ||||
|               }, | ||||
|             )); | ||||
|             break; | ||||
|           } | ||||
|  | ||||
|           case HAErrorActionType.URL: { | ||||
|             actions.add(FlatButton( | ||||
|               child: Text("${action.title}", style: textStyle), | ||||
|               onPressed: () { | ||||
|                 Launcher.launchURLInCustomTab(context: context, url: "${action.url}"); | ||||
|               }, | ||||
|             )); | ||||
|             break; | ||||
|           } | ||||
|  | ||||
|           case HAErrorActionType.OPEN_CONNECTION_SETTINGS: { | ||||
|             actions.add(FlatButton( | ||||
|               child: Text("${action.title}", style: textStyle), | ||||
|               onPressed: () { | ||||
|                 Navigator.pushNamed(context, '/connection-settings'); | ||||
|               }, | ||||
|             )); | ||||
|             break; | ||||
|           } | ||||
|         } | ||||
|       }); | ||||
|       if (actions.isNotEmpty) { | ||||
|         actionsWidget = Row( | ||||
|           mainAxisSize: MainAxisSize.min, | ||||
|           children: actions, | ||||
|           mainAxisAlignment: MainAxisAlignment.end, | ||||
|         ); | ||||
|       } else { | ||||
|         actionsWidget = Container(height: 0.0, width: 0.0,); | ||||
|       } | ||||
|  | ||||
|       if (widget.controller.bottomBarText != null) { | ||||
|         bottomBarChildren.add( | ||||
|             Padding( | ||||
|               padding: EdgeInsets.fromLTRB( | ||||
|                   Sizes.leftWidgetPadding, Sizes.rowPadding, 0.0, | ||||
|                   Sizes.rowPadding), | ||||
|               child: Text( | ||||
|                 "${widget.controller.bottomBarText}", | ||||
|                 textAlign: TextAlign.left, | ||||
|                 softWrap: true, | ||||
|               ), | ||||
|             ) | ||||
|  | ||||
|         ); | ||||
|       } | ||||
|       if (widget.controller.bottomBarProgress) { | ||||
|         bottomBarChildren.add( | ||||
|           CollectionScaleTransition( | ||||
|             children: <Widget>[ | ||||
|               Icon(Icons.stop, size: 10.0, color: HAClientTheme().getOnStateColor(context),), | ||||
|               Icon(Icons.stop, size: 10.0, color: HAClientTheme().getDisabledStateColor(context),), | ||||
|               Icon(Icons.stop, size: 10.0, color: HAClientTheme().getOffStateColor(context),), | ||||
|             ], | ||||
|           ), | ||||
|         ); | ||||
|       } | ||||
|       if (bottomBarChildren.isNotEmpty) { | ||||
|         bottomBar = Container( | ||||
|           color: widget.controller.bottomBarErrorColor ? Theme.of(context).errorColor : Theme.of(context).primaryColorLight, | ||||
|           child: Row( | ||||
|             mainAxisSize: MainAxisSize.max, | ||||
|             children: <Widget>[ | ||||
|               Expanded( | ||||
|                 child: Column( | ||||
|                   crossAxisAlignment: widget.controller.bottomBarProgress ? CrossAxisAlignment.center : CrossAxisAlignment.start, | ||||
|                   mainAxisSize: MainAxisSize.min, | ||||
|                   children: bottomBarChildren, | ||||
|                 ), | ||||
|               ), | ||||
|               actionsWidget | ||||
|             ], | ||||
|           ), | ||||
|         ); | ||||
|       } | ||||
|       return bottomBar; | ||||
|     } | ||||
|   }  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user