track_page.dart 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_blue/flutter_blue.dart';
  3. import 'package:flutter_tracker/utils/app_util.dart';
  4. import 'package:flutter_tracker/views/contact_card.dart';
  5. import 'bluetooth_off_page.dart';
  6. /// Description:
  7. /// Time : 2021年12月03日 Friday
  8. /// Author : liuyuqi.gov@msncn
  9. class TrackPage extends StatefulWidget {
  10. const TrackPage({Key key}) : super(key: key);
  11. @override
  12. _TrackPageState createState() => _TrackPageState();
  13. }
  14. class _TrackPageState extends State<TrackPage> {
  15. String testText = '';
  16. List<dynamic> contactTraces = [];
  17. List<dynamic> contactTimes = [];
  18. List<dynamic> contactLocations = [];
  19. FlutterBlue flutterBlue = FlutterBlue.instance;
  20. @override
  21. Widget build(BuildContext context) {
  22. return StreamBuilder<BluetoothState>(
  23. stream: FlutterBlue.instance.state,
  24. initialData: BluetoothState.unknown,
  25. builder: (c, snapshot) {
  26. final state = snapshot.data;
  27. if (state == BluetoothState.on) {
  28. return buildContent();
  29. }
  30. return BluetoothOffPage(state: state);
  31. });
  32. }
  33. Column buildContent() {
  34. return Column(
  35. children: <Widget>[
  36. Expanded(
  37. child: Padding(
  38. padding: const EdgeInsets.only(
  39. left: 25.0,
  40. right: 25.0,
  41. bottom: 10.0,
  42. top: 30.0,
  43. ),
  44. child: Container(
  45. height: 50.0,
  46. width: double.infinity,
  47. decoration: BoxDecoration(
  48. color: Colors.deepPurple[500],
  49. borderRadius: BorderRadius.circular(20.0),
  50. boxShadow: const [
  51. BoxShadow(
  52. color: Colors.black,
  53. blurRadius: 4.0,
  54. spreadRadius: 0.0,
  55. offset: Offset(2.0, 2.0), // shadow direction: bottom right
  56. )
  57. ],
  58. ),
  59. child: Row(
  60. children: const <Widget>[
  61. Expanded(
  62. child: Image(
  63. image: AssetImage('assets/images/corona.png'),
  64. ),
  65. ),
  66. Expanded(
  67. flex: 2,
  68. child: Text(
  69. '附近用户',
  70. textAlign: TextAlign.left,
  71. style: TextStyle(
  72. fontSize: 21.0,
  73. color: Colors.white,
  74. fontWeight: FontWeight.w500,
  75. ),
  76. ),
  77. )
  78. ],
  79. ),
  80. ),
  81. ),
  82. ),
  83. buildStartButton(),
  84. Expanded(
  85. flex: 2,
  86. child: Padding(
  87. padding: const EdgeInsets.symmetric(horizontal: 25.0),
  88. child: ListView.builder(
  89. itemBuilder: (context, index) {
  90. return ContactCard(
  91. imagePath: 'assets/images/head.jpg',
  92. infection: '健康',
  93. contactUsername: contactTraces[index],
  94. contactTime: contactTimes[index],
  95. );
  96. },
  97. itemCount: contactTraces.length,
  98. ),
  99. ),
  100. ),
  101. ],
  102. );
  103. }
  104. StreamBuilder<bool> buildStartButton() {
  105. return StreamBuilder<bool>(
  106. stream: flutterBlue.isScanning,
  107. initialData: false,
  108. builder: (context, snapshot) {
  109. if (snapshot.data) {
  110. return Padding(
  111. padding: EdgeInsets.only(bottom: 200.0),
  112. child: RaisedButton(
  113. shape: RoundedRectangleBorder(
  114. borderRadius: BorderRadius.circular(20.0)),
  115. elevation: 5.0,
  116. color: Colors.red,
  117. onPressed: () async {
  118. startTrack(snapshot.data);
  119. },
  120. child: const Text(
  121. '停止追踪',
  122. style: TextStyle(
  123. fontSize: 20.0,
  124. fontWeight: FontWeight.bold,
  125. color: Colors.white,
  126. ),
  127. ),
  128. ),
  129. );
  130. } else {
  131. return Padding(
  132. padding: EdgeInsets.only(bottom: 200.0),
  133. child: RaisedButton(
  134. shape: RoundedRectangleBorder(
  135. borderRadius: BorderRadius.circular(20.0)),
  136. elevation: 5.0,
  137. color: Colors.deepPurple[400],
  138. onPressed: () async {
  139. startTrack(snapshot.data);
  140. },
  141. child: const Text(
  142. '开始追踪',
  143. style: TextStyle(
  144. fontSize: 20.0,
  145. fontWeight: FontWeight.bold,
  146. color: Colors.white,
  147. ),
  148. ),
  149. ),
  150. );
  151. }
  152. });
  153. }
  154. void startTrack(bool flag) async {
  155. if (flag) {
  156. flutterBlue.stopScan();
  157. } else {
  158. AppUtil.buildToast("正在搜索附近的人...");
  159. flutterBlue.startScan(timeout: const Duration(seconds: 4));
  160. // 扫描周围蓝牙设备
  161. // List<BluetoothService> services = await device.discoverServices();
  162. var subscription = flutterBlue.scanResults.listen((scanResult) {
  163. for (ScanResult scan in scanResult) {
  164. BluetoothDevice device = scan.device;
  165. print("------------------------------" +
  166. '${device.name} found! rssi: ${scan.rssi}dBm' +
  167. device.id.toString());
  168. }
  169. });
  170. // 上传周边设备到服务器
  171. // 循环对每个设备检测安全性
  172. //更新结果
  173. }
  174. // try {
  175. // bool a = await Nearby().startAdvertising(
  176. // loggedInUser.email,
  177. // strategy,
  178. // onConnectionInitiated: null,
  179. // onConnectionResult: (id, status) {
  180. // print(status);
  181. // },
  182. // onDisconnected: (id) {
  183. // print('Disconnected $id');
  184. // },
  185. // );
  186. //
  187. // print('ADVERTISING ${a.toString()}');
  188. // } catch (e) {
  189. // print(e);
  190. // }
  191. discovery();
  192. AppUtil.buildToast("追踪用户状态中...");
  193. }
  194. void discovery() async {}
  195. }