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

outdated

Jawal SDK Location for flutter

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:jawal_flutter/events/init_result_event.dart';
import 'package:jawal_flutter/events/location_change_event.dart';
import 'package:jawal_flutter/jawal_config.dart';
import 'package:jawal_flutter/models/permission_status.dart';
import 'package:jawal_flutter/jawal.dart';

void main() {
  runApp(const MyApp());
}

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

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

class _MyAppState extends State<MyApp> {
  bool _initialized = false;
  bool _tracking = false;

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

  void bootstrap() async {
    initJawalFlutter();
    final PermissionStatus locationStatus = await Jawal.requestLocationPermission();
    if(locationStatus.isGranted){
      ///Ask for the background location permission
      ///Optional: You can skip this step if you don't need background location
      await Jawal.requestBackgroundLocationPermission();
    }else if(locationStatus.isPermanentlyDenied) {
      ///We can alert the user that the location permission is needed
      ///and redirect him to the app settings
    }
  }

  void initJawalFlutter() async {
    JawalConfig config = JawalConfig(
      apiKey: "YotYDk2J3970YieJKyi7upTr79qzHyZI",
      userId: "test_flutter_1", // Unique user id
      userDescription: "Flutter Test User 1", //Optional
      onInitResult: (InitResultEvent event) {
        setState(() {
          _initialized = event.isSuccessful;
        });
      },
    );
    await Jawal.init(config);
    Jawal.onLocationChange((LocationChangeEvent event) {
      ///Do something with the location
    });
  }

  void toggleTracking()  async {
    Jawal.isTracking().then((value) => {
      if(value) {
        Jawal.stopTracking()
      }else{
        Jawal.startTracking()
      }
    });
    setState(() {
      _tracking = !_tracking;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Jawal Flutter Demo App'),
        ),
        body: Center(
            child: Column(
          children: [
            Container(
              margin: const EdgeInsets.only(top: 20),
              child: Text(
                _initialized
                    ? "Jawal Flutter is initialized"
                    : "Jawal Flutter is not initialized",
                style: const TextStyle(fontSize: 20),
              ),
            ),
            ElevatedButton(
                onPressed: (_initialized) ? toggleTracking : null,
                child: Text((_tracking) ? "Stop Tracking" : "Start Tracking")),
          ],
        )),
      ),
    );
  }
}