diff --git a/example/lib/advanced/actions.dart b/example/lib/advanced/actions.dart index 4881fddc..e02a8b98 100644 --- a/example/lib/advanced/actions.dart +++ b/example/lib/advanced/actions.dart @@ -70,7 +70,7 @@ class Actions { email = await util.Dialog.prompt(context, title: "Email log", labelText: 'Email', value: email, hintText: ''); if (email!.length > 0) { - prefs.setString("email", email!); + prefs.setString("email", email); util.Dialog.alert(context, 'Email log', 'The log will be processed in the background (it can take some time depending on the size of the log). Your email client will launch when ready.'); diff --git a/example/lib/advanced/app.dart b/example/lib/advanced/app.dart index 0bc4c729..cd440fa2 100644 --- a/example/lib/advanced/app.dart +++ b/example/lib/advanced/app.dart @@ -31,14 +31,18 @@ class _AdvancedAppState extends State { final ThemeData theme = ThemeData(); return new MaterialApp( theme: theme.copyWith( - colorScheme: theme.colorScheme.copyWith(secondary:Colors.black), + colorScheme: theme.colorScheme.copyWith(secondary: Colors.black), brightness: Brightness.light, - bottomAppBarColor: Colors.amberAccent, + bottomAppBarTheme: Theme.of(context) + .bottomAppBarTheme + .copyWith(color: Colors.amberAccent), hintColor: Colors.black12, primaryTextTheme: Theme.of(context).primaryTextTheme.apply( - bodyColor: Colors.black, - )), - home: - Scaffold(body: HomeView(), floatingActionButtonLocation: FloatingActionButtonLocation.endFloat, floatingActionButton: MainMenuButton())); + bodyColor: Colors.black, + )), + home: Scaffold( + body: HomeView(), + floatingActionButtonLocation: FloatingActionButtonLocation.endFloat, + floatingActionButton: MainMenuButton())); } } diff --git a/example/lib/advanced/geofence_view.dart b/example/lib/advanced/geofence_view.dart index b014c702..f9ffb4d6 100644 --- a/example/lib/advanced/geofence_view.dart +++ b/example/lib/advanced/geofence_view.dart @@ -7,12 +7,13 @@ import 'package:flutter_background_geolocation_example/advanced/util/dialog.dart as util; class GeofenceView extends StatefulWidget { - LatLng? center; - List? vertices; + final LatLng? center; + final List? vertices; GeofenceView({this.center, this.vertices}); @override - State createState() => _GeofenceViewState(center: this.center, vertices: this.vertices); + State createState() => + _GeofenceViewState(center: this.center, vertices: this.vertices); } class _GeofenceViewState extends State { @@ -55,7 +56,7 @@ class _GeofenceViewState extends State { 'longitude': center?.longitude } } // meta-data for tracker.transistorsoft.com - )).then((bool success) { + )).then((bool success) { bg.BackgroundGeolocation.playSound( util.Dialog.getSoundId('ADD_GEOFENCE')); }).catchError((error) { @@ -84,8 +85,7 @@ class _GeofenceViewState extends State { }, 'vertices': vertices } // meta-data for tracker.transistorsoft.com - )).then((bool success) { - + )).then((bool success) { bg.BackgroundGeolocation.playSound( util.Dialog.getSoundId('ADD_GEOFENCE')); }).catchError((error) { @@ -106,7 +106,7 @@ class _GeofenceViewState extends State { color: Colors.black), title: const Text('Add Geofence'), foregroundColor: Colors.black, - backgroundColor: Theme.of(context).bottomAppBarColor, + backgroundColor: Theme.of(context).bottomAppBarTheme.color, iconTheme: IconThemeData(color: Colors.black), actions: [ MaterialButton(child: Text('Add'), onPressed: _onClickAdd) diff --git a/example/lib/advanced/home_view.dart b/example/lib/advanced/home_view.dart index c1d229e7..abca3ac5 100644 --- a/example/lib/advanced/home_view.dart +++ b/example/lib/advanced/home_view.dart @@ -3,7 +3,8 @@ import 'dart:async'; import 'dart:convert'; import 'package:shared_preferences/shared_preferences.dart'; -import 'package:flutter_background_geolocation/flutter_background_geolocation.dart' as bg; +import 'package:flutter_background_geolocation/flutter_background_geolocation.dart' + as bg; import 'package:background_fetch/background_fetch.dart'; import 'package:http/http.dart' as http; @@ -26,7 +27,8 @@ class HomeView extends StatefulWidget { State createState() => HomeViewState(); } -class HomeViewState extends State with TickerProviderStateMixin, WidgetsBindingObserver { +class HomeViewState extends State + with TickerProviderStateMixin, WidgetsBindingObserver { Future _prefs = SharedPreferences.getInstance(); TabController? _tabController; @@ -55,11 +57,7 @@ class HomeViewState extends State with TickerProviderStateMixin with TickerProviderStateMixin with TickerProviderStateMixin with TickerProviderStateMixin with TickerProviderStateMixin with TickerProviderStateMixin with TickerProviderStateMixin with TickerProviderStateMixin with TickerProviderStateMixin with TickerProviderStateMixin with TickerProviderStateMixin with TickerProviderStateMixin with TickerProviderStateMixin with TickerProviderStateMixin with TickerProviderStateMixin with TickerProviderStateMixin with TickerProviderStateMixin with TickerProviderStateMixin with TickerProviderStateMixin[ - Switch(value: _enabled!, onChanged: _onClickEnable - ), + Switch(value: _enabled!, onChanged: _onClickEnable), ], bottom: TabBar( controller: _tabController, indicatorColor: Colors.red, - tabs: [ - Tab(icon: Icon(Icons.map)), - Tab(icon: Icon(Icons.list)) - ] - ) - ), + tabs: [Tab(icon: Icon(Icons.map)), Tab(icon: Icon(Icons.list))])), //body: body, body: SharedEvents( events: events, child: TabBarView( controller: _tabController, - children: [ - MapView(), - EventList() - ], - physics: new NeverScrollableScrollPhysics() - ) - ), + children: [MapView(), EventList()], + physics: new NeverScrollableScrollPhysics())), bottomNavigationBar: BottomAppBar( color: Colors.amberAccent, child: Container( @@ -511,19 +507,16 @@ class HomeViewState extends State with TickerProviderStateMixin(Colors.black) - ) - ), + foregroundColor: MaterialStateProperty.all( + Colors.black))), MaterialButton( minWidth: 50.0, - child: Icon((_isMoving!) ? Icons.pause : Icons.play_arrow, color: Colors.white), + child: Icon( + (_isMoving!) ? Icons.pause : Icons.play_arrow, + color: Colors.white), color: (_isMoving!) ? Colors.red : Colors.green, - onPressed: _onClickChangePace - ) - ] - ) - ) - ), + onPressed: _onClickChangePace) + ]))), ); } @@ -532,9 +525,11 @@ class HomeViewState extends State with TickerProviderStateMixin with TickerProviderStateMixin { late BuildContext _context; - void _onClickMenu() async { - - } - void _onClickSettings() { bg.BackgroundGeolocation.playSound(util.Dialog.getSoundId("OPEN")); Navigator.of(_context).push(MaterialPageRoute( @@ -36,6 +31,7 @@ class MainMenuButtonState extends State { void _onClickResetOdometer() { bg.BackgroundGeolocation.setOdometer(0.0).catchError((error) { print('********** [resetOdometer] ERROR: $error'); + throw error; }); } @@ -52,14 +48,16 @@ class MainMenuButtonState extends State { } void _onClickRequestPermission() async { - bg.ProviderChangeEvent providerState = await bg.BackgroundGeolocation.providerState; + bg.ProviderChangeEvent providerState = + await bg.BackgroundGeolocation.providerState; showDialog( context: context, barrierDismissible: false, builder: (BuildContext context) { return AlertDialog( title: Text("Request Permission"), - content: Text("Current Authorization status: ${providerState.status}"), + content: + Text("Current Authorization status: ${providerState.status}"), actions: [ TextButton( child: Text('WhenInUse'), @@ -82,14 +80,20 @@ class MainMenuButtonState extends State { void _requestPermission(String request) async { Navigator.of(context).pop(); - await bg.BackgroundGeolocation.setConfig(bg.Config(locationAuthorizationRequest: request)); + await bg.BackgroundGeolocation.setConfig( + bg.Config(locationAuthorizationRequest: request)); int status = await bg.BackgroundGeolocation.requestPermission(); print("[requestPermission] status: $status"); - util.Dialog.alert(context, "Request Permission", "Authorization status: $status"); - - bg.ProviderChangeEvent providerState = await bg.BackgroundGeolocation.providerState; - if ((providerState.status == bg.ProviderChangeEvent.AUTHORIZATION_STATUS_ALWAYS) && (providerState.accuracyAuthorization == bg.ProviderChangeEvent.ACCURACY_AUTHORIZATION_REDUCED)) { + util.Dialog.alert( + context, "Request Permission", "Authorization status: $status"); + + bg.ProviderChangeEvent providerState = + await bg.BackgroundGeolocation.providerState; + if ((providerState.status == + bg.ProviderChangeEvent.AUTHORIZATION_STATUS_ALWAYS) && + (providerState.accuracyAuthorization == + bg.ProviderChangeEvent.ACCURACY_AUTHORIZATION_REDUCED)) { // Request full accuracy. //int status = await bg.BackgroundGeolocation.requestTemporaryFullAccuracy("Demo Purpose"); //util.Dialog.alert(context, "Request Full Accuracy", "Accuracy Authorization: ${status}"); @@ -131,11 +135,11 @@ class MainMenuButtonState extends State { child: Icon(Icons.settings), onTap: _onClickSettings), SpeedDialChild( - label: "Email log", - backgroundColor: bgColor, - foregroundColor: Colors.black, - child: Icon(Icons.email), - onTap: _onClickEmailLog), + label: "Email log", + backgroundColor: bgColor, + foregroundColor: Colors.black, + child: Icon(Icons.email), + onTap: _onClickEmailLog), SpeedDialChild( label: "Upload locations", backgroundColor: bgColor, @@ -155,13 +159,12 @@ class MainMenuButtonState extends State { child: Icon(Icons.lock_open), onTap: _onClickRequestPermission), SpeedDialChild( - //hasLabel: true, + //hasLabel: true, label: "Destroy locations", backgroundColor: bgColor, foregroundColor: Colors.black, child: Icon(Icons.delete), onTap: _onClickDestroyLocations) - ]; } diff --git a/example/lib/advanced/map_view.dart b/example/lib/advanced/map_view.dart index 2cf94fbc..ea1a892d 100644 --- a/example/lib/advanced/map_view.dart +++ b/example/lib/advanced/map_view.dart @@ -19,8 +19,10 @@ class MapView extends StatefulWidget { State createState() => MapViewState(); } -class MapViewState extends State with AutomaticKeepAliveClientMixin { - static const LOCATION_ARROW_IMAGE_PATH = "assets/images/markers/location-arrow-blue.png"; +class MapViewState extends State + with AutomaticKeepAliveClientMixin { + static const LOCATION_ARROW_IMAGE_PATH = + "assets/images/markers/location-arrow-blue.png"; @override bool get wantKeepAlive { @@ -28,7 +30,6 @@ class MapViewState extends State with AutomaticKeepAliveClientMixin _polyline = []; @@ -122,14 +123,18 @@ class MapViewState extends State with AutomaticKeepAliveClientMixin marker.geofence?.identifier == event.identifier); + (GeofenceMarker marker) => + marker.geofence?.identifier == event.identifier); if (marker == null) { - bool exists = await bg.BackgroundGeolocation.geofenceExists(event.identifier); + bool exists = + await bg.BackgroundGeolocation.geofenceExists(event.identifier); if (exists) { // Maybe this is a boot from a geofence event and geofencechange hasn't yet fired - bg.Geofence? geofence = await bg.BackgroundGeolocation.getGeofence(event.identifier); + bg.Geofence? geofence = + await bg.BackgroundGeolocation.getGeofence(event.identifier); marker = GeofenceMarker(geofence!); _geofences.add(marker); } else { @@ -154,7 +159,7 @@ class MapViewState extends State with AutomaticKeepAliveClientMixin with AutomaticKeepAliveClientMixin event location double bearing = Geospatial.getBearing(center, hit); // Compute a coordinate at the intersection of the line joining center point -> event location and the circle. - LatLng edge = Geospatial.computeOffsetCoordinate(center, geofence.radius!, bearing); + LatLng edge = + Geospatial.computeOffsetCoordinate(center, geofence.radius!, bearing); // Green for ENTER, Red for EXIT. - Color color = Colors.green; var colorName = 'green'; if (event.action == "EXIT") { - color = Colors.red; colorName = 'red'; } else if (event.action == "DWELL") { - color = Colors.yellow; colorName = 'amber'; } // Colored circular image marker (red/amber/green) on geofence edge. - _geofenceEventEdges.add(Marker(point: edge, width: 16, height: 16, rotate: false, builder: (context) { - return Image.asset("assets/images/markers/geofence-event-edge-circle-${event.action.toLowerCase()}.png"); - })); + _geofenceEventEdges.add(Marker( + point: edge, + width: 16, + height: 16, + rotate: false, + builder: (context) { + return Image.asset( + "assets/images/markers/geofence-event-edge-circle-${event.action.toLowerCase()}.png"); + })); // Colored event location-arrow Marker (red/amber/green) double heading = location.coords.heading.round().toDouble(); - _geofenceEventLocations.add(Marker(point: hit, width: 24, height: 24, rotate: false, builder: (context) { - return Transform.rotate(angle: (heading * (math.pi / 180)), child: Image.asset("assets/images/markers/location-arrow-${colorName}.png")); - })); + _geofenceEventLocations.add(Marker( + point: hit, + width: 24, + height: 24, + rotate: false, + builder: (context) { + return Transform.rotate( + angle: (heading * (math.pi / 180)), + child: Image.asset( + "assets/images/markers/location-arrow-${colorName}.png")); + })); // Polyline joining the two above. - _geofenceEventPolylines.add(Polyline(points: [edge, hit], strokeWidth: 2.0, color: Colors.black)); + _geofenceEventPolylines.add( + Polyline(points: [edge, hit], strokeWidth: 2.0, color: Colors.black)); } bool hasGeofenceMarker(String identifier) { - return _geofences.firstWhereOrNull( - (GeofenceMarker marker) => marker.geofence?.identifier == identifier - ) != null; + return _geofences.firstWhereOrNull((GeofenceMarker marker) => + marker.geofence?.identifier == identifier) != + null; } void _onGeofencesChange(bg.GeofencesChangeEvent event) { @@ -213,13 +231,13 @@ class MapViewState extends State with AutomaticKeepAliveClientMixin with AutomaticKeepAliveClientMixin with AutomaticKeepAliveClientMixin= 0) ? location.coords.heading.round().toDouble() : 0; - _locations.add(Marker(point: ll, width: 16, height: 16, rotate: false, builder: (context) { - return Transform.rotate(angle: (heading * (math.pi / 180)), child: Image.asset(LOCATION_ARROW_IMAGE_PATH)); - })); + double heading = (location.coords.heading >= 0) + ? location.coords.heading.round().toDouble() + : 0; + _locations.add(Marker( + point: ll, + width: 16, + height: 16, + rotate: false, + builder: (context) { + return Transform.rotate( + angle: (heading * (math.pi / 180)), + child: Image.asset(LOCATION_ARROW_IMAGE_PATH)); + })); } /// Update Big Blue current position dot. @@ -259,7 +285,7 @@ class MapViewState extends State with AutomaticKeepAliveClientMixin with AutomaticKeepAliveClientMixin with AutomaticKeepAliveClientMixin[ - BottomSheetAction(title: const Text('Circular'), onPressed: (context) { - Navigator.of(context).pop(); - Navigator.of(context).push(MaterialPageRoute( - fullscreenDialog: true, - builder: (BuildContext context) { - return GeofenceView(center: latLng); + BottomSheetAction( + title: const Text('Circular'), + onPressed: (context) { + Navigator.of(context).pop(); + Navigator.of(context).push(MaterialPageRoute( + fullscreenDialog: true, + builder: (BuildContext context) { + return GeofenceView(center: latLng); + })); + }), + BottomSheetAction( + title: const Text('Polygon'), + onPressed: (context) { + setState(() { + _isCreatingPolygonGeofence = true; + }); + _showPolygonGeofenceMenu(); + Navigator.of(context).pop(); }) - ); - }), - BottomSheetAction(title: const Text('Polygon'), onPressed: (context) { - setState(() { - _isCreatingPolygonGeofence = true; - }); - _showPolygonGeofenceMenu(); - Navigator.of(context).pop(); - }) ], - cancelAction: CancelAction(title: const Text('Cancel'), onPressed: (context) { - Navigator.of(context).pop(); - _cancelAddGeofence(); - }),// onPressed parameter is optional by default will dismiss the ActionSheet + cancelAction: CancelAction( + title: const Text('Cancel'), + onPressed: (context) { + Navigator.of(context).pop(); + _cancelAddGeofence(); + }), // onPressed parameter is optional by default will dismiss the ActionSheet ); /* Navigator.of(context).push(MaterialPageRoute( @@ -360,8 +395,8 @@ class MapViewState extends State with AutomaticKeepAliveClientMixin with AutomaticKeepAliveClientMixin( - fullscreenDialog: true, - builder: (BuildContext context) { - return GeofenceView(vertices: vertices); - }) - ); - }) - ] - ), - Row( + color: Colors.amberAccent, + border: Border(bottom: BorderSide(color: Colors.black))), + child: Column(children: [ + Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - MaterialButton(child: Icon(Icons.undo), onPressed: () { + MaterialButton( + child: Text("Cancel", + style: TextStyle(color: Colors.white)), + color: Colors.red, + onPressed: _cancelAddGeofence), + + //Text("Tap map to add points", style: TextStyle(fontWeight: FontWeight.bold)), + MaterialButton( + child: Text("Next", + style: TextStyle(color: Colors.white)), + color: Colors.blue, + onPressed: () { + var vertices = _polygonGeofenceCursorMarkers + .map((Marker marker) { + return marker.point; + }).toList(); + _cancelAddGeofence(); + Navigator.of(context) + .push(MaterialPageRoute( + fullscreenDialog: true, + builder: (BuildContext context) { + return GeofenceView(vertices: vertices); + })); + }) + ]), + Row(children: [ + MaterialButton( + child: Icon(Icons.undo), + onPressed: () { HapticFeedback.heavyImpact(); if (_polygonGeofenceCursorPoints.isEmpty) return; setState(() { @@ -419,22 +449,19 @@ class MapViewState extends State with AutomaticKeepAliveClientMixin with AutomaticKeepAliveClientMixin with AutomaticKeepAliveClientMixin { icon: Icon(Icons.close), color: Colors.black), title: const Text('Settings'), - backgroundColor: Theme.of(context).bottomAppBarColor, + backgroundColor: Theme.of(context).bottomAppBarTheme.color, iconTheme: IconThemeData(color: Colors.black), actions: [ new PopupMenuButton( @@ -605,7 +605,6 @@ class _SettingsViewState extends State { .catchError(onFailure); } }; - break; default: return (String? value) { bg.Config config = new bg.Config(); diff --git a/example/lib/app.dart b/example/lib/app.dart index 64cd8bdb..8eadfacd 100644 --- a/example/lib/app.dart +++ b/example/lib/app.dart @@ -39,7 +39,7 @@ class _HomeAppState extends State { final ThemeData theme = ThemeData(); return new MaterialApp( theme: theme.copyWith( - colorScheme: theme.colorScheme.copyWith(secondary:Colors.black)), + colorScheme: theme.colorScheme.copyWith(secondary: Colors.black)), home: new _HomeView()); } } @@ -173,22 +173,21 @@ class _HomeViewState extends State<_HomeView> { break; default: return; - break; } bg.BackgroundGeolocation.playSound(util.Dialog.getSoundId("OPEN")); runApp(app); } bool _usernameIsValid(String username) { - return (username != null) && - new RegExp(USERNAME_REGEXP).hasMatch(username) && + return new RegExp(USERNAME_REGEXP).hasMatch(username) && (username.length > 0); } void _launchUrl() async { String url = '${ENV.TRACKER_HOST}/$_orgname'; - if (await canLaunch(url)) { - await launch(url); + Uri uri = Uri.parse(url); + if (await canLaunchUrl(uri)) { + await launchUrl(uri); } else { throw 'Could not launch $url'; } @@ -198,10 +197,9 @@ class _HomeViewState extends State<_HomeView> { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - title: const Text('Background Geolocation'), - foregroundColor: Colors.black, - backgroundColor: Colors.amberAccent - ), + title: const Text('Background Geolocation'), + foregroundColor: Colors.black, + backgroundColor: Colors.amberAccent), body: Container( color: Colors.black87, padding: EdgeInsets.only(top: 20.0), @@ -262,36 +260,35 @@ class _HomeViewState extends State<_HomeView> { mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ SizedBox( - width: 140, - child: ElevatedButton( - onPressed: () { - _showRegistration(); - }, - child: Text('Edit'), - style: ButtonStyle( - backgroundColor: MaterialStateProperty.all(Colors.redAccent), - ) - )), - //color: Colors.redAccent, - //textColor: Colors.white), + width: 140, + child: ElevatedButton( + onPressed: () { + _showRegistration(); + }, + child: Text('Edit'), + style: ButtonStyle( + backgroundColor: MaterialStateProperty.all( + Colors.redAccent), + ))), + //color: Colors.redAccent, + //textColor: Colors.white), SizedBox( width: 140, child: ElevatedButton( - onPressed: () { - _launchUrl(); - }, - child: Text('View Tracking'), - style: ButtonStyle( - //foregroundColor: MaterialStateProperty.all(Colors.redAccent), - backgroundColor: MaterialStateProperty.all(Colors.blue)) - ) - ) - //color: Colors.blue, - //textColor: Colors.white), + onPressed: () { + _launchUrl(); + }, + child: Text('View Tracking'), + style: ButtonStyle( + //foregroundColor: MaterialStateProperty.all(Colors.redAccent), + backgroundColor: MaterialStateProperty.all( + Colors.blue)))) + //color: Colors.blue, + //textColor: Colors.white), ]))); } - MaterialButton _buildApplicationButton(String text, {onPressed=Function}) { + MaterialButton _buildApplicationButton(String text, {onPressed = Function}) { return MaterialButton( onPressed: onPressed, child: Text(text, style: TextStyle(fontSize: 18.0)), diff --git a/example/lib/config/transistor_auth.dart b/example/lib/config/transistor_auth.dart index 98ca28f4..18314873 100644 --- a/example/lib/config/transistor_auth.dart +++ b/example/lib/config/transistor_auth.dart @@ -16,6 +16,7 @@ void _onHttp(bg.HttpEvent event) async { if (success) { bg.BackgroundGeolocation.sync().catchError((error) { print("[sync] error: $error"); + return []; }); } break; diff --git a/example/lib/hello_world/app.dart b/example/lib/hello_world/app.dart index ea3f754a..f027dc29 100644 --- a/example/lib/hello_world/app.dart +++ b/example/lib/hello_world/app.dart @@ -1,4 +1,3 @@ -import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'dart:async'; @@ -26,7 +25,7 @@ class HelloWorldApp extends StatelessWidget { return new MaterialApp( title: 'BackgroundGeolocation Demo', theme: theme.copyWith( - colorScheme: theme.colorScheme.copyWith(secondary:Colors.black), + colorScheme: theme.colorScheme.copyWith(secondary: Colors.black), primaryTextTheme: Theme.of(context).primaryTextTheme.apply( bodyColor: Colors.black, )), @@ -238,16 +237,15 @@ class _HelloWorldPageState extends State { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - leading: IconButton( - icon: Icon(Icons.home, color: Colors.black), - onPressed: _onClickHome), - title: const Text('BG Geo'), - foregroundColor: Colors.black, - actions: [ - Switch(value: _enabled, onChanged: _onClickEnable), - ], - backgroundColor: Colors.amberAccent - ), + leading: IconButton( + icon: Icon(Icons.home, color: Colors.black), + onPressed: _onClickHome), + title: const Text('BG Geo'), + foregroundColor: Colors.black, + actions: [ + Switch(value: _enabled, onChanged: _onClickEnable), + ], + backgroundColor: Colors.amberAccent), body: SingleChildScrollView(child: Text('$_content')), bottomNavigationBar: BottomAppBar( color: Colors.amberAccent, diff --git a/example/lib/registration_view.dart b/example/lib/registration_view.dart index 9be48602..ae1dc3e2 100644 --- a/example/lib/registration_view.dart +++ b/example/lib/registration_view.dart @@ -1,5 +1,4 @@ import 'package:flutter/material.dart'; -import 'package:flutter/services.dart'; import 'package:flutter_background_geolocation_example/advanced/util/dialog.dart' as util; import 'package:shared_preferences/shared_preferences.dart'; @@ -77,14 +76,12 @@ class _RegistrationViewState extends State { bg.BackgroundGeolocation.setConfig( bg.Config(transistorAuthorizationToken: token)); - Map result = {"orgname": _orgname, "username": _username}; Navigator.pop(context, result); } bool _usernameIsValid(String username) { - return (username != null) && - new RegExp(USERNAME_REGEXP).hasMatch(username) && + return new RegExp(USERNAME_REGEXP).hasMatch(username) && (username.length > 0); } @@ -113,11 +110,11 @@ class _RegistrationViewState extends State { iconTheme: IconThemeData(color: Colors.black), actions: [ TextButton( - onPressed: _onClickSave, - child: Text("REGISTER", - style: TextStyle( - fontWeight: FontWeight.bold, color: Colors.blue)), - ) + onPressed: _onClickSave, + child: Text("REGISTER", + style: TextStyle( + fontWeight: FontWeight.bold, color: Colors.blue)), + ) ]), body: Form( key: _formKey, diff --git a/example/test_driver/app.dart b/example/test_driver/app.dart index cc8c9f71..01cc37d6 100644 --- a/example/test_driver/app.dart +++ b/example/test_driver/app.dart @@ -13,7 +13,8 @@ import 'package:flutter/material.dart'; import 'dart:async'; import 'dart:convert'; -import 'package:flutter_background_geolocation/flutter_background_geolocation.dart' as bg; +import 'package:flutter_background_geolocation/flutter_background_geolocation.dart' + as bg; // Global ref to TestApp. It's ugly but we're just testing. This is so the function #testHandler can reference the TestApp. TestApp? app; @@ -25,16 +26,16 @@ TestApp? app; void main() async { app = TestApp(); - Map deviceParams = await bg.Config.deviceParams; + Map deviceParams = await bg.Config.deviceParams; bg.BackgroundGeolocation.ready(bg.Config( reset: true, debug: true, logLevel: bg.Config.LOG_LEVEL_VERBOSE, autoSync: true, extras: {"foo": "bar"}, - url: "http://tracker.transistorsoft.com/locations/transistor-flutter-test", - params: deviceParams - )); + url: + "http://tracker.transistorsoft.com/locations/transistor-flutter-test", + params: deviceParams)); // Register #testHanlder Function (@see below) to receive test-actions from app_test.dart enableFlutterDriverExtension(handler: testHandler); @@ -45,184 +46,170 @@ void main() async { /// This is where the test requests from TestDriver in app_test.dart are received /// Future testHandler(String? command) async { - Action action = new Action(command: command!); + Action action = new Action(command: command!); - print("FlutterDriverExtension Rx: $command"); + print("FlutterDriverExtension Rx: $command"); - // Setup - bg.BackgroundGeolocation.removeListeners(); - await bg.BackgroundGeolocation.removeGeofences(); - await bg.BackgroundGeolocation.stop(); - await bg.BackgroundGeolocation.reset(bg.Config( - debug: true, - logLevel: bg.Config.LOG_LEVEL_VERBOSE - )); + // Setup + bg.BackgroundGeolocation.removeListeners(); + await bg.BackgroundGeolocation.removeGeofences(); + await bg.BackgroundGeolocation.stop(); + await bg.BackgroundGeolocation.reset( + bg.Config(debug: true, logLevel: bg.Config.LOG_LEVEL_VERBOSE)); - switch(command) { - case 'getState': - bg.State state = await bg.BackgroundGeolocation.state; - action.result = state; - app?.addAction(action); - return state.toString(); - break; - case 'getCurrentPosition': - bg.Location location = await bg.BackgroundGeolocation.getCurrentPosition(samples: 1); + switch (command) { + case 'getState': + bg.State state = await bg.BackgroundGeolocation.state; + action.result = state; + app?.addAction(action); + return state.toString(); + case 'getCurrentPosition': + bg.Location location = + await bg.BackgroundGeolocation.getCurrentPosition(samples: 1); + action.result = location; + app?.addAction(action); + return location.toString(); + case 'getGeofences': + bg.Geofence geofence = bg.Geofence( + identifier: 'test', + radius: 200.0, + latitude: 1.0, + longitude: 1.0, + notifyOnEntry: true, + extras: {"foo": "bar"}); + await bg.BackgroundGeolocation.addGeofence(geofence); + List geofences = await bg.BackgroundGeolocation.geofences; + List rs = []; + geofences.forEach((bg.Geofence geofence) { + rs.add(geofence.toMap()); + }); + action.result = rs; + app?.addAction(action); + return jsonEncode(rs); + case 'start': + bg.State state = await bg.BackgroundGeolocation.start(); + action.result = state; + app?.addAction(action); + return state.toString(); + case 'onLocation': + Completer completer = new Completer(); + bg.BackgroundGeolocation.onLocation((bg.Location location) { action.result = location; app?.addAction(action); - return location.toString(); - break; - case 'getGeofences': - bg.Geofence geofence = bg.Geofence( - identifier: 'test', - radius: 200.0, - latitude: 1.0, - longitude: 1.0, - notifyOnEntry: true, - extras: {"foo":"bar"} - ); - await bg.BackgroundGeolocation.addGeofence(geofence); - List geofences = await bg.BackgroundGeolocation.geofences; - List rs = []; - geofences.forEach((bg.Geofence geofence) { - rs.add(geofence.toMap()); - }); - action.result = rs; + completer.complete(location.toString()); + }); + bg.BackgroundGeolocation.start(); + return completer.future as String; + case 'onMotionChange': + Completer completer = new Completer(); + bg.BackgroundGeolocation.onMotionChange((bg.Location location) { + action.result = location; app?.addAction(action); - return jsonEncode(rs); - break; - case 'start': - bg.State state = await bg.BackgroundGeolocation.start(); - action.result = state; + completer.complete(location.toString()); + }); + bg.BackgroundGeolocation.start(); + return completer.future as String; + case 'onGeofence': + Completer completer = new Completer(); + bg.BackgroundGeolocation.onGeofence((bg.GeofenceEvent event) { + action.result = event; app?.addAction(action); - return state.toString(); - break; - case 'onLocation': - Completer completer = new Completer(); - bg.BackgroundGeolocation.onLocation((bg.Location location) { - action.result = location; - app?.addAction(action); - completer.complete(location.toString()); - }); - bg.BackgroundGeolocation.start(); - return completer.future as String; - break; - case 'onMotionChange': - Completer completer = new Completer(); - bg.BackgroundGeolocation.onMotionChange((bg.Location location) { - action.result = location; - app?.addAction(action); - completer.complete(location.toString()); - }); - bg.BackgroundGeolocation.start(); - return completer.future as String; - break; - case 'onGeofence': - Completer completer = new Completer(); - bg.BackgroundGeolocation.onGeofence((bg.GeofenceEvent event) { - action.result = event; - app?.addAction(action); - completer.complete(event.toString()); - }); + completer.complete(event.toString()); + }); - bg.Location location = await bg.BackgroundGeolocation.getCurrentPosition(samples: 1); - bg.Geofence geofence = bg.Geofence( - identifier: 'test', - radius: 200.0, - latitude: location.coords.latitude, - longitude: location.coords.longitude, - notifyOnEntry: true - ); - bg.BackgroundGeolocation.addGeofence(geofence); - bg.BackgroundGeolocation.start(); - return await completer.future; - break; - case 'onEnabledChange:true': - Completer completer = new Completer(); - bg.BackgroundGeolocation.onEnabledChange((bool enabled) { + bg.Location location = + await bg.BackgroundGeolocation.getCurrentPosition(samples: 1); + bg.Geofence geofence = bg.Geofence( + identifier: 'test', + radius: 200.0, + latitude: location.coords.latitude, + longitude: location.coords.longitude, + notifyOnEntry: true); + bg.BackgroundGeolocation.addGeofence(geofence); + bg.BackgroundGeolocation.start(); + return await completer.future; + case 'onEnabledChange:true': + Completer completer = new Completer(); + bg.BackgroundGeolocation.onEnabledChange((bool enabled) { + action.result = enabled; + app?.addAction(action); + completer.complete(enabled.toString()); + }); + await bg.BackgroundGeolocation.start(); + return completer.future as String; + case 'onEnabledChange:false': + Completer completer = new Completer(); + bg.BackgroundGeolocation.onEnabledChange((bool enabled) { + if (enabled == false) { action.result = enabled; app?.addAction(action); completer.complete(enabled.toString()); - }); - await bg.BackgroundGeolocation.start(); - return completer.future as String; - break; - case 'onEnabledChange:false': - Completer completer = new Completer(); - bg.BackgroundGeolocation.onEnabledChange((bool enabled) { - if (enabled == false) { - action.result = enabled; - app?.addAction(action); - completer.complete(enabled.toString()); - } - }); - await bg.BackgroundGeolocation.start(); - await bg.BackgroundGeolocation.stop(); - return completer.future as String; - break; - case 'onHttp': - Completer completer = new Completer(); + } + }); + await bg.BackgroundGeolocation.start(); + await bg.BackgroundGeolocation.stop(); + return completer.future as String; + case 'onHttp': + Completer completer = new Completer(); - Map deviceParams = await bg.Config.deviceParams; - // Configure #url & #params - await bg.BackgroundGeolocation.setConfig(bg.Config( - autoSync: true, - extras: {"foo": "bar"}, - url: "http://tracker.transistorsoft.com/locations/transistor-flutter-test", - params: deviceParams - )); - // Clear database. - await bg.BackgroundGeolocation.destroyLocations(); + Map deviceParams = await bg.Config.deviceParams; + // Configure #url & #params + await bg.BackgroundGeolocation.setConfig(bg.Config( + autoSync: true, + extras: {"foo": "bar"}, + url: + "http://tracker.transistorsoft.com/locations/transistor-flutter-test", + params: deviceParams)); + // Clear database. + await bg.BackgroundGeolocation.destroyLocations(); - bg.BackgroundGeolocation.onHttp((bg.HttpEvent event) { - action.result = event; - app?.addAction(action); - completer.complete(event.toString()); - }); - bg.BackgroundGeolocation.start(); - return completer.future as String; - break; - case 'onHttp:404': - Completer completer = new Completer(); + bg.BackgroundGeolocation.onHttp((bg.HttpEvent event) { + action.result = event; + app?.addAction(action); + completer.complete(event.toString()); + }); + bg.BackgroundGeolocation.start(); + return completer.future as String; + case 'onHttp:404': + Completer completer = new Completer(); - Map deviceParams = await bg.Config.deviceParams; - // Configure bogus 404 #url & #params - await bg.BackgroundGeolocation.setConfig(bg.Config( - autoSync: true, - extras: {"foo": "bar"}, - url: "https://www.transistorsoft.com/test/error", - params: deviceParams - )); - // Clear database. - await bg.BackgroundGeolocation.destroyLocations(); + Map deviceParams = await bg.Config.deviceParams; + // Configure bogus 404 #url & #params + await bg.BackgroundGeolocation.setConfig(bg.Config( + autoSync: true, + extras: {"foo": "bar"}, + url: "https://www.transistorsoft.com/test/error", + params: deviceParams)); + // Clear database. + await bg.BackgroundGeolocation.destroyLocations(); - bg.BackgroundGeolocation.onHttp((bg.HttpEvent event) { - action.result = event; - app?.addAction(action); - completer.complete(event.toString()); - }); - bg.BackgroundGeolocation.start(); - return completer.future as String; - break; - case 'getCount': - await bg.BackgroundGeolocation.destroyLocations(); - await bg.BackgroundGeolocation.getCurrentPosition(samples: 1, persist: true); - int count = await bg.BackgroundGeolocation.count; - action.result = count; + bg.BackgroundGeolocation.onHttp((bg.HttpEvent event) { + action.result = event; app?.addAction(action); - return count.toString(); - break; - case 'destroyLocations': - await bg.BackgroundGeolocation.getCurrentPosition(samples: 1, persist: true); - bool result = await bg.BackgroundGeolocation.destroyLocations(); - print("destroyLocations: " + result.toString()); - int count = await bg.BackgroundGeolocation.count; - action.result = count; - app?.addAction(action); - return count.toString(); - break; - default: - return "404"; - } + completer.complete(event.toString()); + }); + bg.BackgroundGeolocation.start(); + return completer.future as String; + case 'getCount': + await bg.BackgroundGeolocation.destroyLocations(); + await bg.BackgroundGeolocation.getCurrentPosition( + samples: 1, persist: true); + int count = await bg.BackgroundGeolocation.count; + action.result = count; + app?.addAction(action); + return count.toString(); + case 'destroyLocations': + await bg.BackgroundGeolocation.getCurrentPosition( + samples: 1, persist: true); + bool result = await bg.BackgroundGeolocation.destroyLocations(); + print("destroyLocations: " + result.toString()); + int count = await bg.BackgroundGeolocation.count; + action.result = count; + app?.addAction(action); + return count.toString(); + default: + return "404"; + } } /// @@ -242,12 +229,10 @@ class Action { /// Layout a simple test-app to run the actions and render the results. /// class TestApp extends StatefulWidget { - late _TestAppState _state; + final _TestAppState _state = new _TestAppState(); @override - _TestAppState createState() { - _state = new _TestAppState(); return _state; } @@ -281,20 +266,21 @@ class _TestAppState extends State { //accentColor: Colors.black, primaryColor: Colors.black, //cursorColor: Colors.white, - backgroundColor: Colors.black, - primaryTextTheme: Theme.of(context).primaryTextTheme.apply( - bodyColor: Colors.white - ) - ), + colorScheme: Theme.of(context) + .colorScheme + .copyWith(background: Colors.black), + primaryTextTheme: Theme.of(context) + .primaryTextTheme + .apply(bodyColor: Colors.white)), home: Scaffold( appBar: AppBar( leading: Column( mainAxisAlignment: MainAxisAlignment.center, - children: [Text('Tests: ' + _actions.length.toString())] - ), + children: [ + Text('Tests: ' + _actions.length.toString()) + ]), title: Text('BG Geo Tester'), - actions: [] - ), + actions: []), body: Container( color: Colors.black, //crossAxisAlignment: CrossAxisAlignment.stretch, @@ -304,17 +290,15 @@ class _TestAppState extends State { Action action = _actions[index]; return InputDecorator( decoration: InputDecoration( - contentPadding: EdgeInsets.only(left: 10.0, top: 10.0, bottom: 10.0), - labelStyle: TextStyle(color: Colors.amberAccent, fontSize: 20.0), - labelText: action.command - ), - child: new Text(action.result.toString(), style: TextStyle(color: Colors.white)) - ); - } - ), + contentPadding: EdgeInsets.only( + left: 10.0, top: 10.0, bottom: 10.0), + labelStyle: TextStyle( + color: Colors.amberAccent, fontSize: 20.0), + labelText: action.command), + child: new Text(action.result.toString(), + style: TextStyle(color: Colors.white))); + }), //Card(child: Text(_command)), - ) - ) - ); + ))); } }