diff --git a/lib/Components/CustomAppBar.dart b/lib/Components/CustomAppBar.dart index 324f1e5..fa47ef6 100644 --- a/lib/Components/CustomAppBar.dart +++ b/lib/Components/CustomAppBar.dart @@ -1,5 +1,7 @@ import 'package:flutter/material.dart'; +import 'package:get/get.dart'; import 'package:mymuseum_visitapp/Components/LanguageSelection.dart'; +import 'package:mymuseum_visitapp/Helpers/requirement_state_controller.dart'; import 'package:mymuseum_visitapp/Models/visitContext.dart'; import 'package:mymuseum_visitapp/Screens/Home/home.dart'; import 'package:mymuseum_visitapp/app_context.dart'; @@ -19,7 +21,8 @@ class CustomAppBar extends StatefulWidget implements PreferredSizeWidget { Size get preferredSize => Size.fromHeight(_preferredHeight); } -class _CustomAppBarState extends State { +class _CustomAppBarState extends State with WidgetsBindingObserver { + final controller = Get.find(); @override Widget build(BuildContext context) { @@ -36,6 +39,12 @@ class _CustomAppBarState extends State { setState(() { VisitAppContext visitAppContext = appContext.getContext(); visitAppContext.configuration = null; + visitAppContext.isBeaconEnabled = false; + + // TODO ADD CHECK IF RUNNING + print("PAUSE SCAAAAN"); + controller.pauseScanning(); + Navigator.of(context).pop(); /*Navigator.of(context).pushReplacement(MaterialPageRoute( builder: (context) => const HomePage(), diff --git a/lib/Components/ScannerBouton.dart b/lib/Components/ScannerBouton.dart index ac982ef..8a6f158 100644 --- a/lib/Components/ScannerBouton.dart +++ b/lib/Components/ScannerBouton.dart @@ -28,6 +28,7 @@ class _ScannerBoutonState extends State { width: 85.0, child: FittedBox( child: FloatingActionButton( + heroTag: "scanner", onPressed: _onItemTapped, tooltip: 'Scanner', backgroundColor: kBlue1, diff --git a/lib/Models/beaconSection.dart b/lib/Models/beaconSection.dart index 14944b4..e3a1012 100644 --- a/lib/Models/beaconSection.dart +++ b/lib/Models/beaconSection.dart @@ -2,17 +2,19 @@ import 'package:manager_api/api.dart'; class BeaconSection { String? macAddress; + String? configurationId; String? sectionId; int? rssi; int? accuracy; String? proximityUUID; bool? found = false; - BeaconSection({this.macAddress, this.sectionId, this.rssi, this.accuracy, this.proximityUUID, this.found}); + BeaconSection({this.macAddress, this.configurationId, this.sectionId, this.rssi, this.accuracy, this.proximityUUID, this.found}); Map toMap() { return { 'macAddress': macAddress, + 'configurationId': configurationId, 'sectionId': sectionId, 'rssi': rssi, 'accuracy': accuracy, @@ -24,6 +26,7 @@ class BeaconSection { factory BeaconSection.fromJson(Map json) { return BeaconSection( macAddress: json['macAddress'] as String, + configurationId: json['configurationId'] as String, sectionId: json['sectionId'] as String, rssi: json['rssi'] as int, accuracy: json['accuracy'] as int, @@ -34,6 +37,6 @@ class BeaconSection { @override String toString() { - return 'BeaconSection{macAddress: $macAddress, sectionId: $sectionId, rssi: $rssi, accuracy: $accuracy, proximityUUID: $proximityUUID, found: $found}'; + return 'BeaconSection{macAddress: $macAddress, sectionId: $sectionId, configurationId: $configurationId, rssi: $rssi, accuracy: $accuracy, proximityUUID: $proximityUUID, found: $found}'; } } \ No newline at end of file diff --git a/lib/Models/visitContext.dart b/lib/Models/visitContext.dart index a232898..26aee72 100644 --- a/lib/Models/visitContext.dart +++ b/lib/Models/visitContext.dart @@ -10,6 +10,7 @@ class VisitAppContext with ChangeNotifier{ ConfigurationDTO? configuration; List? sectionIds; // Use to valid QR code found List? beaconSections; + bool isBeaconEnabled = false; VisitAppContext({this.language, this.id, this.configuration, this.instanceId}); diff --git a/lib/Screens/Home/configurations_list.dart b/lib/Screens/Home/configurations_list.dart index 53e7e28..590d83a 100644 --- a/lib/Screens/Home/configurations_list.dart +++ b/lib/Screens/Home/configurations_list.dart @@ -8,7 +8,6 @@ import 'package:mymuseum_visitapp/Components/Loading.dart'; import 'package:mymuseum_visitapp/Components/SearchBox.dart'; import 'package:mymuseum_visitapp/Components/SearchNumberBox.dart'; import 'package:mymuseum_visitapp/Helpers/DatabaseHelper.dart'; -import 'package:mymuseum_visitapp/Helpers/requirement_state_controller.dart'; import 'package:mymuseum_visitapp/Helpers/translationHelper.dart'; import 'package:mymuseum_visitapp/Models/visitContext.dart'; import 'package:mymuseum_visitapp/Screens/Article/article.dart'; @@ -31,8 +30,7 @@ class ConfigurationsList extends StatefulWidget { State createState() => _ConfigurationsListState(); } -class _ConfigurationsListState extends State with WidgetsBindingObserver { - final controller = Get.find(); +class _ConfigurationsListState extends State { List configurations = []; List alreadyDownloaded = []; VisitAppContext? visitAppContext; @@ -69,9 +67,6 @@ class _ConfigurationsListState extends State with WidgetsBin visitAppContext.sectionIds = List.from(await DatabaseHelper.instance.queryWithConfigurationId(DatabaseTableType.sections, visitAppContext.configuration!.id!)).map((e) => e.id).toList(); appContext.setContext(visitAppContext); - print("PAUSE SCANNN"); - controller.pauseScanning(); - Navigator.push( context, MaterialPageRoute( @@ -98,9 +93,6 @@ class _ConfigurationsListState extends State with WidgetsBin visitAppContext.sectionIds = sections.map((e) => e.id).toList(); } - print("PAUSE SCANNN"); - controller.pauseScanning(); - Navigator.push( context, MaterialPageRoute( @@ -132,14 +124,14 @@ class _ConfigurationsListState extends State with WidgetsBin future: ApiService.getResource(appContext, configurations[index].imageId!), builder: (context, AsyncSnapshot snapshot) { if (snapshot.connectionState == ConnectionState.done) { - return snapshot.data != null ? Container( - child: ClipRRect( - borderRadius: const BorderRadius.only(topLeft: Radius.circular(20), bottomLeft: Radius.circular(20)), - child: Image.memory( - base64Decode(snapshot.data.data!), - fit: BoxFit.cover - ), - ), + print("RESULLT DATA"); + print(snapshot.data); + return snapshot.data != null ? ClipRRect( + borderRadius: const BorderRadius.only(topLeft: Radius.circular(20), bottomLeft: Radius.circular(20)), + child: snapshot.data.data != null ? Image.memory( + base64Decode(snapshot.data.data!), + fit: BoxFit.cover + ) : null, ) : const Text(""); } else if (snapshot.connectionState == ConnectionState.none) { return Text(TranslationHelper.getFromLocale("noData", appContext)); diff --git a/lib/Screens/Home/home.dart b/lib/Screens/Home/home.dart index 3187897..e564c56 100644 --- a/lib/Screens/Home/home.dart +++ b/lib/Screens/Home/home.dart @@ -4,8 +4,6 @@ import 'dart:io'; import 'package:flutter/material.dart'; import 'package:auto_size_text/auto_size_text.dart'; import 'package:flutter/services.dart'; -import 'package:flutter_beacon/flutter_beacon.dart'; -import 'package:get/get.dart'; import 'package:manager_api/api.dart'; import 'package:mymuseum_visitapp/Components/CustomAppBar.dart'; import 'package:mymuseum_visitapp/Components/Loading.dart'; @@ -21,7 +19,6 @@ import 'package:mymuseum_visitapp/Services/downloadConfiguration.dart'; import 'package:mymuseum_visitapp/app_context.dart'; import 'package:mymuseum_visitapp/client.dart'; import 'package:mymuseum_visitapp/constants.dart'; -import 'package:permission_handler/permission_handler.dart'; import 'package:provider/provider.dart'; import 'configurations_list.dart'; @@ -34,279 +31,23 @@ class HomePage extends StatefulWidget { } class _HomePageState extends State with WidgetsBindingObserver { - final controller = Get.find(); - StreamSubscription? _streamBluetooth; int currentIndex = 0; List configurations = []; List alreadyDownloaded = []; VisitAppContext? visitAppContext; - StreamSubscription? _streamRanging; - final _regionBeacons = >{}; - final _beacons = []; - //final controller = Get.find(); - - @override - void initState() { - WidgetsBinding.instance.addObserver(this); - - super.initState(); - - listeningState(); - } - - listeningState() async { - print('Listening to bluetooth state'); - _streamBluetooth = flutterBeacon - .bluetoothStateChanged() - .listen((BluetoothState state) async { - controller.updateBluetoothState(state); - await checkAllRequirements(); - }); - } - - checkAllRequirements() async { - final bluetoothState = await flutterBeacon.bluetoothState; - controller.updateBluetoothState(bluetoothState); - print('BLUETOOTH $bluetoothState'); - - final authorizationStatus = await flutterBeacon.authorizationStatus; - controller.updateAuthorizationStatus(authorizationStatus); - print('AUTHORIZATION $authorizationStatus'); - - final locationServiceEnabled = - await flutterBeacon.checkLocationServicesIfEnabled; - controller.updateLocationService(locationServiceEnabled); - print('LOCATION SERVICE $locationServiceEnabled'); - - if (controller.bluetoothEnabled && - controller.authorizationStatusOk && - controller.locationServiceEnabled) { - print('STATE READY'); - //if (currentIndex == 0) { - print('SCANNING'); - //controller.startScanning(); - - - var status = await Permission.bluetoothScan.status; - - if (status.isDenied) { - print("IS DENIIIED"); - // We didn't ask for permission yet or the permission has been denied before but not permanently. - } - - // You can request multiple permissions at once. - Map statuses = await [ - Permission.bluetoothScan, - Permission.bluetoothConnect, - ].request(); - print(statuses[Permission.bluetoothScan]); - print(statuses[Permission.bluetoothConnect]); - - print(status); - - /*controller.startStream.listen((flag) { - if (flag == true) { - initScanBeacon(); - } - });*/ - /*} else { - print('BROADCASTING'); - controller.startBroadcasting(); - }*/ - } else { - print('STATE NOT READY'); - controller.pauseScanning(); - } - } - - initScanBeacon() async { - await flutterBeacon.initializeScanning; - if (!controller.authorizationStatusOk || - !controller.locationServiceEnabled || - !controller.bluetoothEnabled) { - print( - 'RETURNED, authorizationStatusOk=${controller.authorizationStatusOk}, ' - 'locationServiceEnabled=${controller.locationServiceEnabled}, ' - 'bluetoothEnabled=${controller.bluetoothEnabled}'); - return; - } - final regions = [ - Region( - identifier: 'MyMuseumB' - ), - ]; - - if (_streamRanging != null) { - if (_streamRanging!.isPaused) { - _streamRanging?.resume(); - return; - } - } - - _streamRanging = - flutterBeacon.ranging(regions).listen((RangingResult result) { - print(result); - if (mounted) { - - print("visitAppContext"); - print(visitAppContext); - print(visitAppContext!.beaconSections); - if(result.beacons.isNotEmpty && result.beacons.any((b) => b.proximity == "immediate" && visitAppContext!.beaconSections!.any((bs) => b.macAddress == bs!.macAddress) )) { - print("GOT ONE BEACON THAT I KNOOOOOOOOW"); - print(result); - - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text("GOT ONE BEACON THAT I KNOOOOOOOOW"), backgroundColor: kBlue2), - ); - } - //setState(() { - _regionBeacons[result.region] = result.beacons; - _beacons.clear(); - _regionBeacons.values.forEach((list) { - _beacons.addAll(list); - }); - _beacons.sort(_compareParameters); - //}); - } - }); - } - - /*pauseScanBeacon() async { - _streamRanging?.pause(); - if (_beacons.isNotEmpty) { - setState(() { - _beacons.clear(); - }); - } - }*/ - - int _compareParameters(Beacon a, Beacon b) { - int compare = a.proximityUUID.compareTo(b.proximityUUID); - - if (compare == 0) { - compare = a.major.compareTo(b.major); - } - - if (compare == 0) { - compare = a.minor.compareTo(b.minor); - } - - return compare; - } - - @override - void didChangeAppLifecycleState(AppLifecycleState state) async { - print('AppLifecycleState = $state'); - if (state == AppLifecycleState.resumed) { - if (_streamBluetooth != null) { - if (_streamBluetooth!.isPaused) { - _streamBluetooth?.resume(); - } - } - await checkAllRequirements(); - } else if (state == AppLifecycleState.paused) { - _streamBluetooth?.pause(); - } - } - @override Widget build(BuildContext context) { Size size = MediaQuery.of(context).size; final appContext = Provider.of(context); visitAppContext = appContext.getContext(); - /*if (controller.bluetoothEnabled && - controller.authorizationStatusOk && - controller.locationServiceEnabled) { - print("START SCANNING IN BUILD"); - //controller.startScanning(); - }*/ return Scaffold( appBar: CustomAppBar( title: TranslationHelper.getFromLocale("visitTitle", appContext), isHomeButton: false, ), - /*AppBar( - title: const Text('Flutter Beacon'), - centerTitle: false, - actions: [ - Obx(() { - if (!controller.locationServiceEnabled) - return IconButton( - tooltip: 'Not Determined', - icon: Icon(Icons.portable_wifi_off), - color: Colors.grey, - onPressed: () {}, - ); - - if (!controller.authorizationStatusOk) - return IconButton( - tooltip: 'Not Authorized', - icon: Icon(Icons.portable_wifi_off), - color: Colors.red, - onPressed: () async { - await flutterBeacon.requestAuthorization; - }, - ); - - return IconButton( - tooltip: 'Authorized', - icon: Icon(Icons.wifi_tethering), - color: Colors.blue, - onPressed: () async { - await flutterBeacon.requestAuthorization; - }, - ); - }), - Obx(() { - return IconButton( - tooltip: controller.locationServiceEnabled - ? 'Location Service ON' - : 'Location Service OFF', - icon: Icon( - controller.locationServiceEnabled - ? Icons.location_on - : Icons.location_off, - ), - color: - controller.locationServiceEnabled ? Colors.blue : Colors.red, - onPressed: controller.locationServiceEnabled - ? () {} - : handleOpenLocationSettings, - ); - }), - Obx(() { - final state = controller.bluetoothState.value; - - if (state == BluetoothState.stateOn) { - return IconButton( - tooltip: 'Bluetooth ON', - icon: Icon(Icons.bluetooth_connected), - onPressed: () {}, - color: Colors.lightBlueAccent, - ); - } - - if (state == BluetoothState.stateOff) { - return IconButton( - tooltip: 'Bluetooth OFF', - icon: Icon(Icons.bluetooth), - onPressed: handleOpenBluetooth, - color: Colors.red, - ); - } - - return IconButton( - icon: Icon(Icons.bluetooth_disabled), - tooltip: 'Bluetooth State Unknown', - onPressed: () {}, - color: Colors.grey, - ); - }), - ], - ),*/ body: SingleChildScrollView( child: SizedBox( width: size.width, @@ -316,21 +57,6 @@ class _HomePageState extends State with WidgetsBindingObserver { builder: (context, AsyncSnapshot snapshot) { if (snapshot.connectionState == ConnectionState.done) { configurations = List.from(snapshot.data).where((configuration) => configuration.isMobile!).toList(); - - - controller.startStream.listen((flag) async { - print(flag); - if (flag == true) { - print("FIIIIIIREEEE ---------------"); - await initScanBeacon(); - controller.startScanning(); - } - }); - - - controller.startScanning(); - - return RefreshIndicator ( onRefresh: () { setState(() {}); @@ -389,56 +115,6 @@ class _HomePageState extends State with WidgetsBindingObserver { ); } - handleOpenLocationSettings() async { - if (Platform.isAndroid) { - await flutterBeacon.openLocationSettings; - } else if (Platform.isIOS) { - await showDialog( - context: context, - builder: (context) { - return AlertDialog( - title: Text('Location Services Off'), - content: Text( - 'Please enable Location Services on Settings > Privacy > Location Services.', - ), - actions: [ - TextButton( - onPressed: () => Navigator.pop(context), - child: Text('OK'), - ), - ], - ); - }, - ); - } - } - - handleOpenBluetooth() async { - if (Platform.isAndroid) { - try { - await flutterBeacon.openBluetoothSettings; - } on PlatformException catch (e) { - print(e); - } - } else if (Platform.isIOS) { - await showDialog( - context: context, - builder: (context) { - return AlertDialog( - title: Text('Bluetooth is Off'), - content: Text('Please enable Bluetooth on Settings > Bluetooth.'), - actions: [ - TextButton( - onPressed: () => Navigator.pop(context), - child: Text('OK'), - ), - ], - ); - }, - ); - } - } - Future?> getConfigurationsCall(Client client, AppContext appContext) async { bool isOnline = await hasNetwork(); VisitAppContext visitAppContext = appContext.getContext(); @@ -460,7 +136,7 @@ class _HomePageState extends State with WidgetsBindingObserver { if(visitAppContext.beaconSections == null) { List? sections = await ApiService.getAllBeacons(client, visitAppContext.instanceId!); if(sections != null && sections.isNotEmpty) { - List beaconSections = sections.map((e) => BeaconSection(macAddress: e.beaconId, sectionId: e.id)).toList(); + List beaconSections = sections.map((e) => BeaconSection(macAddress: e.beaconId, configurationId: e.configurationId, sectionId: e.id)).toList(); visitAppContext.beaconSections = beaconSections; print("Got some Beacons for you"); print(beaconSections); diff --git a/lib/Screens/Visit/visit.dart b/lib/Screens/Visit/visit.dart index 04eff31..a9ddfc5 100644 --- a/lib/Screens/Visit/visit.dart +++ b/lib/Screens/Visit/visit.dart @@ -1,11 +1,19 @@ +import 'dart:async'; +import 'dart:io'; + import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_beacon/flutter_beacon.dart'; +import 'package:get/get.dart'; import 'package:manager_api/api.dart'; import 'package:mymuseum_visitapp/Components/CustomAppBar.dart'; import 'package:mymuseum_visitapp/Components/ScannerBouton.dart'; +import 'package:mymuseum_visitapp/Helpers/requirement_state_controller.dart'; import 'package:mymuseum_visitapp/Helpers/translationHelper.dart'; import 'package:mymuseum_visitapp/Models/visitContext.dart'; import 'package:mymuseum_visitapp/app_context.dart'; import 'package:mymuseum_visitapp/constants.dart'; +import 'package:permission_handler/permission_handler.dart'; import 'package:provider/provider.dart'; import 'components/body.dart'; @@ -19,9 +27,196 @@ class VisitPage extends StatefulWidget { State createState() => _VisitPageState(); } -class _VisitPageState extends State { +class _VisitPageState extends State with WidgetsBindingObserver { ConfigurationDTO? configuration; + // Beacon specific + final controller = Get.find(); + StreamSubscription? _streamBluetooth; + StreamSubscription? _streamRanging; + final _regionBeacons = >{}; + final _beacons = []; + StreamSubscription? listener; + + @override + void initState() { + WidgetsBinding.instance.addObserver(this); + + super.initState(); + + listeningState(); + } + + listeningState() async { + print('Listening to bluetooth state'); + _streamBluetooth = flutterBeacon + .bluetoothStateChanged() + .listen((BluetoothState state) async { + controller.updateBluetoothState(state); + await checkAllRequirements(); + }); + } + + checkAllRequirements() async { + final bluetoothState = await flutterBeacon.bluetoothState; + controller.updateBluetoothState(bluetoothState); + print('BLUETOOTH $bluetoothState'); + + final authorizationStatus = await flutterBeacon.authorizationStatus; + controller.updateAuthorizationStatus(authorizationStatus); + print('AUTHORIZATION $authorizationStatus'); + + final locationServiceEnabled = + await flutterBeacon.checkLocationServicesIfEnabled; + controller.updateLocationService(locationServiceEnabled); + print('LOCATION SERVICE $locationServiceEnabled'); + + if (controller.bluetoothEnabled && + controller.authorizationStatusOk && + controller.locationServiceEnabled) { + print('STATE READY'); + //if (currentIndex == 0) { + print('SCANNING'); + //controller.startScanning(); + + + var status = await Permission.bluetoothScan.status; + + if (status.isDenied) { + print("IS DENIIIED"); + // We didn't ask for permission yet or the permission has been denied before but not permanently. + } + + // You can request multiple permissions at once. + Map statuses = await [ + Permission.bluetoothScan, + Permission.bluetoothConnect, + ].request(); + print(statuses[Permission.bluetoothScan]); + print(statuses[Permission.bluetoothConnect]); + + print(status); + + /*controller.startStream.listen((flag) { + if (flag == true) { + initScanBeacon(); + } + });*/ + /*} else { + print('BROADCASTING'); + controller.startBroadcasting(); + }*/ + } else { + print('STATE NOT READY'); + controller.pauseScanning(); + } + } + + initScanBeacon(VisitAppContext visitAppContext) async { + await flutterBeacon.initializeScanning; + if (!controller.authorizationStatusOk || + !controller.locationServiceEnabled || + !controller.bluetoothEnabled) { + print( + 'RETURNED, authorizationStatusOk=${controller.authorizationStatusOk}, ' + 'locationServiceEnabled=${controller.locationServiceEnabled}, ' + 'bluetoothEnabled=${controller.bluetoothEnabled}'); + return; + } + final regions = [ + Region( + identifier: 'MyMuseumB' + ), + ]; + + if (_streamRanging != null) { + if (_streamRanging!.isPaused) { + _streamRanging?.resume(); + return; + } + } + + _streamRanging = + flutterBeacon.ranging(regions).listen((RangingResult result) { + //print(result); + if (mounted) { + + //print("visitAppContext"); + //print(visitAppContext); + //print(visitAppContext!.beaconSections); + + if(result.beacons.isNotEmpty) { + print(result); + print(result.beacons.map((b) => b.macAddress)); + print(visitAppContext.beaconSections!.map((bb) => bb!.macAddress)); + + var beaconList = visitAppContext.beaconSections!.where((bs) => result.beacons.any((element) => element.macAddress == bs!.macAddress)); + if(beaconList.isNotEmpty) + { + print("GOT ONE BEACON THAT I KNOOOOOOOOW"); + print(beaconList); + + // TODO SORT BY COMPARE + + print("BEFORE SNACKBAR"); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text('BEACON - ${result.beacons.first.macAddress} - ${result.beacons.first.accuracy} - ${result.beacons.first.proximity.name}'), backgroundColor: kBlue2), + ); + + print("AFTER SNACKBAR"); + } + } + + //setState(() { + /*_regionBeacons[result.region] = result.beacons; + _beacons.clear(); + _regionBeacons.values.forEach((list) { + _beacons.addAll(list); + }); + _beacons.sort(_compareParameters);*/ + //}); + } + }); + } + + /*pauseScanBeacon() async { + _streamRanging?.pause(); + if (_beacons.isNotEmpty) { + setState(() { + _beacons.clear(); + }); + } + }*/ + + int _compareParameters(Beacon a, Beacon b) { + int compare = a.proximityUUID.compareTo(b.proximityUUID); + + if (compare == 0) { + compare = a.major.compareTo(b.major); + } + + if (compare == 0) { + compare = a.minor.compareTo(b.minor); + } + + return compare; + } + + @override + void didChangeAppLifecycleState(AppLifecycleState state) async { + print('AppLifecycleState = $state'); + if (state == AppLifecycleState.resumed) { + if (_streamBluetooth != null) { + if (_streamBluetooth!.isPaused) { + _streamBluetooth?.resume(); + } + } + await checkAllRequirements(); + } else if (state == AppLifecycleState.paused) { + _streamBluetooth?.pause(); + } + } + @override /*Widget build(BuildContext context) { return new WillPopScope( @@ -37,11 +232,30 @@ class _VisitPageState extends State { ), ); }*/ + + @override + void onDispose() { + print("DISPOSE VISIT PAGE"); + + super.dispose(); + } + Widget build(BuildContext context) { final appContext = Provider.of(context); VisitAppContext visitAppContext = appContext.getContext(); configuration = visitAppContext.configuration; + //if(visitAppContext.isBeaconEnabled) { + listener = controller.startStream.listen((flag) async { + print(flag); + if (flag == true) { + print("FIIIIIIREEEE ---------------"); + await initScanBeacon(visitAppContext); + controller.startScanning(); + } + }); + //} + return /*WillPopScope( child:*/ Scaffold( appBar: CustomAppBar( @@ -50,9 +264,124 @@ class _VisitPageState extends State { ), backgroundColor: kBackgroundGrey, body: Body(configurationId: configuration!.id), // TODO handle error.. - floatingActionButton: ScannerBouton(appContext: appContext), + floatingActionButton: Stack( + children: [ + visitAppContext.beaconSections!.where((bs) => bs!.configurationId == visitAppContext.configuration!.id).isNotEmpty ? Align( + alignment: Alignment.bottomRight, + child: Padding( + padding: const EdgeInsets.only(right: 90, bottom: 1), + child: Container( + height: 65.0, + width: 65.0, + child: FittedBox( + child: FloatingActionButton( + heroTag: "beacon", + onPressed: () { + if(!visitAppContext.isBeaconEnabled) { + print("Start Scan"); + controller.startScanning(); + listener!.resume(); + + visitAppContext.isBeaconEnabled = true; + appContext.setContext(visitAppContext); + } else { + print("Pause Scan"); + controller.pauseScanning(); // PAUSE OR DISPOSE ? + //controller.dispose(); // PAUSE OR DISPOSE ? + print(controller.pauseStream); + listener!.cancel(); + listener!.pause(); + //controller.removeListenerId(listener.i, () { }); + visitAppContext.isBeaconEnabled = false; + appContext.setContext(visitAppContext); + } + }, + tooltip: 'Beacon', + backgroundColor: visitAppContext.isBeaconEnabled ? kBlue1 : Colors.grey, + child: const Icon(Icons.my_location), + ), + ), + ), + ), + ) : const SizedBox(), + Align( + alignment: Alignment.bottomRight, + child: ScannerBouton(appContext: appContext), + ), + ], + ),/*Padding( + padding: const EdgeInsets.only(right: 10), + child: Row( + children: [ + Container( + height: 65.0, + width: 65.0, + child: FittedBox( + child: FloatingActionButton( + heroTag: "beacon", + onPressed: () {}, + tooltip: 'Beacon', + backgroundColor: kBlue1, + child: const Icon(Icons.qr_code_scanner), + ), + ), + ), + ScannerBouton(appContext: appContext), + ], + ), + ),*/ );//, //onWillPop: () async => false, //); } + + handleOpenLocationSettings() async { + if (Platform.isAndroid) { + await flutterBeacon.openLocationSettings; + } else if (Platform.isIOS) { + await showDialog( + context: context, + builder: (context) { + return AlertDialog( + title: Text('Location Services Off'), + content: Text( + 'Please enable Location Services on Settings > Privacy > Location Services.', + ), + actions: [ + TextButton( + onPressed: () => Navigator.pop(context), + child: Text('OK'), + ), + ], + ); + }, + ); + } + } + + handleOpenBluetooth() async { + if (Platform.isAndroid) { + try { + await flutterBeacon.openBluetoothSettings; + } on PlatformException catch (e) { + print(e); + } + } else if (Platform.isIOS) { + await showDialog( + context: context, + builder: (context) { + return AlertDialog( + title: Text('Bluetooth is Off'), + content: Text('Please enable Bluetooth on Settings > Bluetooth.'), + actions: [ + TextButton( + onPressed: () => Navigator.pop(context), + child: Text('OK'), + ), + ], + ); + }, + ); + } + } } \ No newline at end of file