import 'package:flutter/material.dart'; import 'package:flutter_blue/flutter_blue.dart'; import 'package:flutter_tracker/dio/login_dao.dart'; import 'package:flutter_tracker/dio/track_dao.dart'; import 'package:flutter_tracker/model/user_model.dart'; import 'package:flutter_tracker/utils/app_util.dart'; import 'package:flutter_tracker/views/contact_card.dart'; import 'package:nearby_connections/nearby_connections.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'bluetooth_off_page.dart'; /// Description: /// Time : 2021年12月03日 Friday /// Author : liuyuqi.gov@msncn class TrackPage extends StatefulWidget { const TrackPage({Key key}) : super(key: key); @override _TrackPageState createState() => _TrackPageState(); } class _TrackPageState extends State { String testText = ''; final Strategy strategy = Strategy.P2P_STAR; UserModel currentUser; List blueList = []; List devices = []; List myScanResult = []; List updateTime = []; String token = ""; FlutterBlue flutterBlue = FlutterBlue.instance; @override Widget build(BuildContext context) { return StreamBuilder( stream: FlutterBlue.instance.state, initialData: BluetoothState.unknown, builder: (c, snapshot) { final state = snapshot.data; if (state == BluetoothState.on) { return buildContent(); } return BluetoothOffPage(state: state); }); } Column buildContent() { return Column( children: [ Expanded( child: Padding( padding: const EdgeInsets.only( left: 25.0, right: 25.0, bottom: 10.0, top: 30.0, ), child: Container( height: 50.0, width: double.infinity, decoration: BoxDecoration( color: Colors.deepPurple[500], borderRadius: BorderRadius.circular(20.0), boxShadow: const [ BoxShadow( color: Colors.black, blurRadius: 4.0, spreadRadius: 0.0, offset: Offset(2.0, 2.0), // shadow direction: bottom right ) ], ), child: Row( children: const [ Expanded( child: Image( image: AssetImage('assets/images/corona.png'), ), ), Expanded( flex: 2, child: Text( '附近用户', textAlign: TextAlign.left, style: TextStyle( fontSize: 21.0, color: Colors.white, fontWeight: FontWeight.w500, ), ), ) ], ), ), ), ), buildStartButton(), Expanded( flex: 2, child: Padding( padding: const EdgeInsets.symmetric(horizontal: 25.0), child: buildListView(), ), ), ], ); } Widget buildListView() { if (blueList.isNotEmpty) { return ListView.builder( itemBuilder: (context, index) { return ContactCard( imagePath: blueList[index].healthStatus == 1 ? 'assets/images/green.jpg' : 'assets/images/red.jpg', infection: blueList[index].healthStatus == 1 ? "阳性" : "阴性", username: blueList[index].username, updateTime: blueList[index].updateTime, deviceid: blueList[index].deviceid, ); }, itemCount: blueList.length, ); } else { return Text(""); } } StreamBuilder buildStartButton() { return StreamBuilder( stream: flutterBlue.isScanning, initialData: false, builder: (context, snapshot) { if (snapshot.data) { return Padding( padding: EdgeInsets.only(bottom: 200.0), child: RaisedButton( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(20.0)), elevation: 5.0, color: Colors.red, onPressed: () async { startTrack(snapshot.data); }, child: const Text( '停止追踪', style: TextStyle( fontSize: 20.0, fontWeight: FontWeight.bold, color: Colors.white, ), ), ), ); } else { return Padding( padding: EdgeInsets.only(bottom: 200.0), child: RaisedButton( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(20.0)), elevation: 5.0, color: Colors.deepPurple[400], onPressed: () async { startTrack(snapshot.data); }, child: const Text( '开始追踪', style: TextStyle( fontSize: 20.0, fontWeight: FontWeight.bold, color: Colors.white, ), ), ), ); } }); } @override void initState() { super.initState(); getPermissions(); getCurrentUser(); addContactsToList(); } void getCurrentUser() async { var sharedPreferences = await SharedPreferences.getInstance(); token = sharedPreferences.getString("token"); UserEntity userEntity = await LoginDao.getUserInfo(token); currentUser = userEntity.user; print("token" + token); print("user----------" + currentUser.username); } void getPermissions() async { Nearby().askLocationAndExternalStoragePermission(); } void addContactsToList() async { // 服务器获取当前用户数据 setState(() async { blueList = await TrackDao.getContactList(token); print("----------" + blueList.length.toString()); }); } void startTrack(bool flag) async { AppUtil.buildToast("正在获取..."); try { bool a = await Nearby().startDiscovery(currentUser.username, strategy, onEndpointFound: (id, name, serviceId) async { print('I saw id:$id with name:$name'); // the name here is an email TrackDao.uploadContact("token", name); }, onEndpointLost: (id) { print(id); }); print('DISCOVERING: ${a.toString()}'); } catch (e) { print(e); } } }