import 'dart:async';

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

void main() {
  runApp(MaterialApp(
    home: MyApp()),
  );
}

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

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  List <double> _accelerometerValues;
  final _streamSubscriptions = <StreamSubscription<dynamic>>[];

  @override
  Widget build(BuildContext context) {
    double x = _accelerometerValues[0];
    double y = _accelerometerValues[1];
    double z = _accelerometerValues[2];

    return Scaffold(
      appBar: AppBar(
        title: Text('Dati Accelerometro')
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          Text('asse x: $x'),
          Text('asse y: $y'),
          Text('asse z: $z')
        ],

      )
        floatingActionButton: FloatingActionButton (

    ),

    );
  }

  @override
  void initState() {
    super.initState();
    _streamSubscriptions.add(
        accelerometerEvents.listen((AccelerometerEvent event){
          setState(() {
            _accelerometerValues = <double> [event.x, event.y, event.z];
          });
        }
        ));

  }

  @override
  void dispose() {
    super.dispose();
    for (final subscription in _streamSubscriptions) {
      subscription.cancel();
    }
  }
}