sentiance_smart_geofences 0.0.20 copy "sentiance_smart_geofences: ^0.0.20" to clipboard
sentiance_smart_geofences: ^0.0.20 copied to clipboard

Flutter library for Sentiance SDK

example/lib/main.dart

import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:sentiance_core/sentiance_core.dart';
import 'package:sentiance_smart_geofences/sentiance_smart_geofences.dart';

// Importing background.dart is necessary if the entry points for background
// Tasks are defined in this file. Even if no methods from background.dart are
// called in this file, the Dart compiler will still include background.dart
// in the application bundle because it's imported here.

// ignore: unused_import
import 'background.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  DetectionMode? _detectionMode;
  String? lastGeofenceRefreshResult;
  String? userId;

  final _sentianceCorePlugin = SentianceCore();
  final _sentianceSmartGeofencesPlugin = SentianceSmartGeofences(api: SentianceApi());

  @override
  void initState() {
    super.initState();
    _sentianceCorePlugin.getUserId()
    .then((id) {
      setState(() {
        userId = id;
      });
    });
  }

  Future<void> refreshGeofences() async {
    String? result;

    _sentianceCorePlugin.getUserId();

    try {
      await _sentianceSmartGeofencesPlugin.refreshGeofences();
      result = "SUCCESS";
    } on PlatformException catch (e) {
      if (Platform.isAndroid) {
        result = "${e.message}";
      } else {
        result = "${e.message} - ${e.details}";
      }
    }

    setState(() {
      lastGeofenceRefreshResult = result;
    });
  }

  Future<void> getCurrentDetectionMode() async {
    DetectionMode? detectionMode;
    try {
      detectionMode = await _sentianceSmartGeofencesPlugin.getDetectionMode();
    } on PlatformException catch (e) {
      debugPrint(
          "Error getting detection mode: ${e.message}, code: ${e.code}, details: ${e.details}");
    }

    if (!mounted) return;

    setState(() {
      _detectionMode = detectionMode;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text(
            'Smart Geofences Plugin ${Platform.operatingSystem} example app',
            style: const TextStyle(
              color: Colors.black,
              fontWeight: FontWeight.bold,
              fontSize: 16,
            ),
          ),
        ),
        body: Center(
          child: Padding(
            padding: const EdgeInsets.all(16.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    const Text(
                      "User ID: ",
                      style: TextStyle(
                          color: Colors.black,
                          fontWeight: FontWeight.bold,
                          fontSize: 16),
                    ),
                    Text(
                      userId ?? "unknown",
                      style: const TextStyle(color: Colors.black, fontSize: 12),
                    ),
                  ],
                ),
                Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    const Text(
                      "Detection mode: ",
                      style: TextStyle(
                          color: Colors.black,
                          fontWeight: FontWeight.bold,
                          fontSize: 16),
                    ),
                    Text(
                      _detectionMode?.name ?? "",
                      style: const TextStyle(color: Colors.black, fontSize: 12),
                    ),
                  ],
                ),
                ElevatedButton(
                  onPressed: () async {
                    await getCurrentDetectionMode();
                  },
                  child: const Text("Query detection mode"),
                ),
                const SizedBox(
                  height: 16,
                ),
                const Text(
                  "Last geofence refresh result: ",
                  style: TextStyle(
                      color: Colors.black,
                      fontWeight: FontWeight.bold,
                      fontSize: 16),
                ),
                Text(
                  lastGeofenceRefreshResult ?? "Nothing yet",
                  style: const TextStyle(color: Colors.black, fontSize: 12),
                ),
                ElevatedButton(
                  onPressed: () async {
                    await refreshGeofences();
                  },
                  child: const Text("Refresh geofences"),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}