flutter_background_geofencing 1.0.0 copy "flutter_background_geofencing: ^1.0.0" to clipboard
flutter_background_geofencing: ^1.0.0 copied to clipboard

A Flutter plugin for native background geofencing with battery-efficient hardware-assisted location monitoring on Android and iOS.

example/main.dart

import 'package:flutter/material.dart';
import 'package:flutter_background_geofencing/flutter_background_geofencing.dart';

void main() {
  runApp(GeofencingExampleApp());
}

class GeofencingExampleApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Geofencing Example',
      home: GeofencingExample(),
    );
  }
}

class GeofencingExample extends StatefulWidget {
  @override
  _GeofencingExampleState createState() => _GeofencingExampleState();
}

class _GeofencingExampleState extends State<GeofencingExample> {
  final GeofencingService _geofencingService = GeofencingService();
  bool _isInitialized = false;
  String _status = 'Not initialized';

  @override
  void initState() {
    super.initState();
    _initializeGeofencing();
  }

  Future<void> _initializeGeofencing() async {
    try {
      setState(() => _status = 'Initializing...');
      
      // 1. Initialize the service
      await _geofencingService.initialize();
      setState(() => _status = 'Requesting permissions...');
      
      // 2. Request permissions
      await _geofencingService.requestPermissions();
      setState(() => _status = 'Starting service...');
      
      // 3. Start the background service
      await _geofencingService.startService(
        notificationTitle: 'Geofencing Active',
        notificationText: 'Monitoring location regions',
      );
      
      // 4. Add a sample geofence (San Francisco)
      final region = GeofenceRegion(
        id: 'sample_location',
        latitude: 37.7749,
        longitude: -122.4194,
        radius: 100.0,
        data: {'name': 'Sample Location'},
      );
      
      await _geofencingService.addGeofence(region);
      
      // 5. Listen for events
      _geofencingService.onGeofenceEvent.listen((event) {
        setState(() {
          _status = 'Event: ${event.type} for ${event.regionId}';
        });
        
        if (event.type == GeofenceEventType.enter) {
          _showSnackBar('Entered ${event.regionId}');
        } else if (event.type == GeofenceEventType.exit) {
          _showSnackBar('Exited ${event.regionId}');
        }
      });
      
      setState(() {
        _isInitialized = true;
        _status = 'Monitoring 1 region';
      });
      
    } catch (e) {
      setState(() => _status = 'Error: $e');
    }
  }

  void _showSnackBar(String message) {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text(message)),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Geofencing Example'),
      ),
      body: Padding(
        padding: EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Card(
              child: Padding(
                padding: EdgeInsets.all(16.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text('Status:', style: Theme.of(context).textTheme.titleMedium),
                    SizedBox(height: 8),
                    Text(_status),
                  ],
                ),
              ),
            ),
            SizedBox(height: 16),
            if (_isInitialized) ...[
              Text(
                'Sample geofence added:',
                style: Theme.of(context).textTheme.titleMedium,
              ),
              SizedBox(height: 8),
              Card(
                child: ListTile(
                  leading: Icon(Icons.location_on),
                  title: Text('Sample Location'),
                  subtitle: Text('37.7749, -122.4194 (100m radius)'),
                ),
              ),
              SizedBox(height: 16),
              Text(
                'Move to the coordinates above to trigger entry/exit events.',
                style: Theme.of(context).textTheme.bodySmall,
              ),
            ],
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    _geofencingService.dispose();
    super.dispose();
  }
}
3
likes
150
points
90
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter plugin for native background geofencing with battery-efficient hardware-assisted location monitoring on Android and iOS.

Repository (GitHub)
View/report issues

Topics

#geofencing #location #background #native #flutter

Documentation

API reference

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on flutter_background_geofencing

Packages that implement flutter_background_geofencing