Skip to content

Commit 206e3b7

Browse files
committed
add google maps reference
1 parent 40cb4c9 commit 206e3b7

File tree

5 files changed

+199
-15
lines changed

5 files changed

+199
-15
lines changed

lib/main.dart

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,30 @@ import 'package:zenith_monitor/modules/signup/screen/sign_up_screen.dart';
1919
import 'package:zenith_monitor/utils/ui/animations/zenith_progress_indicator.dart';
2020
import 'package:zenith_monitor/modules/login/screen/login_screen.dart';
2121
import 'package:firebase_auth/firebase_auth.dart';
22+
import 'package:permission_handler/permission_handler.dart';
2223

2324
void main() async {
2425
WidgetsFlutterBinding.ensureInitialized();
2526
await Firebase.initializeApp(
2627
options: DefaultFirebaseOptions.currentPlatform,
2728
);
2829
await dotenv.load(fileName: ".env");
30+
await requestBluetoothPermissions();
2931
runApp(const ZenithMonitor());
3032
}
3133

34+
Future<void> requestBluetoothPermissions() async {
35+
final statusScan = await Permission.bluetoothScan.request();
36+
final statusConnect = await Permission.bluetoothConnect.request();
37+
final statusLocation = await Permission.locationWhenInUse.request();
38+
39+
if (!statusScan.isGranted ||
40+
!statusConnect.isGranted ||
41+
!statusLocation.isGranted) {
42+
openAppSettings();
43+
}
44+
}
45+
3246
class ZenithMonitor extends StatelessWidget {
3347
const ZenithMonitor({Key? key}) : super(key: key);
3448

@@ -82,8 +96,8 @@ class Application extends StatelessWidget {
8296
backgroundColor: Colors.black.withOpacity(0)),
8397
primaryColor: Colors.black,
8498
),
85-
initialRoute: currentUser != null ? '/home' : '/login',
86-
// initialRoute: '/login',
99+
//initialRoute: currentUser != null ? '/home' : '/login',
100+
initialRoute: '/login',
87101
routes: {
88102
'/login': (context) => const LoginScreen(),
89103
'/signup': (context) => const SignUpScreen(),

lib/modules/bluetooth/ChatPage.dart

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'dart:typed_data';
44

55
import 'package:flutter/material.dart';
66
import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';
7+
import 'package:url_launcher/url_launcher.dart';
78

89
class ChatPage extends StatefulWidget {
910
final BluetoothDevice server;
@@ -17,8 +18,10 @@ class ChatPage extends StatefulWidget {
1718
class Message {
1819
int whom;
1920
String text;
21+
double? latitude;
22+
double? longitude;
2023

21-
Message(this.whom, this.text);
24+
Message(this.whom, this.text, {this.latitude, this.longitude});
2225
}
2326

2427
class ChatPageState extends State<ChatPage> {
@@ -28,8 +31,7 @@ class ChatPageState extends State<ChatPage> {
2831
List<Message> messages = List<Message>.empty(growable: true);
2932
String _messageBuffer = '';
3033

31-
final TextEditingController textEditingController =
32-
new TextEditingController();
34+
final TextEditingController textEditingController = TextEditingController();
3335
final ScrollController listScrollController = ScrollController();
3436

3537
bool isConnecting = true;
@@ -99,11 +101,27 @@ class ChatPageState extends State<ChatPage> {
99101
color:
100102
_message.whom == clientID ? Colors.blueAccent : Colors.grey,
101103
borderRadius: BorderRadius.circular(7.0)),
102-
child: Text(
103-
(text) {
104-
return text == '/shrug' ? \\_(ツ)_/¯' : text;
105-
}(_message.text.trim()),
106-
style: const TextStyle(color: Colors.white)),
104+
child: Column(
105+
crossAxisAlignment: CrossAxisAlignment.start,
106+
children: [
107+
Text(
108+
(text) {
109+
return text == '/shrug' ? \\_(ツ)_/¯' : text;
110+
}(_message.text.trim()),
111+
style: const TextStyle(color: Colors.white),
112+
),
113+
if (_message.longitude != null &&
114+
_message.latitude != null) ...[
115+
const SizedBox(height: 8.0),
116+
ElevatedButton(
117+
onPressed: () {
118+
openGoogleMaps(_message.latitude!, _message.longitude!);
119+
},
120+
child: const Text('Abrir endereço no Google Maps'),
121+
),
122+
],
123+
],
124+
),
107125
),
108126
],
109127
);
@@ -113,10 +131,10 @@ class ChatPageState extends State<ChatPage> {
113131
return Scaffold(
114132
appBar: AppBar(
115133
title: (isConnecting
116-
? Text('Connecting chat to ' + serverName + '...')
134+
? Text('Connecting chat to $serverName...')
117135
: isConnected
118-
? Text('Live chat with ' + serverName)
119-
: Text('Chat log with ' + serverName))),
136+
? Text('Live chat with $serverName')
137+
: Text('Chat log with $serverName'))),
120138
body: SafeArea(
121139
child: Column(
122140
children: <Widget>[
@@ -162,6 +180,17 @@ class ChatPageState extends State<ChatPage> {
162180
);
163181
}
164182

183+
Future<void> openGoogleMaps(double latitude, double longitude) async {
184+
final Uri googleMapsUrl = Uri.parse(
185+
'https://www.google.com/maps/search/?api=1&query=$latitude,$longitude');
186+
187+
if (await canLaunchUrl(googleMapsUrl)) {
188+
await launchUrl(googleMapsUrl);
189+
} else {
190+
throw 'Could not open the map.';
191+
}
192+
}
193+
165194
void _onDataReceived(Uint8List data) {
166195
// Allocate buffer for parsed data
167196
int backspacesCounter = 0;
@@ -187,8 +216,23 @@ class ChatPageState extends State<ChatPage> {
187216
}
188217
}
189218

190-
// Create message if there is new line character
219+
// Cria uma string a partir do buffer
191220
String dataString = String.fromCharCodes(buffer);
221+
222+
// Divide a string em partes usando ponto e vírgula como delimitador
223+
List<String> parts = dataString.split(';');
224+
String message = dataString; // Mensagem original
225+
double? longitude;
226+
double? latitude;
227+
228+
if (parts.length >= 3) {
229+
// Extrai longitude e latitude (segundo e terceiro valores)
230+
longitude = double.tryParse(parts[1].trim());
231+
latitude = double.tryParse(parts[2].trim());
232+
}
233+
234+
// Create message if there is new line character
235+
// String dataString = String.fromCharCodes(buffer);
192236
int index = buffer.indexOf(13);
193237
if (~index != 0) {
194238
setState(() {
@@ -199,6 +243,8 @@ class ChatPageState extends State<ChatPage> {
199243
? _messageBuffer.substring(
200244
0, _messageBuffer.length - backspacesCounter)
201245
: _messageBuffer + dataString.substring(0, index),
246+
longitude: longitude,
247+
latitude: latitude,
202248
),
203249
);
204250
_messageBuffer = dataString.substring(index);
@@ -217,7 +263,7 @@ class ChatPageState extends State<ChatPage> {
217263

218264
if (text.isNotEmpty) {
219265
try {
220-
connection!.output.add(Uint8List.fromList(utf8.encode(text + "\r\n")));
266+
connection!.output.add(Uint8List.fromList(utf8.encode("$text\r\n")));
221267
await connection!.output.allSent;
222268

223269
setState(() {

lib/modules/bluetooth/MainPage.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import './BackgroundCollectingTask.dart';
99
import './ChatPage.dart';
1010
import './DiscoveryPage.dart';
1111
import './SelectBondedDevicePage.dart';
12+
import 'package:url_launcher/url_launcher.dart';
1213

1314
// import './helpers/LineChart.dart';
1415

@@ -114,6 +115,12 @@ class MainPageState extends State<MainPage> {
114115
});
115116
},
116117
),
118+
ListTile(
119+
title: const Text("Teste google maps"),
120+
onTap: () async {
121+
await openGoogleMaps(48.8584, 2.2945);
122+
},
123+
),
117124
ListTile(
118125
title: const Text('Bluetooth status'),
119126
subtitle: Text(_bluetoothState.toString()),
@@ -323,6 +330,17 @@ class MainPageState extends State<MainPage> {
323330
);
324331
}
325332

333+
Future<void> openGoogleMaps(double latitude, double longitude) async {
334+
final Uri googleMapsUrl = Uri.parse(
335+
'https://www.google.com/maps/search/?api=1&query=$latitude,$longitude');
336+
337+
if (await canLaunchUrl(googleMapsUrl)) {
338+
await launchUrl(googleMapsUrl);
339+
} else {
340+
throw 'Could not open the map.';
341+
}
342+
}
343+
326344
Future<void> _startBackgroundTask(
327345
BuildContext context,
328346
BluetoothDevice server,

pubspec.lock

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,46 @@ packages:
808808
url: "https://pub.dev"
809809
source: hosted
810810
version: "2.3.0"
811+
permission_handler:
812+
dependency: "direct main"
813+
description:
814+
name: permission_handler
815+
sha256: bc56bfe9d3f44c3c612d8d393bd9b174eb796d706759f9b495ac254e4294baa5
816+
url: "https://pub.dev"
817+
source: hosted
818+
version: "10.4.5"
819+
permission_handler_android:
820+
dependency: transitive
821+
description:
822+
name: permission_handler_android
823+
sha256: "59c6322171c29df93a22d150ad95f3aa19ed86542eaec409ab2691b8f35f9a47"
824+
url: "https://pub.dev"
825+
source: hosted
826+
version: "10.3.6"
827+
permission_handler_apple:
828+
dependency: transitive
829+
description:
830+
name: permission_handler_apple
831+
sha256: "99e220bce3f8877c78e4ace901082fb29fa1b4ebde529ad0932d8d664b34f3f5"
832+
url: "https://pub.dev"
833+
source: hosted
834+
version: "9.1.4"
835+
permission_handler_platform_interface:
836+
dependency: transitive
837+
description:
838+
name: permission_handler_platform_interface
839+
sha256: "6760eb5ef34589224771010805bea6054ad28453906936f843a8cc4d3a55c4a4"
840+
url: "https://pub.dev"
841+
source: hosted
842+
version: "3.12.0"
843+
permission_handler_windows:
844+
dependency: transitive
845+
description:
846+
name: permission_handler_windows
847+
sha256: cc074aace208760f1eee6aa4fae766b45d947df85bc831cde77009cdb4720098
848+
url: "https://pub.dev"
849+
source: hosted
850+
version: "0.1.3"
811851
petitparser:
812852
dependency: transitive
813853
description:
@@ -941,6 +981,70 @@ packages:
941981
url: "https://pub.dev"
942982
source: hosted
943983
version: "1.3.2"
984+
url_launcher:
985+
dependency: "direct main"
986+
description:
987+
name: url_launcher
988+
sha256: "21b704ce5fa560ea9f3b525b43601c678728ba46725bab9b01187b4831377ed3"
989+
url: "https://pub.dev"
990+
source: hosted
991+
version: "6.3.0"
992+
url_launcher_android:
993+
dependency: transitive
994+
description:
995+
name: url_launcher_android
996+
sha256: f0c73347dfcfa5b3db8bc06e1502668265d39c08f310c29bff4e28eea9699f79
997+
url: "https://pub.dev"
998+
source: hosted
999+
version: "6.3.9"
1000+
url_launcher_ios:
1001+
dependency: transitive
1002+
description:
1003+
name: url_launcher_ios
1004+
sha256: e43b677296fadce447e987a2f519dcf5f6d1e527dc35d01ffab4fff5b8a7063e
1005+
url: "https://pub.dev"
1006+
source: hosted
1007+
version: "6.3.1"
1008+
url_launcher_linux:
1009+
dependency: transitive
1010+
description:
1011+
name: url_launcher_linux
1012+
sha256: e2b9622b4007f97f504cd64c0128309dfb978ae66adbe944125ed9e1750f06af
1013+
url: "https://pub.dev"
1014+
source: hosted
1015+
version: "3.2.0"
1016+
url_launcher_macos:
1017+
dependency: transitive
1018+
description:
1019+
name: url_launcher_macos
1020+
sha256: "9a1a42d5d2d95400c795b2914c36fdcb525870c752569438e4ebb09a2b5d90de"
1021+
url: "https://pub.dev"
1022+
source: hosted
1023+
version: "3.2.0"
1024+
url_launcher_platform_interface:
1025+
dependency: transitive
1026+
description:
1027+
name: url_launcher_platform_interface
1028+
sha256: "552f8a1e663569be95a8190206a38187b531910283c3e982193e4f2733f01029"
1029+
url: "https://pub.dev"
1030+
source: hosted
1031+
version: "2.3.2"
1032+
url_launcher_web:
1033+
dependency: transitive
1034+
description:
1035+
name: url_launcher_web
1036+
sha256: "772638d3b34c779ede05ba3d38af34657a05ac55b06279ea6edd409e323dca8e"
1037+
url: "https://pub.dev"
1038+
source: hosted
1039+
version: "2.3.3"
1040+
url_launcher_windows:
1041+
dependency: transitive
1042+
description:
1043+
name: url_launcher_windows
1044+
sha256: "49c10f879746271804767cb45551ec5592cdab00ee105c06dddde1a98f73b185"
1045+
url: "https://pub.dev"
1046+
source: hosted
1047+
version: "3.1.2"
9441048
usb_serial:
9451049
dependency: "direct main"
9461050
description:

pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ dependencies:
5353
flutter_blue_plus: ^1.32.12
5454
flutter_bluetooth_serial: ^0.4.0
5555
scoped_model: ^2.0.0
56+
url_launcher: ^6.1.10
57+
permission_handler: ^10.2.0
5658

5759
dependency_overrides:
5860
firebase_core_platform_interface: 5.1.0

0 commit comments

Comments
 (0)