track_page.dart 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_blue/flutter_blue.dart';
  3. import 'package:flutter_tracker/dio/login_dao.dart';
  4. import 'package:flutter_tracker/dio/track_dao.dart';
  5. import 'package:flutter_tracker/model/user_model.dart';
  6. import 'package:flutter_tracker/utils/app_util.dart';
  7. import 'package:flutter_tracker/views/contact_card.dart';
  8. import 'package:nearby_connections/nearby_connections.dart';
  9. import 'package:shared_preferences/shared_preferences.dart';
  10. import 'bluetooth_off_page.dart';
  11. /// Description:
  12. /// Time : 2021年12月03日 Friday
  13. /// Author : liuyuqi.gov@msncn
  14. class TrackPage extends StatefulWidget {
  15. const TrackPage({Key key}) : super(key: key);
  16. @override
  17. _TrackPageState createState() => _TrackPageState();
  18. }
  19. class _TrackPageState extends State<TrackPage> {
  20. String testText = '';
  21. final Strategy strategy = Strategy.P2P_STAR;
  22. UserModel currentUser;
  23. List<UserModel> blueList = [];
  24. List<BluetoothDevice> devices = [];
  25. List<ScanResult> myScanResult = [];
  26. List<String> updateTime = [];
  27. String token = "";
  28. FlutterBlue flutterBlue = FlutterBlue.instance;
  29. @override
  30. Widget build(BuildContext context) {
  31. return StreamBuilder<BluetoothState>(
  32. stream: FlutterBlue.instance.state,
  33. initialData: BluetoothState.unknown,
  34. builder: (c, snapshot) {
  35. final state = snapshot.data;
  36. if (state == BluetoothState.on) {
  37. return buildContent();
  38. }
  39. return BluetoothOffPage(state: state);
  40. });
  41. }
  42. Column buildContent() {
  43. return Column(
  44. children: <Widget>[
  45. Expanded(
  46. child: Padding(
  47. padding: const EdgeInsets.only(
  48. left: 25.0,
  49. right: 25.0,
  50. bottom: 10.0,
  51. top: 30.0,
  52. ),
  53. child: Container(
  54. height: 50.0,
  55. width: double.infinity,
  56. decoration: BoxDecoration(
  57. color: Colors.deepPurple[500],
  58. borderRadius: BorderRadius.circular(20.0),
  59. boxShadow: const [
  60. BoxShadow(
  61. color: Colors.black,
  62. blurRadius: 4.0,
  63. spreadRadius: 0.0,
  64. offset: Offset(2.0, 2.0), // shadow direction: bottom right
  65. )
  66. ],
  67. ),
  68. child: Row(
  69. children: const <Widget>[
  70. Expanded(
  71. child: Image(
  72. image: AssetImage('assets/images/corona.png'),
  73. ),
  74. ),
  75. Expanded(
  76. flex: 2,
  77. child: Text(
  78. '附近用户',
  79. textAlign: TextAlign.left,
  80. style: TextStyle(
  81. fontSize: 21.0,
  82. color: Colors.white,
  83. fontWeight: FontWeight.w500,
  84. ),
  85. ),
  86. )
  87. ],
  88. ),
  89. ),
  90. ),
  91. ),
  92. buildStartButton(),
  93. Expanded(
  94. flex: 2,
  95. child: Padding(
  96. padding: const EdgeInsets.symmetric(horizontal: 25.0),
  97. child: buildListView(),
  98. ),
  99. ),
  100. ],
  101. );
  102. }
  103. Widget buildListView() {
  104. if (blueList.isNotEmpty) {
  105. return ListView.builder(
  106. itemBuilder: (context, index) {
  107. return ContactCard(
  108. imagePath: blueList[index].healthStatus == 1
  109. ? 'assets/images/green.jpg'
  110. : 'assets/images/red.jpg',
  111. infection: blueList[index].healthStatus == 1 ? "阳性" : "阴性",
  112. username: blueList[index].username,
  113. updateTime: blueList[index].updateTime,
  114. deviceid: blueList[index].deviceid,
  115. );
  116. },
  117. itemCount: blueList.length,
  118. );
  119. } else {
  120. return Text("");
  121. }
  122. }
  123. StreamBuilder<bool> buildStartButton() {
  124. return StreamBuilder<bool>(
  125. stream: flutterBlue.isScanning,
  126. initialData: false,
  127. builder: (context, snapshot) {
  128. if (snapshot.data) {
  129. return Padding(
  130. padding: EdgeInsets.only(bottom: 200.0),
  131. child: RaisedButton(
  132. shape: RoundedRectangleBorder(
  133. borderRadius: BorderRadius.circular(20.0)),
  134. elevation: 5.0,
  135. color: Colors.red,
  136. onPressed: () async {
  137. startTrack(snapshot.data);
  138. },
  139. child: const Text(
  140. '停止追踪',
  141. style: TextStyle(
  142. fontSize: 20.0,
  143. fontWeight: FontWeight.bold,
  144. color: Colors.white,
  145. ),
  146. ),
  147. ),
  148. );
  149. } else {
  150. return Padding(
  151. padding: EdgeInsets.only(bottom: 200.0),
  152. child: RaisedButton(
  153. shape: RoundedRectangleBorder(
  154. borderRadius: BorderRadius.circular(20.0)),
  155. elevation: 5.0,
  156. color: Colors.deepPurple[400],
  157. onPressed: () async {
  158. startTrack(snapshot.data);
  159. },
  160. child: const Text(
  161. '开始追踪',
  162. style: TextStyle(
  163. fontSize: 20.0,
  164. fontWeight: FontWeight.bold,
  165. color: Colors.white,
  166. ),
  167. ),
  168. ),
  169. );
  170. }
  171. });
  172. }
  173. @override
  174. void initState() {
  175. super.initState();
  176. getPermissions();
  177. getCurrentUser();
  178. addContactsToList();
  179. }
  180. void getCurrentUser() async {
  181. var sharedPreferences = await SharedPreferences.getInstance();
  182. token = sharedPreferences.getString("token");
  183. UserEntity userEntity = await LoginDao.getUserInfo(token);
  184. currentUser = userEntity.user;
  185. print("token" + token);
  186. print("user----------" + currentUser.username);
  187. }
  188. void getPermissions() async {
  189. Nearby().askLocationAndExternalStoragePermission();
  190. }
  191. void addContactsToList() async {
  192. // 服务器获取当前用户数据
  193. setState(() async {
  194. blueList = await TrackDao.getContactList(token);
  195. print("----------" + blueList.length.toString());
  196. });
  197. }
  198. void startTrack(bool flag) async {
  199. AppUtil.buildToast("正在获取...");
  200. try {
  201. bool a = await Nearby().startDiscovery(currentUser.username, strategy,
  202. onEndpointFound: (id, name, serviceId) async {
  203. print('I saw id:$id with name:$name'); // the name here is an email
  204. TrackDao.uploadContact("token", name);
  205. }, onEndpointLost: (id) {
  206. print(id);
  207. });
  208. print('DISCOVERING: ${a.toString()}');
  209. } catch (e) {
  210. print(e);
  211. }
  212. }
  213. }