flutter_sensor_compass 0.1.1 copy "flutter_sensor_compass: ^0.1.1" to clipboard
flutter_sensor_compass: ^0.1.1 copied to clipboard

A plugin to get the orientation of the device (in degrees) relative to the magnetic north.

example/lib/main.dart

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

import 'package:flutter_sensor_compass/flutter_sensor_compass.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool _compassEnabled = false;
  double _degrees = 0.0;
  StreamSubscription _compassSubscription;

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

  @override
  void dispose() {
    _stopCompass();
    super.dispose();
  }

  void _checkCompassAvailability() async {
    Compass().isCompassAvailable().then((value) {
      setState(() {
        _compassEnabled = value;
      });
    });
  }

  void _startCompass() {
    if (_compassSubscription != null) return;
    _compassSubscription = Compass()
        .compassUpdates(interval: Duration(milliseconds: 200))
        .listen((value) {
      setState(() {
        _degrees = value;
      });
    });
  }

  void _stopCompass() {
    if (_compassSubscription == null) return;
    _compassSubscription.cancel();
    _compassSubscription = null;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Compass example'),
        ),
        body: Container(
          padding: EdgeInsets.all(16.0),
          alignment: AlignmentDirectional.topCenter,
          child: Column(
            children: <Widget>[
              Text("Compass Enabled: $_compassEnabled"),
              Padding(padding: EdgeInsets.only(top: 16.0)),
              Text("Degrees: $_degrees"),
              Padding(padding: EdgeInsets.only(top: 16.0)),
              MaterialButton(
                child: Text("Start"),
                color: Colors.green,
                onPressed: _compassEnabled ? () => _startCompass() : null,
              ),
              MaterialButton(
                child: Text("Stop"),
                color: Colors.red,
                onPressed: _compassEnabled ? () => _stopCompass() : null,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
3
likes
40
pub points
16%
popularity

Publisher

unverified uploader

A plugin to get the orientation of the device (in degrees) relative to the magnetic north.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

flutter, flutter_sensors, vector_math

More

Packages that depend on flutter_sensor_compass