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

Access all device sensors and stream accelerometer in Flutter

example/lib/main.dart

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

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

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

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(home: SensorPage());
  }
}

class SensorPage extends StatefulWidget {
  const SensorPage({super.key});
  @override
  State<SensorPage> createState() => _SensorPageState();
}

class _SensorPageState extends State<SensorPage> {
  List<Map<String, dynamic>>? sensors;
  Map<String, dynamic>? accelerometer;

  @override
  void initState() {
    super.initState();
    loadSensors();
    // Listen to accelerometer stream
    AllDeviceSensorsStream.accelerometerStream.listen((data) {
      setState(() {
        accelerometer = data;
      });
    });
  }

  Future<void> loadSensors() async {
    final result = await AllDeviceSensors().getSensors();
    setState(() {
      sensors = result;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('All Device Sensors')),
      body: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          children: [
            if (accelerometer != null)
              Text(
                'Accelerometer: x=${accelerometer!['x']}, y=${accelerometer!['y']}, z=${accelerometer!['z']}',
                style: const TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.bold,
                ),
              ),
            const SizedBox(height: 20),
            Expanded(
              child: sensors == null
                  ? const Center(child: CircularProgressIndicator())
                  : ListView.builder(
                      itemCount: sensors!.length,
                      itemBuilder: (context, index) {
                        final sensor = sensors![index];
                        return Card(
                          margin: const EdgeInsets.symmetric(vertical: 6),
                          child: Padding(
                            padding: const EdgeInsets.all(12),
                            child: Text(
                              'Name: ${sensor['name']}\n'
                              'Type: ${sensor['type']}\n'
                              'Vendor: ${sensor['vendor']}\n'
                              'Power: ${sensor['power']} mA\n'
                              'Resolution: ${sensor['resolution']}',
                              style: const TextStyle(fontSize: 16),
                            ),
                          ),
                        );
                      },
                    ),
            ),
          ],
        ),
      ),
    );
  }
}
0
likes
140
points
68
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Access all device sensors and stream accelerometer in Flutter

Repository (GitHub)

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on all_device_sensors

Packages that implement all_device_sensors