geofence_flutter 2.0.0 geofence_flutter: ^2.0.0 copied to clipboard
Flutter geofencing plugin for flutter application (android and ios).
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:geofence_flutter/geofence_flutter.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Geofence',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Geofence'),
debugShowCheckedModeBanner: false,
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
StreamSubscription<GeofenceEvent>? geofenceEventStream;
String geofenceEvent = '';
TextEditingController latitudeController = new TextEditingController();
TextEditingController longitudeController = new TextEditingController();
TextEditingController radiusController = new TextEditingController();
@override
void initState() {
// TODO: implement initState
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
// mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"Geofence Event: " + geofenceEvent,
),
TextField(
controller: latitudeController,
decoration: InputDecoration(
border: InputBorder.none, hintText: 'Enter pointed latitude'),
),
TextField(
controller: longitudeController,
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Enter pointed longitude'),
),
TextField(
controller: radiusController,
decoration: InputDecoration(
border: InputBorder.none, hintText: 'Enter radius meter'),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextButton(
child: Text("Start"),
onPressed: () async {
print("start");
await Geofence.startGeofenceService(
pointedLatitude: latitudeController.text,
pointedLongitude: longitudeController.text,
radiusMeter: radiusController.text,
eventPeriodInSeconds: 10);
if (geofenceEventStream == null) {
geofenceEventStream = Geofence.getGeofenceStream()
?.listen((GeofenceEvent event) {
print(event.toString());
setState(() {
geofenceEvent = event.toString();
});
});
}
},
),
SizedBox(
width: 10.0,
),
TextButton(
child: Text("Stop"),
onPressed: () {
print("stop");
Geofence.stopGeofenceService();
geofenceEventStream?.cancel();
},
),
],
),
],
),
),
);
}
@override
void dispose() {
// TODO: implement dispose
latitudeController.dispose();
longitudeController.dispose();
radiusController.dispose();
super.dispose();
}
}