compassx 1.0.1 copy "compassx: ^1.0.1" to clipboard
compassx: ^1.0.1 copied to clipboard

Flutter compass plugin offering true heading, accuracy, and calibration needs.

example/lib/main.dart

import 'dart:io';

import 'package:compassx/compassx.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:math' as math;

import 'package:permission_handler/permission_handler.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);
  runApp(const App());
}

class App extends StatelessWidget {
  const App({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: DefaultTextStyle(
          style: Theme.of(context).textTheme.headlineSmall!,
          child: Center(
            child: StreamBuilder<CompassXEvent>(
              stream: CompassX.events,
              builder: (context, snapshot) {
                if (snapshot.hasError) return Text(snapshot.error.toString());
                if (!snapshot.hasData) return const CircularProgressIndicator();
                final compass = snapshot.data!;
                return Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text('Heading: ${compass.heading}'),
                    Text('Accuracy: ${compass.accuracy}'),
                    Text('Should calibrate: ${compass.shouldCalibrate}'),
                    Transform.rotate(
                      angle: compass.heading * math.pi / 180,
                      child: Icon(
                        Icons.arrow_upward_rounded,
                        size: MediaQuery.of(context).size.width - 80,
                      ),
                    ),
                    FilledButton(
                      onPressed: () async {
                        if (!Platform.isAndroid) return;
                        await Permission.location.request();
                      },
                      child: const Text('Request permission'),
                    ),
                  ],
                );
              },
            ),
          ),
        ),
      ),
    );
  }
}
17
likes
160
points
1.83k
downloads
screenshot

Publisher

verified publishermidoridesign.studio

Weekly Downloads

Flutter compass plugin offering true heading, accuracy, and calibration needs.

Homepage
Repository (GitHub)
View/report issues
Contributing

Topics

#compass #sensor #heading #north #calibration

Documentation

Documentation
API reference

Funding

Consider supporting this project:

github.com

License

BSD-3-Clause (license)

Dependencies

flutter

More

Packages that depend on compassx