Advertisement
Guest User

Untitled

a guest
May 25th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. class DeviceItem extends StatefulWidget {
  2. DeviceItem({Key key, this.data}) : super(key: key);
  3.  
  4. final DeviceModel data;
  5.  
  6. @override
  7. _DeviceItemState createState() => _DeviceItemState();
  8. }
  9.  
  10. class _DeviceItemState extends State<DeviceItem> {
  11. RelayAnimController controller;
  12.  
  13. @override
  14. void initState() {
  15. controller = RelayAnimController(widget.data.relayState);
  16. super.initState();
  17. }
  18.  
  19. @override
  20. Widget build(BuildContext context) {
  21. return Container(
  22. height: 77.0,
  23. child: GestureDetector(
  24. onTap: () => _navigateToDeviceEdit(context),
  25. child: Column(
  26. children: <Widget>[
  27. Container(
  28. color: AppColors.divider,
  29. height: 1.0,
  30. ),
  31. Container(
  32. color: AppColors.primary,
  33. height: 76.0,
  34. child: Row(
  35. crossAxisAlignment: CrossAxisAlignment.stretch,
  36. children: <Widget>[
  37. _serialNumber,
  38. _modelAndTitle,
  39. _connectionState,
  40. _relayState
  41. ],
  42. ),
  43. ),
  44. ],
  45. ),
  46. ),
  47. );
  48. }
  49.  
  50. /// Serial number with decoration widget
  51. get _serialNumber => Stack(
  52. children: <Widget>[
  53. Container(
  54. color: AppColors.deviceItemConnector,
  55. margin: EdgeInsets.only(
  56. left: 27.0,
  57. ),
  58. width: 2.0,
  59. ),
  60. Align(
  61. alignment: Alignment.centerLeft,
  62. child: Container(
  63. margin: EdgeInsets.only(left: 16.0),
  64. height: 24.0,
  65. width: 24.0,
  66. decoration: BoxDecoration(
  67. shape: BoxShape.circle,
  68. color: AppColors.primary,
  69. border: Border.all(
  70. color: widget.data.connectionState ==
  71. DeviceConnectionState.CONNECTED
  72. ? AppColors.secondary
  73. : AppColors.textInactive,
  74. width: 2.0,
  75. style: BorderStyle.solid,
  76. ),
  77. ),
  78. child: Center(
  79. child: Text(
  80. "${widget.data.position}",
  81. style: TextStyle(color: _getColorByDeviceState(widget.data)),
  82. ),
  83. ),
  84. ),
  85. )
  86. ],
  87. );
  88.  
  89. /// Model and name text widget
  90. get _modelAndTitle => Expanded(
  91. child: Container(
  92. margin: EdgeInsets.only(left: 16.0, right: 8.0),
  93. child: Column(
  94. crossAxisAlignment: CrossAxisAlignment.start,
  95. mainAxisAlignment: MainAxisAlignment.center,
  96. children: <Widget>[
  97. Container(
  98. margin: EdgeInsets.only(bottom: 2.0),
  99. child: Text(
  100. widget.data.title,
  101. style: TextStyle(
  102. fontSize: 14.0,
  103. color: _getColorByDeviceState(widget.data),
  104. ),
  105. maxLines: 2,
  106. ),
  107. ),
  108. Container(
  109. margin: EdgeInsets.only(top: 2.0),
  110. child: Text(
  111. widget.data.model,
  112. style: TextStyle(
  113. fontSize: 12.0,
  114. color: widget.data.connectionState ==
  115. DeviceConnectionState.CONNECTED
  116. ? AppColors.textSubtitleNormal
  117. : AppColors.textInactive),
  118. maxLines: 2,
  119. ),
  120. ),
  121. ],
  122. ),
  123. ),
  124. );
  125.  
  126. /// Connection state widget
  127. get _connectionState => Container(
  128. margin: EdgeInsets.only(right: 60.0),
  129. child: Align(
  130. alignment: Alignment.centerLeft,
  131. child: _getIconDataByDeviceState(widget.data),
  132. ),
  133. );
  134.  
  135. /// Relay state widget
  136. get _relayState => GestureDetector(
  137. onTap: () => _updateRelayState(),
  138. child: Container(
  139. height: 30.0,
  140. width: 47.0,
  141. margin: EdgeInsets.only(right: 20.0),
  142. child: FlareActor("assets/relay.flr",
  143. // animation: "Wait-On",
  144. controller: controller,
  145. alignment: Alignment.center,
  146. fit: BoxFit.contain),
  147. ),
  148. );
  149.  
  150. _navigateToDeviceEdit(BuildContext context) {
  151. Navigator.of(context).pushNamed(Routes.DEVICE_EDIT, arguments: widget.data.id);
  152. }
  153.  
  154. _updateRelayState() {
  155. switch (widget.data.relayState) {
  156. case RelayState.TURNED_OFF:
  157. widget.data.relayState = RelayState.TURNING_ON;
  158. break;
  159. case RelayState.TURNING_ON:
  160. widget.data.relayState = RelayState.TURNED_ON;
  161. break;
  162. case RelayState.TURNING_OFF:
  163. widget.data.relayState = RelayState.TURNED_OFF;
  164. break;
  165. case RelayState.TURNED_ON:
  166. widget.data.relayState = RelayState.TURNING_OFF;
  167. break;
  168. }
  169. controller.applyState(widget.data.relayState);
  170. }
  171. }
  172.  
  173. /// helper methods
  174. Color _getColorByDeviceState(DeviceModel device) {
  175. return device.connectionState == DeviceConnectionState.CONNECTED
  176. ? AppColors.textNormal
  177. : AppColors.textInactive;
  178. }
  179.  
  180. Icon _getIconDataByDeviceState(DeviceModel device) {
  181. switch (device.connectionState) {
  182. case DeviceConnectionState.CONNECTED:
  183. return Icon(AppIcons.ic_toolbar_wifi_on,
  184. color: AppColors.secondary, size: 14.0);
  185. case DeviceConnectionState.CONNECTING:
  186. return Icon(AppIcons.ic_toolbar_wifi_on,
  187. color: AppColors.textInactive, size: 14.0);
  188. case DeviceConnectionState.NO_CONNECTION:
  189. return Icon(AppIcons.ic_toolbar_wifi_no_connection,
  190. color: AppColors.textInactive, size: 14.0);
  191. default:
  192. return null;
  193. }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement