device_info_page.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_blue/flutter_blue.dart';
  3. import 'package:provider/provider.dart';
  4. import 'package:shared_preferences/shared_preferences.dart';
  5. import 'package:sport/provider/bluetooth.dart';
  6. import 'package:sport/router/navigator_util.dart';
  7. import 'package:sport/services/Converter.dart';
  8. import 'package:sport/widgets/appbar.dart';
  9. import 'package:sport/widgets/box.dart';
  10. import 'package:sport/widgets/dialog/search_device.dart';
  11. import 'package:sport/widgets/image.dart';
  12. import 'package:sport/widgets/loading.dart';
  13. class DeviceInfoPage extends StatefulWidget {
  14. @override
  15. State<StatefulWidget> createState() => _PageState();
  16. }
  17. class _PageState extends State<DeviceInfoPage> {
  18. var labelPadding = const EdgeInsets.symmetric(horizontal: 12.0, vertical: 10.0);
  19. var contentPadding = const EdgeInsets.symmetric(horizontal: 0.0);
  20. bool _vibrate = true;
  21. @override
  22. void initState() {
  23. super.initState();
  24. loadSetting();
  25. }
  26. void loadSetting() {
  27. SharedPreferences.getInstance().then((value) {
  28. _vibrate = value.getBool("vibrate") ?? true;
  29. setState(() {});
  30. });
  31. }
  32. @override
  33. Widget build(BuildContext context) {
  34. return Scaffold(
  35. body: CustomScrollView(
  36. slivers: <Widget>[
  37. buildSliverAppBar(context, "设备详情", backgroundColor: Theme.of(context).scaffoldBackgroundColor),
  38. SliverToBoxAdapter(
  39. child: Selector<Bluetooth, BluetoothDevice>(
  40. selector: (_, bluetooth) => bluetooth.device,
  41. builder: (_, device, ___) {
  42. return device == null ? RequestLoadingWidget() : _infoWidget(context, device);
  43. })),
  44. ],
  45. ));
  46. }
  47. Widget _infoWidget(BuildContext context, BluetoothDevice device) {
  48. return Column(crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[
  49. Padding(
  50. padding: labelPadding,
  51. child: Text("设置", style: Theme.of(context).textTheme.bodyText2),
  52. ),
  53. BoxWidget(
  54. paddingAll: false,
  55. body: ListView(
  56. padding: EdgeInsets.all(0),
  57. physics: NeverScrollableScrollPhysics(),
  58. shrinkWrap: true,
  59. children: ListTile.divideTiles(context: context, tiles: [
  60. ListTile(
  61. title: Row(
  62. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  63. children: <Widget>[
  64. Text("当前设备"),
  65. Text(
  66. "${device.name}",
  67. style: Theme.of(context).textTheme.bodyText2,
  68. )
  69. ],
  70. ),
  71. contentPadding: contentPadding,
  72. onTap: () {},
  73. ),
  74. ListTile(
  75. title: Row(
  76. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  77. children: <Widget>[
  78. Text("设备状态"),
  79. StreamBuilder<BluetoothDeviceState>(
  80. stream: device.state,
  81. initialData: BluetoothDeviceState.connecting,
  82. builder: (c, snapshot) => Text(
  83. (snapshot.data == BluetoothDeviceState.connected) ? "已连接" : "未连接",
  84. style: Theme.of(context).textTheme.bodyText2.copyWith(color: Theme.of(context).accentColor),
  85. ))
  86. ],
  87. ),
  88. contentPadding: contentPadding,
  89. ),
  90. ListTile(
  91. title: Row(
  92. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  93. children: <Widget>[
  94. Text("更换设备"),
  95. ],
  96. ),
  97. contentPadding: contentPadding,
  98. trailing: arrowRight5(),
  99. onTap: () {
  100. showDialog(context: context, child: SearchDeviceDialog());
  101. },
  102. ),
  103. ListTile(
  104. title: Row(
  105. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  106. children: <Widget>[
  107. Text("设备测试"),
  108. ],
  109. ),
  110. contentPadding: contentPadding,
  111. trailing: arrowRight5(),
  112. onTap: () {
  113. var device = Provider.of<Bluetooth>(context, listen: false)?.device;
  114. if (device != null) {
  115. NavigatorUtil.goPage(
  116. context,
  117. (context) => DeviceScreen(
  118. device: device,
  119. ));
  120. }
  121. },
  122. ),
  123. ]).toList())),
  124. BoxWidget(
  125. paddingAll: false,
  126. body: ListView(
  127. padding: EdgeInsets.all(0),
  128. physics: NeverScrollableScrollPhysics(),
  129. shrinkWrap: true,
  130. children: ListTile.divideTiles(context: context, tiles: [
  131. ListTile(
  132. title: Row(
  133. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  134. children: <Widget>[
  135. Text("震动反馈开关"),
  136. Switch(
  137. value: _vibrate,
  138. onChanged: (bool value) {
  139. SharedPreferences.getInstance().then((prefs) {
  140. _vibrate = value;
  141. prefs.setBool("vibrate", value);
  142. setState(() {});
  143. });
  144. },
  145. )
  146. ],
  147. ),
  148. contentPadding: contentPadding,
  149. ),
  150. ]).toList())),
  151. Padding(
  152. padding: labelPadding,
  153. child: Text("设备数据", style: Theme.of(context).textTheme.bodyText2),
  154. ),
  155. BoxWidget(
  156. paddingAll: false,
  157. body: ValueListenableBuilder(
  158. valueListenable: Provider.of<Bluetooth>(context, listen: false).dataNotifier,
  159. builder: (_, data, ___) => ListView(
  160. padding: EdgeInsets.all(0),
  161. physics: NeverScrollableScrollPhysics(),
  162. shrinkWrap: true,
  163. children: ListTile.divideTiles(context: context, tiles: [
  164. ListTile(
  165. title: Row(
  166. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  167. children: <Widget>[
  168. Text("设备电量"),
  169. Text(
  170. // "${min(Converter.toInt(data['0_electricity']),Converter.toInt(data['1_electricity']))}%",
  171. "${Converter.toInt(data['0_electricity'])}%\t\t/\t\t${Converter.toInt(data['1_electricity'])}%",
  172. style: Theme.of(context).textTheme.bodyText2,
  173. )
  174. ],
  175. ),
  176. contentPadding: contentPadding,
  177. onTap: () {
  178. Provider.of<Bluetooth>(context, listen: false).queryDeviceData();
  179. },
  180. ),
  181. ListTile(
  182. title: Row(
  183. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  184. children: <Widget>[
  185. Text("鞋内温度"),
  186. Text(
  187. "${data['0_temperature'] ?? 0}℃\t\t/\t\t${data['1_temperature'] ?? 0}℃",
  188. style: Theme.of(context).textTheme.bodyText2,
  189. )
  190. ],
  191. ),
  192. contentPadding: contentPadding,
  193. ),
  194. ListTile(
  195. title: Row(
  196. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  197. children: <Widget>[
  198. Text("压力指数"),
  199. Text(
  200. "${data['0_pressure'] ?? 0}\t\t/\t\t${data['1_pressure'] ?? 0}",
  201. style: Theme.of(context).textTheme.bodyText2,
  202. )
  203. ],
  204. ),
  205. contentPadding: contentPadding,
  206. ),
  207. ]).toList()))),
  208. Padding(
  209. padding: labelPadding,
  210. child: Text("基本信息", style: Theme.of(context).textTheme.bodyText2),
  211. ),
  212. BoxWidget(
  213. paddingAll: false,
  214. body: ValueListenableBuilder(
  215. valueListenable: Provider.of<Bluetooth>(context, listen: false).infoNotifier,
  216. builder: (_, data, ___) => ListView(
  217. padding: EdgeInsets.all(0),
  218. physics: NeverScrollableScrollPhysics(),
  219. shrinkWrap: true,
  220. children: ListTile.divideTiles(context: context, tiles: [
  221. ListTile(
  222. title: Row(
  223. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  224. children: <Widget>[
  225. Text("设备型号"),
  226. Text(
  227. "${data['name']?.toString()?.trim() ?? 'SHOES'}",
  228. style: Theme.of(context).textTheme.bodyText2,
  229. )
  230. ],
  231. ),
  232. contentPadding: contentPadding,
  233. onTap: () {
  234. Provider.of<Bluetooth>(context, listen: false).queryDeviceInfo();
  235. },
  236. ),
  237. // ListTile(
  238. // title: Row(
  239. // mainAxisAlignment: MainAxisAlignment.spaceBetween,
  240. // children: <Widget>[
  241. // Text("鞋码"),
  242. // Text(
  243. // "42码",
  244. // style: Theme.of(context).textTheme.bodyText2,
  245. // )
  246. // ],
  247. // ),
  248. // contentPadding: contentPadding,
  249. // ),
  250. ListTile(
  251. title: Row(
  252. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  253. children: <Widget>[
  254. Text("软件版本"),
  255. Text(
  256. "V${data['softwareVer'] ?? 0}",
  257. style: Theme.of(context).textTheme.bodyText2,
  258. )
  259. ],
  260. ),
  261. contentPadding: contentPadding,
  262. ),
  263. ListTile(
  264. title: Row(
  265. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  266. children: <Widget>[
  267. Text("硬件版本"),
  268. Text(
  269. "V${data['hardwareVer'] ?? 0}",
  270. style: Theme.of(context).textTheme.bodyText2,
  271. )
  272. ],
  273. ),
  274. contentPadding: contentPadding,
  275. ),
  276. ListTile(
  277. title: Row(
  278. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  279. children: <Widget>[
  280. Text("蓝牙地址"),
  281. Text(
  282. "${Provider.of<Bluetooth>(context, listen: false)?.device?.id?.id ?? ""}",
  283. style: Theme.of(context).textTheme.bodyText2,
  284. )
  285. ],
  286. ),
  287. contentPadding: contentPadding,
  288. ),
  289. ]).toList()))),
  290. ]);
  291. }
  292. }